Allows the specification of automatic conversion functions between Lisp and an instance of a FLI type.
keyword
:wrapper foreign-type &key lisp-to-foreign foreign-to-lisp
foreign-type⇩ |
The underlying type to wrap. |
lisp-to-foreign⇩ |
Code specifying how to convert between Lisp and the FLI. |
foreign-to-lisp⇩ |
Code specifying how to convert between the FLI and Lisp. |
The FLI type :wrapper
allows for an extra level of conversion between Lisp and a foreign language through the FLI. With the :wrapper
type you can use lisp-to-foreign and foreign-to-lisp to specify conversion functions from and to an instance of another type foreign-type. Whenever data is passed to the object, or received from the object it is passed through the conversion function. See below for an example of a use of :wrapper
to pass values to an :int as strings, and to receive them back as strings when the pointer to the :int is dereferenced.
In the following example an :int is allocated with a wrapper to allow the :int to be accessed as a string.
(setq wrap (fli:allocate-foreign-object :type '(:wrapper :int :lisp-to-foreign read-from-string :foreign-to-lisp prin1-to-string)))
The object pointed to by wrap
, although consisting of an underlying :int, is set with dereference by passing a string, which is automatically converted using the Lisp function read-from-string. Similarly, when wrap
is dereferenced, the value stored as an :int is converted using prin1-to-string to a Lisp string, which is the returned. The following two commands demonstrate this.
(setf (fli:dereference wrap) "#x100")
(fli:dereference wrap)
The first command sets the value stored at wrap
to be 256 (100 in hex), by passing a string to it. The second command dereferences the value at wrap
, but returns it as a string. The pointer wrap
can be coerced to return the value as an actual :int as follows:
(fli:dereference wrap :type :int)
Foreign Language Interface User Guide and Reference Manual - 01 Dec 2021 19:34:59