[Openmcl-devel] How do I initialize an array of structs? (FFI)

David Steuber david at david-steuber.com
Sat Jan 8 15:46:32 PST 2005


On Jan 8, 2005, at 3:17 AM, David Steuber wrote:

> Give this C fragment:
>
> EventTypeSpec moonSpec[] = {{kEventClassCommand, kEventCommandProcess},
>   {kEventClassWindow, kEventWindowClose}};
>
> How do I initialize moonspec in the following rlet?
>
>   (rlet ((nibref :<ibn>ib<r>ef)
>          (mainspec :<e>vent<t>ype<s>pec
>                    :event<c>lass #$kEventClassCommand
>                    :event<k>ind #$kEventCommandProcess)
>          (moonspec (:array :<e>vent<t>ype<s>pec 2)))
>     ...)
>
> I tried doing it in the rlet until I found this:
>
> " When the type is an array,  initforms  may  not be provided, because 
>  make-record   cannot initialize its values.  make-record   is also 
> unable to initialize fields of a  struct   which are themselves   
> struct   s. The user of   make-record   should set these values  by 
> another means."

Ok, after much hunting through the docs and a couple of aborted 
attempts, I came up with this:

   (rlet ((nibref :<ibn>ib<r>ef)
          (mainspec :<e>vent<t>ype<s>pec
                    :event<c>lass #$kEventClassCommand
                    :event<k>ind #$kEventCommandProcess)
          (moonspec (:array :<e>vent<t>ype<s>pec 2)))
     (let ((offset 0))                                 ; initialize 
moonspec the only way I know how
       (dolist (sfv (list #$kEventClassCommand #$kEventCommandProcess 
#$kEventClassWindow #$kEventWindowClose))
         (setf (ccl::%get-unsigned-long moonspec offset) sfv)
         (incf offset (ccl::record-length :unsigned))))

I had a bit of confusion over two points.  #$Foo becomes OS::|Foo| so 
using a quoted list didn't work like I thought it would, hence LIST.  I 
also thought %GET-UNSIGNED-WORD would be 32 bits, not 16.  Oops.  Both 
facts are documented though, the latter in 9.3.2.1 in the OpenMCL 
documentation.

Is this the best way to do this?




More information about the Openmcl-devel mailing list