[C++-sig] In a C++ extension, how to use a C++ class exported in another extension

Hua Su suh at mails.tsinghua.edu.cn
Fri May 20 03:05:46 CEST 2005


On Thu, May 19, 2005 at 11:29:17AM -0400, David Abrahams wrote:
> The following message is a courtesy copy of an article
> that has been posted to gmane.comp.python.c++ as well.
> 
> Clark <foo.Clark at gmail.com> writes:
> 
> > 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.
> 
> Well, you can use the raw converter stuff, which is documented in
> http://www.boost.org/libs/python/doc/v2/reference.html#type_conversion.
> You might also see the test code that exercises these facilities in
> libs/python/test/m1.cpp, libs/python/test/m2.cpp, and
> libs/python/test/newtest.py.  
> 
> If SWIG is generating a Python extension type object BaseType, and an
> extension instance type BaseObject corresponding to Base, you could
> use lvalue_from_pytype, something like:
> 
>   lvalue_from_pytype<extract_member<
>         BaseObject, Base, &BaseObject::value>,&BaseType>();
> 
> Otherwise, you'll have to touch some ugly, undocumented details of
> Boost.Python.  Let me know if you need that, and I'll try to help.
>

Thanks very much!
This information is very useful for me. It should be the solution I've
looking for.





> HTH,
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com



More information about the Cplusplus-sig mailing list