mit-scheme-devel
[Top][All Lists]
Advanced

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

[MIT-Scheme-devel] FFI callbacks as struct members


From: Matt Birkholz
Subject: [MIT-Scheme-devel] FFI callbacks as struct members
Date: Thu, 11 May 2017 09:05:39 -0700

> From: Joel Gustafson <address@hidden>
> Date: Thu, 11 May 2017 08:27:52 +0000
> 
> [...]
> Specifically, there's a struct OutStream that has a member
> write_callback that I need to set.

If you are not also trying to set userdata, you are forgetting the
most important part, the ID that Scheme uses to find the Scheme
callback procedure.

> struct OutStream {
> ...
> void *userdata;
> void (*write_callback)(struct OutStream *, int min, int max);
> ...
> }
> 
> In the libsoundio examples it's done directly with
> outstream->write_callback = write_callback;
> How can I declare and set a Scheme callback in a struct like this?

void
register_write_callback (struct OutStream *out, void *userdata)
{
  out->userdata = userdata;
  out->write_callback = write_callback_helper;
}

static void*
write_callback_helper (struct OutStream *out, int min, int max)
{
  Scm_write_callback (out, min, max, out->userdata);
}

(callback (* void) write_callback
          (out (* (struct OutStream)))
          (min int)
          (max int)
          (ID (* void)))

(extern void register_write_callback
        (out (* (struct OutStream)))
        (ID (* void)))

(define (init-outstream out)
  ...
  (C-call "register_write_callback" out (C-callback write-callback))
  ...)

(define (write-callback out min max)
  ...)

De-registering write-callback is left as an exercise...



reply via email to

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