[C++-sig] Re: Re: Re: Re: Re: Re: custom iterator object

Mike Rovner mike at bindkey.com
Tue Nov 12 18:52:00 CET 2002


"David Abrahams" <dave at boost-consulting.com> wrote in message
news:usmy6zxo8.fsf at boost-consulting.com...
> What is CustomIter? I've never seen this before.  I'm sorry, but I
> can't be expected to understand your code if you don't give me a
> complete picture.

OK. Let me try to give a whole picture:

1) I have an API:

typedef int* T; // some type

class CustomIter {
  T First();
  T Next();
};

class Scheme {
    CustomIter GetIter() const;
    CustomIter GetIter1() const;
    CustomIter GetIter2() const;
};

2) I want to expose Scheme to Python with 3 iterators
GetIter as __iter__, GetIter1 as attribute 'base', GetIter2 as attribute
'top'
i.e. I want to be able to write:
  s=Scheme()
  for i in s: print i
  for i in s.base: print i
  for i in s.top: print i

3) In order to do that I created C++ wrap for CustomIter:

class MyIter {
    MyIter(CustomIter& init);
    T next();
};

and exposed it:

object identity(object x)  { return x; }

class_<MyIter>("_myiter", no_init)
   .def("__iter__", identity)
   .def("next, &MyIter::next)
 ;

Now I use 'make_iter' to create a 'iter_gen' object instance with operator()
which when called with Scheme& produces MyIter object:

class iter_gen {
  typedef CustomIter (Scheme::*PM)() const;
  iter_gen(PM pm) : _pm(pm);
  MyIter operator()(Scheme& s) { return MyIter((s.*pm)()); }
private:
  PM _pm;
};

iter_gen make_iter(PM pm) { return iter_gen(pm); }

Then I hope to wrap Scheme with using of that 'iter_gen' objects to produce
my iterators.

make_iter(&Scheme::GetIter)
make_iter(&Scheme::GetIter1)
make_iter(&Scheme::GetIter2)

But I still don't know how.
And even not sure that I'm going in right direction at all :(

Thank you for your patience,
Mike








More information about the Cplusplus-sig mailing list