Boost logo

Boost :

From: Anton Gluck (gluc_at_[hidden])
Date: 2000-11-29 00:39:03


Ullrich,

On Mon, 27 Nov 2000, Ullrich Koethe wrote:

> Petr Kocmid wrote:
> >
> > > Don't expose iterators. Python has its own iteration idioms (e.g.
> > > "for x in
> > > S"), so just support the python sequence interface on your vector. Since
> > > your vector is a random-access container, this will be fast. I reccommend
> > > this approach.
> >
> > I predict the problem Anton had will arise many times in the future. Perhaps
> > we can
> > add some generic conversion of STL containers to sequence/mapping protocol
> > to py_cpp.
> >
>
>
> I've implemented and successfully tested the following "Cursor"-feature
> to wrap STL iterators:
>
> in C++:
>
> typedef std::list<Foo> FooList;
> boost::python::class_builder<FooList> foolist_class(my_module,
> "FooList");
> foolist_class.def(boost::python::constructor<>());
> foolist_class.def((void (FooList::*)(Foo const
> &))&FooList::push_back, "append");
>
> // define a cursor for FooLists
> my_module.def_cursor_for(foolist_class);
>
> Then in Python:
>
> >>> fooList = FooList()
> >>> fooList.append(Foo(1))
> >>> fooList.append(Foo(2))
> >>>
> >>> for i in fooList.cusor(): # use cursor in a loop
> ... i
> ... print i.getData()
> ...
> <Foo object at 111070>
> 1
> <Foo object at 1110e8>
> 2
> >>>
> >>> c = fooList.cursor()
> >>> c[1].getData() # use cursor to read and write the data
> 2
> >>> c[1] = Foo(4)
> >>> c[1].getData()
> 4
>
> Would this suit your needs ?

I'm not sure if this was meant for me or Petr. I can see useful parts
here when creating an iterator, but can't see how it helps exposing an
existing iterator. I guess the magic is happening in
"my_module.def_cursor_for(foolist_class);"?

Gruesse,

Toni


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk