On 02/28/2007 05:24 AM, Tony Sidaway wrote:
> However it occurs to me that I ought to be able to do this using the
> typemap system. Leaving aside the language and implementation
> aspects, is my instinct correct? I've looked briefly at the
> documentation for typemaps but only saw typemaps of data, not function
> Am I on the right track?
The typemap system (or rather, I should say the module source code) is
not really set up to do this...
Typemaps don't work, because typemaps are for building functions that
are designed to be called from chicken... the function accepts C_words,
and uses the typemaps to convert C_words to C++ types, calls the
function, then converts back to C_types and calls C_kontinue.
Python and some other languages have directors, which are similar in
spirit to function pointers, in that they allow control to pass from C++
into the target language. Here, SWIG produces functions (actually
classes) that are designed to be inserted into C++.
Chicken does not currently implement directors, but it is on the TODO
list... Notice, directors are in some sense easier to do than function
pointers, because function pointers you don't have the overridability
that virtual functions have.
Chicken has a more difficult problem here, because chicken functions
never return. Of course, this can be simulated with longjmp (like some
of the chicken wrapper functions do), but this requires a garbage
collection. Not the fastest thing to do. If you notice the currently
generated SWIG functions correctly integrate with the chicken stack, and
do not return or force a garbage collection.
In summary, the options are to write the wrapper functions yourself or
learn enough about SWIG to add directors to chicken (or wait until I get
around to it, probably not an option :)
If you have a large number of callback functions to wrap, you can do
something like where you create a C function that just stores the
parameters in some new C++ class or C structure, and calls a chicken
function. The scheme code can then access the parameters to the
callback through the c++ class (call back into the C code).
This gets you around having to write all the type conversion, since SWIG
does all that for you. You just need one function on the scheme side to
dispatch based on its argument (pointer to a class), call whatever
callback function. Could even automate on the scheme side, so the
callback function walks the list of generic functions (or with macros or
something) to determine the arguments, and then calls the registered
callback with apply and whatever.... Hope that makes sense.