SWIG and Callbacks

David Abrahams david.abrahams at rcn.com
Sat Jun 15 13:29:55 EDT 2002


"Michael 'Mickey' Lauer" <mickey at tm.informatik.uni-frankfurt.de> wrote in
message news:3d0b4fd6 at nntp.server.uni-frankfurt.de...
> David Abrahams <david.abrahams at rcn.com> wrote:
> > > "Michael 'Mickey' Lauer" <mickey at tm.informatik.uni-frankfurt.de> wrote
in
> > message news:3d0a8b6e at nntp.server.uni-frankfurt.de...
> >
> >
> >> I had a quick look at the other wrappers (SILOON, GRAD, CXX, etc.)
> >> but most look either outdated or not appropriate for this task.
> >> Do you know an (automatic) wrapper generator which has this feature?
> >>
> >> I know sip can do this for c++ libraries - can sip also be used
> >> to wrap plain c-libs without "handwriting" code?
> >
> > On most platforms, Boost.Python would work for you. The determining
issue is
> > whether or not the C++ compiler treats "C" and "C++" linkage function
> > pointers the same way. If the appended program will compile with your
C++
> > compiler, you're probably OK:
>
> The attached program compiles fine here. However, regarding boost.python I
> thought this was more of a c++ library for handcrafting tightly integrated
> bindings rather than an automatic wrapper generator? Is this understanding
wrong?

Automation is a relative term. Every wrapping system requires something more
than just your source code to tell it what to wrap, and how. For example:

    struct X {
        void set(int y) { value = y; }
        int value;
    };

    struct Y {
        X& get_x() { return x; }
        X x;
    };

Now, throw this header at SWIG. What does it do with Y::get_x()? How is the
return value handled? Is the X object copied or does the resulting Python
object refer back to the x member of the containing Y object? It makes a
difference if X::set() is ever called.

With Boost.Python, what you write looks like a kind of IDL (interface
description language), which just happens to be built out of C++ source
code. Lots of jobs (function argument type checking, conversion, arity
checking) are done automatically, but the library doesn't decide which
elements to wrap. Unlike SWIG and a few others, there's no attempt to parse
your source code. Instead, the compile-time introspection capabilities of
C++ are exploited to automatically build wrappers from function and member
function pointers.

-Dave







More information about the Python-list mailing list