Allows convenient access to the slots of a foreign structure.
fli
with-foreign-slots slots-and-options form &body body
slots-and-options ::= (slots &key object-type) | slots
slots ::= (slot-spec*)
slot-spec ::= slot-name | (variable-name slot-name &key copy-foreign-object)
form⇩ |
A form evaluating to an instance of (or a pointer to) a FLI structure. |
body⇩ |
Forms to be executed. |
object-type⇩ |
A FLI structure type. |
slot-name⇩ |
A symbol. |
variable-name⇩ |
A symbol. |
copy-foreign-object⇩ | t , nil or :error . |
The macro with-foreign-slots
is analogous to the Common Lisp macro with-slots. Within body, each slot-name (or variable-name) evaluates to the result of calling foreign-slot-value on form with that slot. setf can be used to set the foreign slot value.
If the first syntax of slots-and-options is used, then object-type is passed as the value of the :object-type
keyword argument in all the generated calls to foreign-slot-value. If the second syntax of slots-and-options is used, no object-type is passed.
Each slot-spec can either be a symbol slot-name naming a slot in the object, which will be also be used in body, or a list of variable-name, a symbol naming a slot, and a plist of options. In this case copy-foreign-object is passed as the value of the :copy-foreign-object
keyword argument in the generated call to foreign-slot-value. The default value of copy-foreign-object is :error
.
The with-foreign-slots
form returns the value of the last form in body.
(fli:define-c-struct abc (a :int) (b :int) (c :int)) => (:STRUCT ABC) (setf abc (fli:allocate-foreign-object :type 'abc)) => #<Pointer to type (:STRUCT ABC) = #x007F3BE0> (fli:with-foreign-slots (a b c) abc (setf a 6 b 7 c (* a b))) => 42 (fli:foreign-slot-value abc 'c) => 42
Foreign Language Interface User Guide and Reference Manual - 01 Dec 2021 19:34:58