Previous: translate-to-foreign, Up: Foreign Types


with-foreign-slots

Syntax

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

Arguments and Values

vars
A list of symbols.
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. 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 tm))
  => #<A Mac Pointer #x102A30>
  CFFI> (with-foreign-slots ((sec min hour mday mon year) * 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