Boost logo

Boost :

Subject: Re: [boost] [python] Expose a struct with a std::vector<std::string> member
From: Stefan Seefeld (seefeld_at_[hidden])
Date: 2009-02-04 11:40:20


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...

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