[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Type conversion in the FFI
From: |
felix |
Subject: |
Re: [Chicken-users] Type conversion in the FFI |
Date: |
Tue, 13 Apr 2004 13:16:33 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113 |
Daniel B. Faken wrote:
Hello all,
I have C functions which return a boolean value of type (unsigned char).
How can I get chicken to convert these to/from its own boolean values. I
tried the __declare(type, ..) operation, but that tries to redefine my
C boolean type ("BOOL") as an int: "#define BOOL int" is inserted into the
file. This changes the C semantics, which I would like to avoid.. (e.g.
an array of these values wouldn't be properly conveted).
I also tried (define-foreign-type), but couldn't get it to work either.
At present I am just using (integer->char 1) or (not (eq? (char->integer
(C-fn) 0)), but its kinda clumsy..
Here is some code that should work:
; Converters:
(define (uchar->my-bool x) (not (eq? 0 (char->integer x))))
(define (my-bool->uchar x) (integer->char (if x 1 0)))
; Define a foreign type that uses the converters:
(define-foreign-type my-bool unsigned-char my-bool->uchar uchar->my-bool)
; Just for trying it out:
#>
unsigned char foo(unsigned char x) { return !x; }
<#
(define foo
(foreign-lambda my-bool "foo" my-bool) )
(print (foo #t))
(print (foo #f))
On an unrelated note, where can I find out more about "properties", as
mentioned in the hash-table docs? These seem to be a common feature of
LISP/scheme, but I couldn't find anything in R5RS..
R5RS doesn't have anything like LISPs property lists. Chicken has something
similar to CLs "disembodied" property lists, that is, property lists that are
not attached to symbols (as in LISP).
Essentially, those p-lists are just hashtables containing association lists.
cheers,
felix