[Boost-users] Wrapping a nested c++ container to python

255 views
Skip to first unread message

RR

unread,
Apr 26, 2008, 10:46:26 PM4/26/08
to boost...@lists.boost.org
Hello I have some trouble with handling c++-container.

I have these code:

typedef struct foo {
   typedef std::vector<double> pt_coords;
   typedef std::vector<pt_coords> pt_list;
};

foo return_foo();

Now I want to wrap these, but I don't know how! I have test this, but I think it's wrong:

BOOST_PYTHON_MODULE(...) {
   implicitly_convertible<foo::pt_coords,tuple>();
   implicitly_convertible<foo::pt_list,list>();
}

By using this in Python I get:

TypeError: No Python class registered for C++ class std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >

How to do this right?

Greetings

Bruno Lalande

unread,
Apr 28, 2008, 4:26:45 AM4/28/08
to boost...@lists.boost.org
Hi,

You can use the provided vector indexing suite. If I remember well,
all you have to do is including the header and defining a vector class
for each type of vector you want to use:

#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

class_<std::vector<pt_coords> >("coords_vector")
.def(vector_indexing_suite<std::vector<pt_coords> >())
;

Your vector will be a known class once in Python, and you'll be able
to use it like a Python list.

Bruno
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

RR

unread,
Apr 28, 2008, 1:19:58 PM4/28/08
to boost...@lists.boost.org
Thanks,

but the wrapped classes has not the same behavior as python list, but its a solution ...

Greetings

Bruno Lalande

unread,
Apr 29, 2008, 4:45:43 AM4/29/08
to boost...@lists.boost.org
Hi,

> but the wrapped classes has not the same behavior as python list, but its a
> solution ...

You can get a real Python list out of this class by doing this in Python :

real_python_list = list(wrapped_vector)

Reply all
Reply to author
Forward
0 new messages