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

Holger Brandsmeier brandsmeier at gmx.de
Fri Aug 5 20:00:00 CEST 2011


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rcpPy.cpp
Type: text/x-c++src
Size: 2114 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110805/fafe3a69/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: teuchosRCPPy.h
Type: text/x-chdr
Size: 205 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110805/fafe3a69/attachment.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_rcp.py.out
Type: application/octet-stream
Size: 1116 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110805/fafe3a69/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_rcp.py
Type: text/x-python
Size: 754 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110805/fafe3a69/attachment.py>


More information about the Cplusplus-sig mailing list