<br><br><div class="gmail_quote">On Fri, Oct 26, 2012 at 10:26 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu" target="_blank">mflatt@cs.utah.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
That seems like an ok solution.<br>
<br>
I would be tempted to write<br>
<br>
(define-cstruct _XEvent ([type _int]))<br>
<br>
(define-cstruct (_XAnyEvent _XEvent) ([serial _ulong]<br>
....))<br>
(define-cstruct (_XKeyEvent _XEvent) ([serial _ulong]<br>
....))<br>
....<br>
<br>
Those declarations more directly fit the sub-structuring that is<br>
implemented by the union in "Xlib.h", but it doesn't give you<br>
`XAnyEvent-type', `XKeyEvent-type', etc., and the size of `_XEvent'<br>
isn't the maximum of the variant sizes.<br></blockquote><div><br>Ok, so as Jon pointed out, the last part may be a problem then.<br>And _union is not the right thing to use here apparently.<br><br>Now I think that Jon's solution is actually quite good.<br>
The XNextEvent* procedure does all that is needed (add the correct pointer-tag) for the user, who can then use the corresponding
"sub-struct" on the event.<br>I'm just wondering if it would not be possible to simply add all relevant tags to the any XEvent, e.g.:<br>(define (make-dummy-XEvent)<br> (let ((s (malloc _int (* 24 (ctype-sizeof _long)))))<br>
(memset s 0 24 _long)<br> (cpointer-push-tag! s XEvent-tag)<br> (cpointer-push-tag! s XAnyEvent-tag)<br> (cpointer-push-tag! s XKeypressedEvent-tag)<br> ....<br> s)<br>without caring about the actual kind of event received, and let the user use the event kind it wants. But it's probably not so important now.<br>
<br>Thanks for your answers, I understand it better now.<br><br>Laurent<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
At Fri, 26 Oct 2012 19:00:01 +0200, Laurent wrote:<br>
> Oooohh, I think I understand now: the way it was is already sufficiently<br>
> 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<br>
> 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<br>
> be avoided?<br>
><br>
> Laurent<br>
><br>
> On Fri, Oct 26, 2012 at 6:39 PM, Laurent <<a href="mailto:laurent.orseau@gmail.com">laurent.orseau@gmail.com</a>> wrote:<br>
><br>
> > 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<br>
> > 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<br>
> > _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<br>
> > _union, would it?), so I don't have all the -tag, -pointer, etc. bindings,<br>
> > which I need.<br>
> ><br>
> > What should I do ?<br>
> ><br>
> > Thanks,<br>
> > Laurent<br>
> ><br>
> ><br>
</div></div>> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</blockquote></div><br>