g-wrap-dev
[Top][All Lists]
Advanced

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

Mapping one Scheme argument to two C arguments


From: Ludovic Courtès
Subject: Mapping one Scheme argument to two C arguments
Date: Thu, 24 Mar 2005 11:38:33 +0100
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux)

Hello,

address@hidden (Ludovic Courtès) writes:

>  /* Read at most AMOUNT bytes from FILE into BUFFER.  Return in *READ
>     the amount of data actually read.  On error, return non-zero.  */
>  errcode_t read_stuff (file_t *file, char *buffer, size_t amount,
>                        size_t *read);

[...]

>   2.  Find a way to make the BUFFER/READ pair translate into a return
>       value of the Scheme function.

So here is what I came up with.  I defined an "input buffer" type, and
then declared only one such argument when wrapping the function:

  (define-class <input-buffer-type> (<gw-type>))

  ...

    (add-type! ws (make <input-buffer-type>
                    #:name '<input-buffer>))

    (wrap-function! ws
                    #:name 'read-stuff!
                    #:c-name "read_stuff"
                    #:returns '<errcode>
                    #:arguments '((<file> file)
                                  (<input-buffer> buffer)
                                  ((int out) read)))

I use SRFI-4 vectors (from Guile 1.7) to represent buffers on the Scheme
side.  Then, the trick consists in having the code generation methods
stealthily generate an additional argument for each argument of type
`<input-buffer>' that is processed:

  (define-method (call-arg-cg (type <chop-input-buffer-type>)
                              (value <gw-value>))
    (let ((size-var (string-append (var value) "_size")))
      (list (var value) ", " size-var)))

Where `size-var' is the name of a previously initialized variable
(e.g. in the corresponding `unwrap-value-cg' method) that contains the
size of the Scheme vector (obtained with, e.g.,
`scm_u8vector_writable_elements ()').

The `call-arg-cg' thing looks somewhat hackish but it does the job.

Thanks,
Ludovic.




reply via email to

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