27.11.2014 Views

CFFI User Manual - Common Lisp.net

CFFI User Manual - Common Lisp.net

CFFI User Manual - Common Lisp.net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: An Introduction to Foreign Interfaces and <strong>CFFI</strong> 20<br />

(with-slots (pointer error-buffer c-strings) handle<br />

(curl-easy-cleanup pointer)<br />

(foreign-free error-buffer)<br />

(mapc #’foreign-string-free c-strings)))<br />

(define-foreign-type easy-handle-type ()<br />

()<br />

(:actual-type :pointer)<br />

(:simple-parser easy-handle))<br />

(defmethod translate-to-foreign (handle (type easy-handle-type))<br />

"Extract the pointer from an easy-HANDLE."<br />

(slot-value handle ’pointer))<br />

While we changed some of the <strong>Lisp</strong> functions defined earlier to use CLOS slots rather<br />

than hash tables, the foreign functions work just as well as they did before.<br />

The greatest strength, and the greatest limitation, of the type translator comes from its<br />

generalized interface. As stated previously, we could define all foreign function calls in terms<br />

of the primitive foreign types provided by <strong>CFFI</strong>. The type translator interface allows us to<br />

cleanly specify the relationship between <strong>Lisp</strong> and C data, independent of where it appears<br />

in a function call. This independence comes at a price; for example, it cannot be used to<br />

modify translation semantics based on other arguments to a function call. In these cases,<br />

you should rely on other features of <strong>Lisp</strong>, rather than the powerful, yet domain-specific,<br />

type translator interface.<br />

4.12 What’s next?<br />

<strong>CFFI</strong> provides a rich and powerful foundation for communicating with foreign libraries; as<br />

we have seen, it is up to you to make that experience a pleasantly <strong>Lisp</strong>y one. This tutorial<br />

does not cover all the features of <strong>CFFI</strong>; please see the rest of the manual for details. In<br />

particular, if something seems obviously missing, it is likely that either code or a good<br />

reason for lack of code is already present.<br />

Implementor’s note: There are some other things in <strong>CFFI</strong> that might deserve<br />

tutorial sections, such as free-translated-object, or structs. Let us know which ones<br />

you care about.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!