[plt-scheme] C++ Bindings using FFI

From: Eli Barzilay (eli at barzilay.org)
Date: Tue May 9 18:46:39 EDT 2006

On May  9, Hans Oesterholt-Dijkema wrote:
> Eli Barzilay schreef:
> >
> > If you can get the type information in some readable format
> > (readable without a C-parser), then it should be easy to do the
> > same with Scheme, maybe easier.
> Ahum. I'd personally use SWIG to generate the type information in
> 'some readable format'.

Yes, we're going in circles here -- I already said that parsing header
file is a good feature.

> > My personal conclusion about this is that for some strange reason
> > people completely miss the use of the Scheme interface.  It's a
> > different mode of thinking, and you should just try it out to see
> > the differences.
> > 
> I actually like the idea of using the scheme interface. I just don't
> want to code the scheme FFI code for 12.000 functions and a couple
> of hundred enumeration types.

If you use swig to generate such a readable description file, you
could use it for this too.

> Also I have questions. How would I resolve the memory management
> differences between scheme and Gtk (see
> http://www.elemental-programming.org/mzgtk2-mzgtk2-memory-management.html).

I don't know of any gtk memory management facilities, and looking at
your page the problem is the usual who-will-free-this-object.  If the
whole interface is in Scheme, then the easiest thing is to register a
finalizer that will delete the C object -- the whole thing can be
easily automated (for example, see `defmagick-pointer-type' in the
magick interface).

It may be arbitrarily complicated, of course, if you have sharing
between GTK objects without going through a Scheme object.  This is,
BTW, one point where header files don't have enough information, and
neither SWIG nor any other tool can save you -- you need to
distinguish constructors, from functions that return references to
existing objects.

> How would I let callbacks work?

You send a Scheme function.  (You just need to hold a reference to it
in Scheme so the GC won't take it away.)

> How would I make property setting work with GtkValue types?  How
> would I be able to use the GObject introspection facilities? How
> would I code with use of the FFI a specific Gtk list-model (that
> requires a new 'GObject derived class') to be able to use the
> functional way mzscheme to fill up tables?

I don't know about these aspects of GTK.  There was a GTK interface
project that dealt with some of these.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.