Oooohh, I think I understand now: the way it was is already sufficiently general.<br>The writer of the initial code took a different direction.<br>Instead of defining a union type, he changes the tag of the pointer depending on what kind of type he needs in the union:<br>
(cpointer-push-tag! e XAnyEvent-tag)<br>(case (XAnyEvent-type e)<br> ((KeyPress) (cpointer-push-tag! e XKeyPressedEvent-tag))<br> ((KeyRelease) (cpointer-push-tag! e XKeyReleasedEvent-tag))<br> ....<br><br>It's pretty smart. Is it the right way to go in general, and should _unions be avoided?<br>
<br>Laurent<br><br><div class="gmail_quote">On Fri, Oct 26, 2012 at 6:39 PM, Laurent <span dir="ltr"><<a href="mailto:laurent.orseau@gmail.com" target="_blank">laurent.orseau@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br><br>I am trying to convert an FFI cstruct definition into a union.<br>More exactly, the previous definition was :<br>(define-cstruct _XEvent<br> ((type EventType)))<br><br>with EventType being defined elsewhere.<br>
But this was an incomplete definition, and I now want to translate the following C union:<br>typedef union _XEvent {<br> int type; /* must not be changed; first element */<br> XAnyEvent xany;<br> XKeyEvent xkey;<br>
// ... the rest for later<br>} XEvent;<br><br>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. <br>I guessed the following:<br> (define _XEvent<br>
(_union EventType ; type<br> _XAnyEvent ; xany<br> _XKeyEvent ; xkey<br> ; etc.<br> ))<br> (define (XEvent-type ev) (union-ref ev 0))<br> (define (XEvent-xany ev) (union-ref ev 1))<br>
(define (XEvent-xkey ev) (union-ref ev 2))<br><br>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.<br>
<br>What should I do ?<br><br>Thanks,<br>Laurent<br><br>
</blockquote></div><br>