Boost logo

Boost Users :

From: CT (ctrembla_at_[hidden])
Date: 2006-09-18 15:23:38


Hi ,

Can someone can give me some help on how to expose correctly an array
of object pointers using boost.python ?

here is a small example of what i need

i have a class

class A
{
public:
        A(std::string &oName);
        ~A(){}
        const std::string &GetName() const;
}

class B
{
        std::vector<*A> *poNames

public:
        std::vector<*A> GetNames() { return *poNames }

}

assume that there is two instances of A in the container of the object B

now for the boost::python code. I would like to be able to expose class
a and class b and the vector of A*

typedef std::vector<A*> array_proj;
array_proj::iterator begin_array_proj(array_proj &x) {return x.begin();}
array_proj::iterator end_array_proj(array_proj &x) {return x.end();}

BOOST_PYTHON_MODULE(pysession)
{

        class_<A>("object a",init<std::string &)
        .def("GetName",&A::GetName,return_value_policy
                        <copy_const_reference>() )
        ;
        
        class_<B>("object B")
        .def("GetNames", &B::GetNames,
             return_value_policy<manage_new_object>() )
        ;

        class_<std::vector<*A> >("array of pointer to objects A")
        .def("__iter__", range( begin_array_proj, end_array_proj) )
        ;
        
}

Now in python for example I call

#create a new object B
#and iterate over all of the object A

#assume that there are two object of type A in the vector
oNewSession = pysession.B()
oVector = oNewSession.GetNames()
        
for oA in oVector.__iter__():
    print (oA.GetName())

i have this error when i start the python script

    for oA in oVector.__iter__():
TypeError: No to_python (by-value) converter found for C++ type: class
::A *

Does someone can give me some help on how to expose correctly an array
of pointers using boost.python ?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net