[C++-sig] custom iterator object

Mike Rovner mike at bindkey.com
Thu Nov 7 19:51:02 CET 2002


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.
Can you point me the right way to do it.

Thanks,
Mike








More information about the Cplusplus-sig mailing list