[C++-sig] boost::python and custom smart pointer

Roman Yakovenko roman.yakovenko at gmail.com
Fri Mar 2 19:25:22 CET 2007


On 3/2/07, Gabriel Becedillas <gabriel.becedillas at corest.com> wrote:
>
> I also forgot to say that upcasts aren't working either.
> If I return a MySmartPtr<Base> and the concrete type is Derived,
> boost::python let me use the Derived interface (which is great!), but
> when this instance has to be passed back to C++ it'll only match an
> exact MySmartPtr<T> type. It won't do neither an upcast nor a downcast.

Upcast is solvable, look documentation for implicitly_convertible.
You also can take a look on complete example of exposing custom smart
pointer here:
http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html

> After checking shared_ptr_from_python a litte bit I realize that
> convertible(...) is returning a raw T* to the object (when the passed
> PyObject is not Py_None) and that construct(...) is creating a new
> boost::shared_ptr from the raw T*.
> converter::get_lvalue_from_python(p, registered<T>::converters) is used
> to extract the raw T* from the PyObject. This won't work for me because
> I need to extract the whole MySmartPtr<T>*, not just the raw T*, in
> order to copy the MySmartPtr<T> in construct(...).
>
> Is there a way to do that ? Please keep in mind that I need to check
> both for upcasts and downcasts. My PyObject could hold a
> MySmartPtr<Derived> instance and I'd like to pass it to C++ as a
> MySmartPtr<Base>, and the other way around (previously checking if a
> downcast is possible).
> I really need to make MySmartPtr<T> to behave as boost::shared_ptr<T>
> regarding conversions.

There is only one person who can help you here: Dave Abrahams.

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



More information about the Cplusplus-sig mailing list