[python] Expose a struct with a std::vector<std::string> member

Hello all, I am new to boost::python. I have access to a C++ library which I want to expose to python using boost. One of the structs I want to expose is something like: #include <vector> struct ExampleStruct { std::vector<std::string> elements; }; I would like to expose this class but I like to avoid having to write a wrapper that would inefficiently have to copy the vector contents to a boost::python::list member. Is there a way to do this? Thank you very much in advance Cheers Tiago

Tiago Coutinho wrote:
Hello all,
I am new to boost::python. I have access to a C++ library which I want to expose to python using boost. One of the structs I want to expose is something like:
#include <vector> struct ExampleStruct { std::vector<std::string> elements; };
I would like to expose this class but I like to avoid having to write a wrapper that would inefficiently have to copy the vector contents to a boost::python::list member. Is there a way to do this?
You may use the indexing-suite to expose the above as a container (with iterators etc.). However, note that strings in Python are immutable, so you can't modify them in-place. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Could you give me an example? I tried something like: #include <vector> #include <string> #include <boost/python.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp> struct ExampleStruct { std::vector<std::string> elements; }; class_<A>("A", init<>()) .def_readonly("elements", vector_indexing_suite<std::vector<std::string> >()) ; Of course the above code is wrong. My question is how should I initialize the vector_indexing_suite. Thank you very much in advance Cheers Tiago On Tue, Feb 3, 2009 at 6:31 PM, Stefan Seefeld <seefeld@sympatico.ca> wrote:
Tiago Coutinho wrote:
Hello all,
I am new to boost::python. I have access to a C++ library which I want to expose to python using boost. One of the structs I want to expose is something like:
#include <vector> struct ExampleStruct { std::vector<std::string> elements; };
I would like to expose this class but I like to avoid having to write a wrapper that would inefficiently have to copy the vector contents to a boost::python::list member. Is there a way to do this?
You may use the indexing-suite to expose the above as a container (with iterators etc.). However, note that strings in Python are immutable, so you can't modify them in-place.
Regards, Stefan
--
...ich hab' noch einen Koffer in Berlin...
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Tiago Coutinho wrote:
Could you give me an example?
I tried something like:
#include <vector> #include <string> #include <boost/python.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
struct ExampleStruct { std::vector<std::string> elements; };
class_<A>("A", init<>()) .def_readonly("elements", vector_indexing_suite<std::vector<std::string> >()) ;
Of course the above code is wrong. My question is how should I initialize the vector_indexing_suite.
See http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/indexing.html: As you want to work with vector<string>, you have to expose that first: typedef std::vector<std::string> StringVector; class_<StringVector> sv("StringVector"); sv.def(vector_indexing_suite<StringVector>()); Now you can expose your ExampleStruct: class_<ExampleStruct> es("ExampleStruct"); es.def_readonly("elements", &ExampleStruct::elements); HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Thank you very much. It solved my problem elegantly I would say :-) On Wed, Feb 4, 2009 at 5:40 PM, Stefan Seefeld <seefeld@sympatico.ca> wrote:
Tiago Coutinho wrote:
Could you give me an example?
I tried something like:
#include <vector> #include <string> #include <boost/python.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
struct ExampleStruct { std::vector<std::string> elements; };
class_<A>("A", init<>()) .def_readonly("elements", vector_indexing_suite<std::vector<std::string> >()) ;
Of course the above code is wrong. My question is how should I initialize the vector_indexing_suite.
See http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/indexing.html:
As you want to work with vector<string>, you have to expose that first:
typedef std::vector<std::string> StringVector; class_<StringVector> sv("StringVector"); sv.def(vector_indexing_suite<StringVector>());
Now you can expose your ExampleStruct:
class_<ExampleStruct> es("ExampleStruct"); es.def_readonly("elements", &ExampleStruct::elements);
HTH, Stefan
--
...ich hab' noch einen Koffer in Berlin...
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (2)
-
Stefan Seefeld
-
Tiago Coutinho