Next Previous Up Top Contents Index

2.2 Aggregate types

2.2.3 Structures and unions

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

To define types to interface with C structures, the FLI functiondefine-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;
} POINT; 

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 setpoint equal to an instance oftagPOINT, and set the Lisp variablenames equal to a list of the names of the slots oftagPOINT.

(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 Listnames, and sets the variablename-type equal to it.

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

Finally, the following command setspoint-to equal to a pointer to the first element ofpoint, 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 functiondefine-c-union is also provided for the definition of specific union types.


LispWorks Foreign Language Interface - 12 Oct 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker