2.2.3 Structures and unions

The FLI provides the :struct and :union types to interface Lisp objects with the C struct and union types.

To define types to interface with C structures, the FLI macro define-c-struct is provided. In the next example it is used to define an FLI structure, tagpoint :

(fli:define-c-struct tagpoint 
  (x :long) 
  (y :long)
  (visible (:boolean :byte))

This structure would interface with the following C structure:

typedef struct tagPOINT {
    LONG x; 
    LONG y; 
    BYTE visible;

The various elements of a structure are known as slots , and can be accessed using the FLI foreign slot functions, foreign-slot-names, foreign-slot-type, and foreign-slot-value. For example, the next commands set point equal to an instance of tagPOINT , and set the Lisp variable names equal to a list of the names of the slots of tagPOINT .

(setq point (fli:allocate-foreign-object :type 'tagpoint)) (setq names (fli:foreign-slot-names point))

The next command finds the type of the first element in the List names , and sets the variable name-type equal to it.

(setq name-type (fli:foreign-slot-type point (car names)))

Finally, the following command sets point-to equal to a pointer to the first element of point , with the correct type.

(setq point-to (fli:foreign-slot-pointer point (car names) 
                                        :type name-type))

The above example demonstrates some of the functions used to manipulate FLI structures. The FLI :union type is similar to the :struct type, in that the FLI slot functions can be used to access instances of a union. The convenience FLI function define-c-union is also provided for the definition of specific union types.

LispWorks Foreign Language Interface User Guide and Reference Manual - 27 Mar 2005