I've read how to expose normal vectors to python in boost python, but I want to know how to expose and make use of a vector. For instance, I have a vector of shared_ptrs as follows:
std::vector<shared_ptr<StatusEffect> > Effects;
Based on the material for exposing vectors, I should be able to expose this type of class. What I want to know is how can I actually add to it? How do I create instances of shared_ptr<StatusEffect>
since I don't have access to new, and the shared_ptr can point to multiple derived types making adding a static creation method to each class a little tedious.
Does anyone have some pointers or can suggest how to do this? Finding good example for boost::python for what I want to do has been abit tricky
Thanks in advance
struct A {
int index;
static shared_ptr<A> create () { return shared_ptr<A>(new A); }
std::string hello () { return "Just nod if you can hear me!"; }
};
BOOST_PYTHON_MODULE(my_shared_ptr)
{
class_<A, shared_ptr<A> >("A",boost::python::init<>())
.def("create",&A::create )
.staticmethod("create")
.def("hello",&A::hello)
.def_readonly("index",&A::index)
;
boost::python::class_<std::vector<shared_ptr<A>>>("PyVec")
.def(boost::python::vector_indexing_suite<std::vector<shared_ptr< A>>,true >());
}
The argument of ",true" is important!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With