diff --git a/SWIG/Lib/python/pyiterators.swg b/SWIG/Lib/python/pyiterators.swg index 5092da852..3a760a29b 100644 --- a/SWIG/Lib/python/pyiterators.swg +++ b/SWIG/Lib/python/pyiterators.swg @@ -147,56 +147,25 @@ namespace swig { %fragment("PySwigIterator_T","header",fragment="StdTraits") { #include namespace swig { + template class PySwigIterator_T : public PySwigIterator { public: typedef OutIterator out_iterator; - typedef typename std::iterator_traits::value_type value_type; - + typedef typename std::iterator_traits::value_type value_type; typedef PySwigIterator_T self_type; - - PySwigIterator_T(out_iterator curr, out_iterator first, out_iterator last, PyObject *seq) - : PySwigIterator(seq), current(curr), begin(first), end(last) + + PySwigIterator_T(out_iterator curr, PyObject *seq) + : PySwigIterator(seq), current(curr) { } - - PyObject *value() const throw (stop_iteration) { - if (current == end) { - throw stop_iteration(); - } else { - return swig::from((const value_type&)*(current)); - } - } - - PySwigIterator *copy() const - { - return new self_type(*this); - } - PySwigIterator *incr(size_t n = 1) throw (stop_iteration) + const out_iterator& get_current() const { - while (n--) { - if (current == end) { - throw stop_iteration(); - } else { - ++current; - } - } - return this; + return current; } - PySwigIterator *decr(size_t n = 1) throw (stop_iteration) - { - while (n--) { - if (current == begin) { - throw stop_iteration(); - } else { - --current; - } - } - return this; - } bool equal (const PySwigIterator &iter) const throw (std::invalid_argument) { @@ -216,25 +185,121 @@ namespace swig { } else { throw std::invalid_argument("bad iterator type"); } + } + + protected: + out_iterator current; + }; + + + template + class PySwigIteratorOpen_T : public PySwigIterator_T + { + public: + typedef OutIterator out_iterator; + typedef typename std::iterator_traits::value_type value_type; + typedef PySwigIterator_T base; + typedef PySwigIteratorOpen_T self_type; + + PySwigIteratorOpen_T(out_iterator curr, PyObject *seq) + : PySwigIterator_T(curr, seq) + { } - const out_iterator& get_current() const + PyObject *value() const throw (stop_iteration) { + return swig::from((const value_type&)*(base::current)); + } + + PySwigIterator *copy() const { - return current; + return new self_type(*this); + } + + PySwigIterator *incr(size_t n = 1) throw (stop_iteration) + { + while (n--) { + ++base::current; + } + return this; + } + + PySwigIterator *decr(size_t n = 1) throw (stop_iteration) + { + while (n--) { + --base::current; + } + return this; + } + }; + + template + class PySwigIteratorClosed_T : public PySwigIterator_T + { + public: + typedef OutIterator out_iterator; + typedef typename std::iterator_traits::value_type value_type; + typedef PySwigIterator_T base; + typedef PySwigIteratorClosed_T self_type; + + PySwigIteratorClosed_T(out_iterator curr, out_iterator first, out_iterator last, PyObject *seq) + : PySwigIterator_T(curr, seq), begin(first), end(last) + { + } + + PyObject *value() const throw (stop_iteration) { + if (base::current == end) { + throw stop_iteration(); + } else { + return swig::from((const value_type&)*(base::current)); + } + } + + PySwigIterator *copy() const + { + return new self_type(*this); + } + + PySwigIterator *incr(size_t n = 1) throw (stop_iteration) + { + while (n--) { + if (base::current == end) { + throw stop_iteration(); + } else { + ++base::current; + } + } + return this; + } + + PySwigIterator *decr(size_t n = 1) throw (stop_iteration) + { + while (n--) { + if (base::current == begin) { + throw stop_iteration(); + } else { + --base::current; + } + } + return this; } private: - out_iterator current; out_iterator begin; out_iterator end; }; template inline PySwigIterator* - make_output_iterator(const OutIter& current, const OutIter& begin = OutIter(), - const OutIter& end = OutIter(), PyObject *seq = 0) + make_output_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, PyObject *seq = 0) { - return new PySwigIterator_T(current, begin, end, seq); + return new PySwigIteratorClosed_T(current, begin, end, seq); + } + + template + inline PySwigIterator* + make_output_iterator(const OutIter& current, PyObject *seq = 0) + { + return new PySwigIteratorOpen_T(current, seq); } } }