[C++-sig] Wrappend objects identity problem

Victor Nakoryakov nail-mail at mail.ru
Thu Mar 30 13:52:31 CEST 2006


Roman Yakovenko wrote:
>>Thanks for reply. Here is a quote from FAQ:
>>
>>Another approach to this requires you to change your C++ code a bit; if
>>that's an option for you it might be a better way to go. work we've been
>>meaning to get to anyway. When a shared_ptr<X> is converted from Python,
>>the shared_ptr actually manages a reference to the containing Python
>>object. When a shared_ptr<X> is converted back to Python, the library
>>checks to see if it's one of those "Python object managers" and if so
>>just returns the original Python object. *So you could just write
>>object(p) to get the Python object back*. To exploit this you'd have to
>>be able to change the C++ code you're wrapping so that it deals with
>>shared_ptr instead of raw pointers.
>>
>>This is suitable approach for me, but phrase marked bold (between **)
>>isn't clear for me: where to write "object(p)" and what is 'p' in this
>>context?
> 
> 
> I could be wrong, but p is instance of shared_ptr and object is
> boost::python::object.
> 
> 

I thought this way too, but the following code:

> class A
> {
> };
> 
> boost::shared_ptr<A> ptr;
> python::object getA()
> {
> 	if (!ptr)
> 		ptr = boost::shared_ptr<A>(new A);
> 
> 	return python::object(ptr);
> }
> 
> void Init()
> {
> 	python::class_< A, boost::shared_ptr<A>, boost::noncopyable >( "AClass", python::no_init );
> 	python::def("getA", &getA);
> }

produces absolutely the same result :(

-- 
Victor (aka nail) Nakoryakov
nail-mail<at>mail<dot>ru

Krasnoznamensk, Moscow, Russia




More information about the Cplusplus-sig mailing list