[C++-sig] custom smart pointers

Roman Yakovenko roman.yakovenko at gmail.com
Wed Sep 6 08:16:33 CEST 2006


On 9/5/06, Alex Mohr <amohr at pixar.com> wrote:
> In my experience, boost python needs a lot of hand holding to work well
> with custom smart pointers.
>
> I think the first thing you need here is an rvalue from-python
> conversion for your smart pointers.

I don't know and understand how I should do it, really :-(.

I took a look on shared_ptr_from_python class. I understand the code
which is written
in construct function.

static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
{

...
new (storage) shared_ptr<T>(
                static_cast<T*>(data->convertible),
                shared_ptr_deleter(handle<>(borrowed(source)))
                );
...
}

Boost.Python creates new instance of shared_ptr with custom deleter. This way it
says to shared_ptr that the object is already managed.

The smart pointer, the project use, does not provide this
functionality. More over,
I don't understand what data->convertible will contain.


> Register a conversion for each
> smart_ptr<T> that you expose to python.  And make sure you do it for
> smart_ptr<T>, not smart_ptr<wrapper<T> > in the case of polymorphic stuff.

I think I do it:

register_ptr_to_python< my_smart_ptr_t< base > >();
register_ptr_to_python< my_smart_ptr_t< derived > >();
implicitly_convertible< my_smart_ptr_t< derived >, my_smart_ptr_t< base > >();

This is what yoy mean, right?


Many thanks for your help.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list