You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 6: Foreign Types 28<br />
struct person {<br />
int number;<br />
char* reason;<br />
};<br />
The equivalent defcstruct form follows:<br />
(defcstruct person<br />
(number :int)<br />
(reason :string))<br />
By default, [convert-from-foreign], page 29 (and also [mem-ref], page 68) will make a<br />
plist with slot names as keys, and [convert-to-foreign], page 30 will translate such a plist<br />
to a foreign structure. A user wishing to define other translations should use the :class<br />
argument to [defcstruct], page 33, and then define methods for [translate-from-foreign],<br />
page 52 and [translate-into-foreign-memory], page 54 that specialize on this class, possibly<br />
calling call-next-method to translate from and to the plists rather than provide a direct<br />
interface to the foreign object. The macro translation-forms-for-class will generate<br />
the forms necessary to translate a <strong>Lisp</strong> class into a foreign structure and vice versa.<br />
Please note that this interface is only for those that must know about the values contained<br />
in a relevant struct. If the library you are interfacing returns an opaque pointer that needs<br />
only be passed to other C library functions, by all means just use :pointer or a typesafe<br />
definition munged together with defctype and type translation. To pass or return<br />
a structure by value to a function, load the cffi-libffi system and specify the structure as<br />
(:struct structure-name). To pass or return the pointer, you can use either :pointer<br />
or (:pointer (:struct structure-name)).<br />
Compatibility note: Previous versions of <strong>CFFI</strong> accepted the “bare” structure-name as a<br />
type specification, which was interpreted as a pointer to the structure. This is deprecated<br />
and produces a style warning. Using this deprecated form means that [mem-aref], page 67<br />
retains its prior meaning and returns a pointer. Using the (:struct structure-name)<br />
form for the type, [mem-aref], page 67 provides a <strong>Lisp</strong> object translated from the structure<br />
(by default a plist). Thus the semantics are consistent with all types in returning the<br />
object as represented in <strong>Lisp</strong>, and not a pointer, with the exception of the “bare” structure<br />
compatibility retained. In order to obtain the pointer, you should use the function [memaptr],<br />
page 66.<br />
See [defcstruct], page 33 for more details.<br />
6.7 Allocating Foreign Objects<br />
See Section 7.2 [Allocating Foreign Memory], page 57.