[C++-sig] Exposing std::list<> via Boost.Python

Jim Bosch talljimbo at gmail.com
Wed Jun 23 03:46:25 CEST 2010


On 06/21/2010 07:01 AM, Phil Bass wrote:
> We have a lot of C++ code using a home-grown Event<T> class template. An
> Event<T> is just a list of pointers to callback function objects. More
> precisely, an Event<T> is a std::list<unary_function<T>*>, where
> unary_function is a polymorphic function object class. Code that
> generates an event iterates through the list, dereferences each pointer
> and calls the corresponding function.
>
> I'd like python code to be able to generate these C++ events. To do that
> Event<>s need to support Python's iterable interface, which requires
> random access to the container.Unfortunately, std::list<> doesn't
> support random access. Short of copying the contents of every
> std::list<> to a std::vector<> for Python's benefit I don't see how this
> can be accomplished.
>
> Does anyone have any ideas for a simpler, more natural implementation?
>

You don't need random access to make a python iterable at all - Python's 
built-in dict and set types are examples.  All you need to do is define 
an __iter__ special function, and you should be able to do that fairly 
automatically using the stuff described here:

http://www.boost.org/doc/libs/1_43_0/libs/python/doc/v2/iterator.html



Jim Bosch


More information about the Cplusplus-sig mailing list