[plt-scheme] C++ Bindings using FFI

From: Hans Oesterholt-Dijkema (hdnews at gawab.com)
Date: Mon May 8 15:08:27 EDT 2006

> IIUC, it does not generate the code from scratch -- for example, when
> you write:
>
>   %typemap(in) T * {
>     mzgtk2_catch(SCHEME_LISTP($input),TYPENAME);
>     $1=NULL;
>     {
>       int i,N=scheme_list_length($input);
>       Scheme_Object *obj,*list=$input;
>       for(i=0;i<N;i++) {
>         obj=scheme_car(list);
>         list=scheme_cdr(list);
>         mzgtk2_catch(PREP(obj),TYPENAME);
>         $1=GTKCALL(g_list_append,($1,MZ_C(obj)));
>       }
>     }
>   }
>
> it looks like a bunch of code that it will send to GCC.
>   
True. But note that this little file with 628 lines of type mappings is 
the only one I need to
generate all code that is needed from the 21.000 lines of Gtk+ headers.

I imagine there would also be typemap code needed for the FFI, but maybe 
much simpler.

> (Using SWIG to generate Scheme glue code means that you only use its
> parsing ability.)
>   
No. Not really. You need to write a scheme generator. Weather you write 
a C generator or
a Scheme generator doesn't really matter, or does it?
>> Again. SWIG is only needed for the parsing and generation part. You
>> don't need to distribute SWIG with your code.
>>     
>
> Yes, I'm aware of that.  The point I made is different: say that you
> write an interface for `libfoo', which provides a `foo' int->int
> function -- SWIG knows about this from the header file and will
> generate the Scheme glue code.  Later on I download your code, but I
> have an older version of libfoo, where `foo' is char->char.  (More
> than that -- I might not have the libfoo header files.)
>
>   
Users never need the libfoo header files. Because the swig code has been 
already generated.

So, maybe I'm missing a link here. Does the FFI know from the library, 
how a function is typed?


--Hans



Posted on the users mailing list.