[C++-sig] range and iterator

CT ctrembla at pcigeomatics.com
Mon Sep 18 16:38:14 CEST 2006


Hi ,

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


here is a small example of my code

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 ?






More information about the Cplusplus-sig mailing list