[C++-sig] Re: creating an instance of a class_<> object in c++ and exporting it to python
Francois Ostiguy
ostiguy at fnal.gov
Mon Aug 16 19:21:38 CEST 2004
Thank You Dave, Max and Chez for your insights.
I am getting closer to my goal, but it is not quite working yet.
In the process I am slowly gaining more understanding of the way
boost.python works. I have to say that I never cease to be amazed
at how carefully crafted boost.python is.
It is truly a remarkable piece of work.
Back to my problem:
Last Friday, following Max and Dave's suggestions I tried (in simplified
form):
class A;
class_<A, A*, boost::noncopyable> PythonTypeA;
A* get_existing() { return aptr; }
...
A* aptr = new A();
object py_get_existing =
make_function( &get_existing,
return_value_policy<reference_existing_object>() );
object PythonInstanceOfA = PythonTypeA( py_get_existing() );
...
Another poster pointed out that make_function is a factory for python
objects and therefore the last line was incorrect. Dave added that it was
"bogus".
When I remove this last line, everything compiles properly, but of
course when I print the "py_get_existing" object in python I get
<Boost.Python.function object at 0x834cb28>;
which is not a wrapped class A object.
After thinking about this a bit, it occured to me that what I needed was
more along the lines of
object PythonInstanceOfA = PythonTypeA( aptr ); ///
If I understand correctly, this should instantiate an object using
the automatic converter defined by the extended python type
class_<A, A*, boost::noncopyable> PythonTypeA.
The code compiles without a hitch, but I get a core dump
when the statement is executed. I am still
investigating what might be going on (*** see note below ***) ).
The approach suggested by Chez (chezdique AT yahoo.com) appears more
promising. I have yet to try his code, but a cursory look gives me the
impression that what he is doing is not fundamentally not different from
what I just described. He defines an an empty ObjectWrapper<T> and a
correcponding get_ptr() function. Once again, correct me if I am wrong,
but this appers superflous since T& and T* converters should automatically
be generated by boost.python. An ObjectWrapper class would be useful to
support some kind of smart pointers.
I apologize for the lengthy post. I hope this is not too confusing
and I would greatly appreciate more comments/feedback.
-Francois
----------------------------------------------------------------------------
Dr. Jean-Francois OSTIGUY voice: (630) 840-2231
Beam Physics Dept MS220 FAX: (630) 840-6039
Fermi National Accelerator Laboratory email: ostiguy at fnal.gov
Batavia IL 60510-0500 WWW:www-ap.fnal.gov/~ostiguy
*** note: one complication is that I want to keep all the
extended python type definitions together. This means that somehow the
PythonTypeA object has to be global. Since there are typically
a large number of types declared in each modules, the simplest
thing seemed to use a global
class_<A, A*, boost::noncopyable>* PythonTypeAPtr
and set PythonTypeAPtr = &PythonTypeA.
More information about the Cplusplus-sig
mailing list