[C++-sig] Boost iterators

Martin Casado casado2 at llnl.gov
Fri Apr 12 23:28:58 CEST 2002


>
> #include <boost/python/errors.hpp>
> ...
>
>     PyErr_SetString(PyExc_StopIteration,"done iterating");
>     throw boost::python::error_already_set();

  works marvelously, thanks

>
> >  2. Currently, I have to add a function in my C++ class to return a
>
> newly
>
> >     constructed PyIter.  Is there a way to have boost create the
> >     type given an accessor method to the container or the container
>
> itself?
>
> Could you be more specific? What did you have in mind?

given

struct Foo{
  vector<int> v;
 
  const vector<int>& getV(){return v;}
}

I think it would be tres cool if you could say something like..

           .def("v", &Foo::getV, 
return_value_policy<create_iterator_from<vector<int> >())

  which would construct a PyIter object under the covers:  

  PyIter<vector<int> >(obj->getV());

  and return it to python.

  so:

  v = myfoo.v()

  for i in v:
    print i

  currently I have to declare a function in class foo() which returns a
  const PyIter<vector<int> >& which I would like to avoid.

  Or even better, I would like to be able to create an attribute "v"
  that has the same functionality (using the same mechanics)

  I would be happy to take a stab at writing the new return policy if you
  would affirm that it is possible and not extremely complicated.

  Thanks, :-)

                            ~~m






More information about the Cplusplus-sig mailing list