unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters
@ 2010-07-05  8:56 rixed
  2010-07-05  9:57 ` Thien-Thi Nguyen
  2010-07-05 20:18 ` Neil Jerram
  0 siblings, 2 replies; 6+ messages in thread
From: rixed @ 2010-07-05  8:56 UTC (permalink / raw)
  To: guile-user

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 ?





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-07-08 19:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-05  8:56 Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters rixed
2010-07-05  9:57 ` Thien-Thi Nguyen
2010-07-07 14:33   ` Cedric Cellier
2010-07-05 20:18 ` Neil Jerram
2010-07-06 11:22   ` Cedric Cellier
2010-07-08 19:51   ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).