[cffi-devel] trying to understand foreign types and translations

James Bielman jamesjb at jamesjb.com
Thu Jul 6 01:03:54 UTC 2006


rif <rif at MIT.EDU> writes:

> I don't quite understand.  fnv-double is NOT a pointer.  It's a CL
> struct that holds two arguments, one of which is the foreign
> pointer.  I see I was misusing translate-to-foreign, so maybe I
> don't need a defctype at all, just an appropriate
> translate-to-foreign?

Take the type translators out of the picture for a minute.  I assume
the C function you want to call looks like:

  double foo (double *array);

So, you could define an interface to "foo" like this:

  (defcfun "foo" :double
    (array :pointer))

And call it like:

  (let ((fnv ...))
    (foo (fnv-foreign-pointer fnv)))

Now, when we want to set up a type translator, we need to introduce a
new foreign type that will "own" the translator so that we don't
override the default behavior.  So instead of the :POINTER argument to
FOO above, we define a typedef for :POINTER that we can attach our
translation behavior to:

  (defctype fnv-pointer :pointer)

  ;; Define how to convert objects of Lisp type FNV-DOUBLE to foreign 
  ;; type FNV-POINTER.
  (defmethod translate-to-foreign ((val fnv-double) (name (eql 'fnv-pointer)))
    (fnv-foreign-pointer val))

  (defcfun "foo" :double
    (array fnv-pointer))

  (let ((fnv ...))
    (foo fnv))  ;; fnv is automatically converted to a pointer

Hopefully this is more clear about the distinction between FNV-DOUBLE
(a Lisp type) and FNV-POINTER (a CFFI foreign type).

James



More information about the cffi-devel mailing list