Previous: translate-into-foreign-memory, Up: Foreign Types


with-foreign-slots

Syntax

— Macro: with-foreign-slots (vars ptr type) &body body

Arguments and Values

vars
A list with each element a symbol, or list of length two with the first element :pointer and the second a symbol.
ptr
A foreign pointer to a structure.
type
A structure type.
body
A list of forms to be executed.

Description

The with-foreign-slots macro creates local symbol macros for each var in vars to reference foreign slots in ptr of type. If the var is a list starting with :pointer, it will bind the pointer to the slot (rather than the value). It is similar to Common Lisp's with-slots macro.

Examples

  (defcstruct tm
    (sec :int)
    (min :int)
    (hour :int)
    (mday :int)
    (mon  :int)
    (year :int)
    (wday :int)
    (yday :int)
    (isdst  :boolean)
    (zone   :string)
    (gmtoff :long))
   
  CFFI> (with-foreign-object (time :int)
          (setf (mem-ref time :int)
                (foreign-funcall "time" :pointer (null-pointer) :int))
          (foreign-funcall "gmtime" :pointer time (:pointer (:struct tm))))
  => #<A Mac Pointer #x102A30>
  CFFI> (with-foreign-slots ((sec min hour mday mon year) * (:struct tm))
          (format nil "~A:~A:~A, ~A/~A/~A"
                  hour min sec (+ 1900 year) mon mday))
  => "7:22:47, 2005/8/2"

See Also

defcstruct
defcunion
foreign-slot-value