-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-foreign-slots.lisp
25 lines (23 loc) · 1.01 KB
/
with-foreign-slots.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(in-package :with)
(defmacro with-foreign-slots ((vars ptr type) &body body)
"Create local symbol macros for each var in VARS to reference
foreign slots in PTR of TYPE. Similar to WITH-SLOTS.
Each var can be of the form:
name name bound to slot of same name
(:pointer name) name bound to pointer to slot of same name
(name slot-name) name bound to slot-name
(name :pointer slot-name) name bound to pointer to slot-name"
(let ((ptr-var (gensym "PTR")))
`(let ((,ptr-var ,ptr))
(symbol-macrolet
,(loop :for var :in vars
:collect
(if (listp var)
(let ((p1 (first var)) (p2 (second var)) (p3 (third var)))
(if (eq p1 :pointer)
`(,p2 (foreign-slot-pointer ,ptr-var ',type ',p2))
(if (eq p2 :pointer)
`(,p1 (foreign-slot-pointer ,ptr-var ',type ',p3))
`(,p1 (foreign-slot-value ,ptr-var ',type ',p2)))))
`(,var (foreign-slot-value ,ptr-var ',type ',var))))
,@body))))