Boost logo

Boost :

From: Ullrich Koethe (koethe_at_[hidden])
Date: 2000-11-27 14:06:48


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 ?

Ragards
Ulli

-- 
 ________________________________________________________________
|                                                                |
| Ullrich Koethe  Universität Hamburg / University of Hamburg    |
|                 FB Informatik / Dept. of Computer Science      |
|                 AB Kognitive Systeme / Cognitive Systems Group |
|                                                                |
| Phone: +49 (0)40 42883-2573                Vogt-Koelln-Str. 30 |
| Fax:   +49 (0)40 42883-2572                D - 22527 Hamburg   |
| Email: u.koethe_at_[hidden]               Germany             |
|        koethe_at_[hidden]                        |
| WWW:   http://kogs-www.informatik.uni-hamburg.de/~koethe/      |
|________________________________________________________________|

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