|
Boost : |
From: John Hunter (jdhunter_at_[hidden])
Date: 2001-12-29 18:28:29
>>>>> "David" == David Abrahams <david.abrahams_at_[hidden]> writes:
David> Hi John, What you want to do is certainly possible. If you
David> know how to verify that a given Python object holds a
David> wrapped wxDateTime, and extract a reference to that
David> wxDateTime object, then you have everything you need. You
David> just need to declare and define the appropriate
David> from_python(PyObject, type<...>) functions
Hmmm.. Somehow the text portion of my last post which explained the
attachment got lost, so here goes again.
To access the SWIG wrapped object, you need
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
extern "C" char * SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type);
Foo from_python(PyObject* p, python::type<const Foo&>) {
Foo *pf;
if (SWIG_GetPtrObj(p, (void **) &pf, "_Foo_p")) {
PyErr_SetString(PyExc_TypeError, "Type error in Bar. Expected a Foo.");
throw python::error_already_set();
}
return *pf;
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
and then link the boost extension code with -lswigpy.
The tar file in the previous post contains the example code
illustrating how to do this.
Here is the README for the tarfile:
MOTIVATION:
The files here show how to use python extensions built with swig and
boost together. There are two simple classes, Foo and Bar. Bar
takes a Foo in it's constructor:
Bar(const Foo& f)
The goal is to take a Foo extension built with SWIG and pass it to a
Bar constructor built with boost::python. The motivation for this
is that some extensive class libraries (such as wx windows) have
python extensions built with SWIG. I would like to use those python
extensions with the extensions I build with boost::python.
In other words, I want to be able to do this:
from swigmod import Foo #Foo was exposed with SWIG
from boostmod import Bar #Bar was exposed with Boost::Python
f = Foo(3)
b = Bar(f)
print b.whats_foo_got() #should print 3
FILES:
Makefile - The flags, required libs, etc...
Foo.h - The Foo class
mixedmod.cpp - The Bar boost::python extension with
from_python for SWIG wrapped objects
swigmod.i - The Foo SWIG extension
test_swigmod.py - Test the SWIG extension
test_mixed.py - Test the SWIG and boost extensions together
USE:
Edit the Makefile to point to your SWIG Makefile.template and your
boost and python distributions.
make swig - Build the SWIG extension
make mixed - Build the boost::python extension
make clean - You know
Each of the test files output '3' if all is well.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk