unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: rixed@happyleptic.org
To: guile-user@gnu.org
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	[thread overview]
Message-ID: <20100705085635.GB9492@apc> (raw)

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	other threads:[~2010-07-05  8:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-05  8:56 rixed [this message]
2010-07-05  9:57 ` Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100705085635.GB9492@apc \
    --to=rixed@happyleptic.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).