[C++-sig] In a C++ extension, how to use a C++ class exported in another extension
Clark
foo.Clark at gmail.com
Mon May 16 03:47:14 CEST 2005
We know that cross-module type info share is easy with latest Swig or
Boost.Python.
But how to make it possible to do with Swig and Boost.Python together?
For example, we have two c++ extension A and B, which are exposed
with Swig and Boost.Python, respectively. They are as follows:
==============Module A wrapped with Swig ===============
class Base
{
public:
Base();
...
};
--------------------------------------------------------
========== Module B wrapped with Boost.Python ==========
void foo(Base *);
class Derive: public Base
{
...
};
--------------------------------------------------------
I wish to use module A and B in Python like this:
============= Module A and B used in Python ============
>>>import A
>>>import B
>>>obj = A.Base()
>>>B.foo(obj)
>>>obj = B.Derive()
>>>B.foo(obj)
--------------------------------------------------------
Could anybody give me a solution or some hints? I've working for
this for long.
On Fri, May 13, 2005 at 06:32:21PM -0700, Ralf W. Grosse-Kunstleve wrote:
> I guess the problem is that wxDC is not wrapped with Boost.Python, but with
> SWIG. Therefore Boost.Python doesn't know how to extract the wxDC instance (or
> a reference to the instance) from the Python object. I believe this can be
Yes, wxPython uses SWIG.
> solved in a nice and general (via a custom from_python converter), but first
> I'd experiment like this (untested):
>
> void
> foo(boost::python::object wxDC_swig_object)
> {
> }
>
> ...
>
> def("foo", foo);
>
> Pass your SWIG-wrapped wxDC instance to foo(). This should work in any case, I
> hope. Once you get this working, develop the body of foo(). I'd look at the
> SWIG generated code for a wxPython function that accepts a wxDC object as an
> argument. SWIG uses PyArg_ParseTuple() to get a PyObject*; in foo() you want to
> use wxDC_swig_object.ptr() to get the same pointer. You have to find out how
> SWIG extracts the wxDC instance given the PyObject* and emulate this in the
> body of foo(). From within foo() you can finally call your function expecting a
> wxDC object.
>
> Let us know if you get this far. The next step would be to rewrite foo() as a
> custom converter. However, it is only worth it if you want to wrap a number of
> functions with wxDC in the argument list. Otherwise the "thin wrapper" approach
> above (foo() is the thin wrapper) is the final solution.
Thanks very much. It's very useful for me. Your solution should be
correct, although I failed to test it for other reasons.
I've tried your approach, but it seems something wrong with my SWIG.
SWIG's manual says I need link my extension with SWIG run time library
(libswigpy.a or libswigpy.so). But in my installation of SWIG there
isn't nether libswigpy.a nor libswigpy.so. I don't know why.
>
> --- Hua Su <suh at mails.tsinghua.edu.cn> wrote:
> > Hi,
> >
> > I'v learnt Boost.Python for two weeks and now encounter some problems :(
> >
> > In brief, I can't find the way to use a C++ class, which is exported in
> > an
> >
> > existing extension, in another extension written by myself.
> >
> > In detail, I'm writing a paint program with wxPython which is an
> >
> > extension for GUI library wxWidgets (wxWindows). I use wxPython to implement
> >
> > GUI, and use C++ to implement paint operations.
> >
> > ( In my application the paint operations is a bottleneck so I have to
> > implement
> >
> > is in C++ )
> >
> > When painting, I need to pass a pointer of DC from Python to my C++ code.
> >
> > The problem is, the class "wxDC" is a real C++ class writen in wxWidgets,
> >
> > and is exported in wxPython as Python class "DC", but I need use it in my
> >
> > code as C++ class "wxDC". I can't find support from Boost.Python for
> >
> > this problem. I have tried to pass pointer "void *" instead, but Boost.Python
> >
> > declares it does not support "void *" as argument or return type.
> >
> > Could someone give me some example? I work for this problem for days :(
> >
> > Thanks for your reply.
> >
> > regards,
> > Clark
>
>
>
>
> __________________________________
> Yahoo! Mail Mobile
> Take Yahoo! Mail with you! Check email on your mobile phone.
> http://mobile.yahoo.com/learn/mail
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
More information about the Cplusplus-sig
mailing list