[racket] [FFI] _union and define-cstruct

From: Laurent (laurent.orseau at gmail.com)
Date: Fri Oct 26 12:39:27 EDT 2012


I am trying to convert an FFI cstruct definition into a union.
More exactly, the previous definition was :
(define-cstruct _XEvent
    ((type EventType)))

with EventType being defined elsewhere.
But this was an incomplete definition, and I now want to translate the
following C union:
typedef union _XEvent {
        int type;        /* must not be changed; first element */
    XAnyEvent xany;
    XKeyEvent xkey;
    // ... the rest for later
} XEvent;

I searched the collects directory, but I see only a single and too simple
_union use, so I'm not sure how to use it.
I guessed the following:
  (define _XEvent
    (_union EventType ; type
            _XAnyEvent ; xany
            _XKeyEvent ; xkey
            ; etc.
  (define (XEvent-type ev) (union-ref ev 0))
  (define (XEvent-xany ev) (union-ref ev 1))
  (define (XEvent-xkey ev) (union-ref ev 2))

However, now I don't use define-cstruct anymore (it would not work with
_union, would it?), so I don't have all the -tag, -pointer, etc. bindings,
which I need.

What should I do ?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121026/9d1fc5bf/attachment.html>

Posted on the users mailing list.