guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best way to call a user defined hook (written in guile) from C when the


From: rixed
Subject: Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters
Date: Mon, 5 Jul 2010 10:56:36 +0200
User-agent: Mutt/1.5.12-2006-07-14

Hello !

Frequently, our C program computes a set of approximately 50 values that it 
wants
to make accessible for a user defined call back so that the user can choose to
print some of these values (if some condition is met for instance).

I first thought to make some C functions accessible that would return the
values (one C getter per value), and have the C scheme procedure call these
C functions for the values it's interested in. But the problem is that it's
a little verbose (both in C and in scheme due to the required parenthesis).

I then though of binding the values to global variables that the user callback
would use but ruled this out because I was afraid this solution would be slow.

Then I wanted to pass the values as regular parameters, but I don't want to 
impose
the user to define his function with 50 parameters.

So finally we came up with something like this :

The user enter merely a list of the values he wants (in a "configuration" file) 
:

(define user-fields `(foo bar (+ baz foo) (if (> foo bar) 1 2)))

And then in C we evaluate :

(define (hook-helper %s) (lambda () #\t))

where %s is the long list of parameters (foo bar baz...) that's inserted by the 
C program.
And :

(define (hook . args) (local-eval (cons print-user-fields user-fields) 
(procedure-environment (apply hook-helper args))))

So the user entry is minimal and we can, for each set of values, apply the hook
function to the values (which is I think one of the fastest way to make 
available some
a set of named values to scheme).

The downside is : the above scheme line looks like black magic.

Can anyone suggest a better way to do this ?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]