[racket] [FFI] _union and define-cstruct

From: Laurent (laurent.orseau at gmail.com)
Date: Fri Oct 26 13:00:01 EDT 2012

Oooohh, I think I understand now: the way it was is already sufficiently
The writer of the initial code took a different direction.
Instead of defining a union type, he changes the tag of the pointer
depending on what kind of type he needs in the union:
(cpointer-push-tag! e XAnyEvent-tag)
(case (XAnyEvent-type e)
    ((KeyPress) (cpointer-push-tag! e XKeyPressedEvent-tag))
    ((KeyRelease) (cpointer-push-tag! e XKeyReleasedEvent-tag))

It's pretty smart. Is it the right way to go in general, and should _unions
be avoided?


On Fri, Oct 26, 2012 at 6:39 PM, Laurent <laurent.orseau at gmail.com> wrote:

> Hi,
> 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 ?
> Thanks,
> Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121026/64c4c48d/attachment.html>

Posted on the users mailing list.