[C++-sig] Passing Boost wrapped classes (refs and/or pointers) to/from Python and C++

David Abrahams david.abrahams at rcn.com
Mon Jun 10 16:49:22 CEST 2002


----- Original Message -----
From: "Mike Dickheiser" <miked at sinistergames.com>


> Ok. I've wrapped numerous C++ class using Boost,

I presume you're using Boost.Python v1 and not the v2 library described at
the top of http://www.boost.org/libs/python/doc/index.html?

> and I know how to call
> Python functions from C++ and vice versa. But how do I call these
functions
> using a reference or pointer to one of my wrapped classes?
>
> For example, say I have wrapped the C++ class "Vector3" and have the
> following Python script:
>
> def DoSomething(args)
>    x = Vector3()
>    x.set(0.0, 0.0, 0.0)
>
>    y = [a Vector3 passed into args]
>    y.set(0.0, 0.0, 0.0)
>
>
> So, how do I extract 'y' from 'args'?

I don't understand what you're asking for.

> And how would I do it if a Vector3* were passed into args instead?

I don't mean to be rententive about this, but I don't understand the term
"passed into args".
It looks like you've got a python function that takes a single argument. If
the user has passed a Vector3 object as the function's argument, there's
nothing to extract on the Python side.

> For that matter, how do I pack a Vector3 or Vector3* into the args to
begin
> with on the C++ side?

What do you mean "pack"?

> Finally, if I had DoSomething() return a Vector3, how would the C++ code
> that called the function convert the returned PyObject into a Vector3?

If you are invoking DoSomething() from C++, I think what you're looking for
is the Python callback interface, in v1:

    boost::python::callback<ResultType>::call(arg1, arg2, arg3...);

Unfortunately, Boost.Python v1 doesn't give you a way to pass references
and pointers to the callback interface (well, you can pass references, but
the values are copied into new Python objects). Boost.Python v2 does have
that facility. For more details, consult
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/
python/doc/v2/callbacks.html

Good luck,
Dave







More information about the Cplusplus-sig mailing list