[C++-sig] Type conversion problem -- simplified

David Abrahams dave at boost-consulting.com
Sat Nov 4 22:08:27 CET 2006


Randall Hopper <viznut at charter.net> writes:

>      I've been digging into this and can summarize the problem more
> succinctly now (I was barking up the wrong tree before).
>
>      What do I need to do to tell boost::python to dereference a wrapped
> C++ pointer 

wrapped how?

> before trying to invoke the C++ class instance's methods?

Normally -- if you've exposed a C++ class X to Python with 

  class_<X, ...>("PyX") 

-- when you ask Boost.Python to convert an X* to Python, it complains
because it doesn't know whether and how to manage the lifetime of the
referenced X object.  Normally one uses a CallPolicy
(http://tinyurl.com/yjt72m#python.call_policies) to describe how the
pointer should be handled.  Specifically, it's usually an
return_value_policy<R> where R is a ResultConverterGenerator
(http://tinyurl.com/ydmvuf#result_converter_generators) such as
manage_new_object.

Once you've succeeded in making the conversion, what you end up with
on the Python side is just a PyX object that happens to get to the X
instance through that pointer, and operations on the converted X* work
just the same as if you had converted an X object directly to Python.

>      I've got a method returning a pointer to a C++ class instance, and
> right now I can't use it as a callable because boost::python treats it like
> a pointer.

I don't think that's the problem.

>
> Traceback (most recent call last):
>   ...
>   File "osg_to_sgg.py", line 153, in setupGeode
>     v[0].set( 0, 1, 2 )
> Boost.Python.ArgumentError: Python argument types in
>     Vec3Array.__getitem__(Vec3Array, int)

__getitem__ refers to the square brackets operator, so this has
nothing to do with callability of the Vec3Array object.  

> did not match C++ signature:
>     __getitem__(osg::TemplateArray<osg::Vec3f, (osg::Array::Type)10, 3, 5126>*, int)
> Note that the Vec3Array type is identical to the osg::TemplateArray<...>
> type (via typedef), so the key difference is the extra "*" in the last
> line.

Interesting; how did you generate that __getitem__ method?  Its first
parameter should be a reference to a Vec3Array, not a pointer.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list