extract<> with custom shared pointers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

extract<> with custom shared pointers

Holger Brandsmeier
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.

For completeness I attached a simple example code, but let me explain
here the problem. I have two simple classes A and B, where B is
derived from A.

The C++-functions:

void computeOnA(Teuchos::RCP<A> a)
{
    a->a *= 2;
}
and

void computeOnAs(boost::shared_ptr<A> a)
{
    a->a *= 2;
}
both work nicely from python.

The function

void computeOnTupleOfAs(const tuple& as)
{
  for(int i = 0; i < len(as); ++i) {
    boost::shared_ptr<A> a = extract<boost::shared_ptr<A> >(as[i]);
    computeOnAs(a);
  }
}
also works nicely, given both tuples containing instances of A or the
derived class B as expected.

However the function:
void computeOnTupleOfA(const tuple& as)
{
  for(int i = 0; i < len(as); ++i) {
    Teuchos::RCP<A> a = extract<Teuchos::RCP<A> >(as[i]);
    computeOnA(a);
  }
}
only works given tuples of A and not the derived class B, the
following error is shown:

TypeError: No registered converter was able to produce a C++ rvalue of
type Teuchos::RCP<A> from this Python object of type B.

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)

Thanks for any help,
Holger

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig

rcpPy.cpp (2K) Download Attachment
teuchosRCPPy.h (282 bytes) Download Attachment
test_rcp.py.out (1K) Download Attachment
test_rcp.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: extract<> with custom shared pointers

Jim Bosch-2
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
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig