[C++-sig] custom iterator object

David Abrahams dave at boost-consulting.com
Thu Nov 7 20:21:16 CET 2002


"Mike Rovner" <mike at bindkey.com> writes:

> I have the following class, implementing my iterator protocol:
>
> template<class T>
> class Iter
> {
>     T* First();
>     T* Next();
> }
> returning 0, when iteration is done.
>
> I'm trying to wrap it to Python iterator protocol:
>
> template<class T>
> struct PyIter
> {
>   PyIter(IterFactory get_func) : started(false), it(get_func()) {}
>
>   const T* next()  {
>      const T* res;
>      if(started)
>        res=it.Next();
>      else {
>           started=true;
>           res=it.First(); }
>      if(res) return res;
>     objects::set_stop_iteration_error();
>     return 0;
> }
> private:
>   bool started;
>   Iter<T> it;
> };
>
> but fill it's clumsy and not good.

What's "not good" about it?

> Can you point me the right way to do it.

Boost.Python provides several ways to wrap regular C++ iterators as
Python iterators. Did you consider using those?

http://www.boost.org/libs/python/doc/v2/iterator.html

-- 
                    David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list