[C++-sig] extract<> with custom shared pointers

Jim Bosch talljimbo at gmail.com
Fri Aug 5 20:34:46 CEST 2011


On 08/05/2011 11:00 AM, Holger Brandsmeier wrote:
> Dear Boost::python experts,
>
> I am trying to use a custom shared pointer type instead of
> boost::shared_pointer, in my case Teuchos::RCP from the Trilinos
> project. The details of Teuchos::RCP should not matter much here. In
> any case there is a Doxygen documentation on the Trilinos webpage.

<snip>

> So my question is, which magic do I need to do (I am willing to use
> certain internals of boost::python) so that B elements can be
> extracted from tuples of Bs.
>
> I currently only provide the functions:
>    T* get_pointer(Teuchos::RCP<T>  const&  p)
>    T* get_pointer(Teuchos::RCP<T>&  p)
>

First, you should try partial-specializing the bp::pointee struct for 
your smart pointer, if you haven't already:

http://www.boost.org/doc/libs/1_46_1/libs/python/doc/v2/pointee.html

If that doesn't work:
When implementing custom template-based conversions (very convenient, 
but definitely deep in the internals), I've found it necessary to 
partial-specialize all of the following template classes in order to get 
all of the features of Boost.Python to work:

bp::to_python_value< T const & >
bp::to_python_value< T & >
bp::converter::arg_to_python< T >
bp::converter::arg_rvalue_from_python< T const & >
bp::converter::extract_rvalue< T >

In your case, some of these are already provided by the custom smart 
pointer support in Boost.Python itself.  I suspect you only need to 
specialize extract_rvalue, or maybe arg_rvalue_from_python to get 
extraction working properly.  I can provide more detail on how to do 
that if you need help, but everything I know I learned from reading the 
source.

Jim Bosch


More information about the Cplusplus-sig mailing list