Next: , Previous: defcenum, Up: Foreign Types


define-foreign-type

Syntax

— Macro: define-foreign-type type-name lambda-list &body body => type-name

Arguments and Values

type-name
A symbol naming the new foreign type.
lambda-list
A lambda list which is the argument list of the new foreign type.
body
One or more forms that provide a definition of the new foreign type.

Description

The macro define-foreign-type defines a new parameterized type called type-name. Given the arguments specified in lambda-list, executing body should return a type specifier which will determine the behaviour of type-name. The behaviour of parameterized types can be further customized by specializing translate-to-foreign, translate-from-foreign, and free-translated-object.

Unlike defctype, which is used to define simple C-like typedefs, define-foreign-type provides a mechanism for type aliases to take arguments. The following examples illustrate this capability.

Examples

Taken from CFFI's :boolean type definition:

  (define-foreign-type :boolean (&optional (base-type :int))
    "Boolean type. Maps to an :int by default. Only accepts integer types."
    (ecase base-type
      ((:char
        :unsigned-char
        :int
        :unsigned-int
        :long
        :unsigned-long) base-type)))
   
  CFFI> (canonicalize-foreign-type :boolean)
  => :INT
  CFFI> (canonicalize-foreign-type '(:boolean :long))
  => :LONG
  CFFI> (canonicalize-foreign-type '(:boolean :float))
  ;; error--> signalled by ECASE.
  

This next example is hypothetical as there is no :array type yet.

  (define-foreign-type int-array (&rest dimensions)
    `(:array :int ,@dimensions))

See Also

defctype