[C++-sig] Wrappend objects identity problem
David Abrahams
dave at boost-consulting.com
Thu Mar 30 18:38:42 CEST 2006
Victor Nakoryakov <nail-mail at mail.ru> writes:
> Thanks for your reply.
>
> David Abrahams wrote:
>>
>> It sounds like you want a smart pointer that, when converted to Python
>> the first time, manages a Python object. That would make an
>> interesting addition to Boost.Python.
>>
>
> Yes, I think this is what I want :)
>
>
> So... If I have a function in wrapped class 'A' that returns
> shared_ptr<B> where 'B' is another wrapped class then to get always the
> same py address of 'B' I have to write thin wrapper, that will convert
> shared_ptr<B> to python::object and expose that function instead of
> original. Am I right?
No, that won't do it.
Consider:
python::object thin_wrapper()
{
boost::shared_ptr<B> p = create_B();
// No Python object exists
python::object x(p);
// Now x holds a reference to a *NEW* Python object
// holding *p via a copy of p
return x;
}
That's a new Python object **every time thin_wrapper is called**.
Here's something you might use:
template <class T>
struct associate_python_object(boost::shared_ptr<T>& p)
{
// If p isn't managing a Python object already, create one
python::object x(p);
// Replace p with a shared_ptr that manages the Python object
p = python::extract<boost::shared_ptr<T> >(x);
}
HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list