[cffi-devel] Branch: New Type Translator Interface
James Bielman
jamesjb at jamesjb.com
Fri Dec 23 20:58:33 UTC 2005
Hi all,
I've done yet-another rewrite of the type translator interface. It does
everything through generic functions at run-time, which allows us to
specialize on both the Lisp object being converted, and the foreign type
we are converting to.
DEFINE-TYPE-TRANSLATOR is gone---instead you simply specialize the
generic functions as needed. For example (taken from
examples/gettimeofday.lisp):
;; A NULL-POINTER is a :POINTER that must always be NULL.
(defctype null-pointer :pointer)
;; Translator that ensures VALUE is a null pointer. NIL is
;; also converted to a null pointer for convenience.
(defmethod translate-to-foreign (value type (name (eql 'null-pointer)))
(declare (ignore type name))
(cond
((null value) (null-pointer))
((null-pointer-p value) value)
(t (error "~A is not a null pointer." value))))
This method on TRANSLATE-TO-FOREIGN replaces the :TO-C case of
DEFINE-TYPE-TRANSLATOR. Specializing the other GFs is similar---I've
also added a FREE-TRANSLATED-OBJECT generic function.
There isn't yet a mechanism for optimizing the case where we are
converting a Lisp object to a C object with dynamic extent---I will add
this next, with some sort of compiler-macro-like interface.
The branch can be downloaded via:
darcs get http://slacknet.com/~jamesjb/cffi-new-translators
Please give it a spin and let me know what you think! Documentation
updates coming soon...
James
More information about the cffi-devel
mailing list