Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Python and vectors of shared_ptr

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

like image 885
Megatron Avatar asked May 07 '11 04:05

Megatron


1 Answers

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!

like image 187
xuming Avatar answered Nov 02 '22 15:11

xuming