3 (define-foreign-type g-boxed-foreign-type
()
5 :accessor g-boxed-foreign-info
6 :initform
(error "info must be specified"))
7 (return-p :initarg
:return-p
8 :accessor g-boxed-foreign-return-p
10 (:actual-type
:pointer
))
12 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
13 (defstruct g-boxed-info
17 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
18 (defun get-g-boxed-foreign-info (name)
19 (get name
'g-boxed-foreign-info
)))
21 (defvar *g-type-name-
>g-boxed-foreign-info
* (make-hash-table :test
'equal
))
23 (defun get-g-boxed-foreign-info-for-gtype (g-type-designator)
24 (or (gethash (gtype-name (gtype g-type-designator
)) *g-type-name-
>g-boxed-foreign-info
*)
25 (error "Unknown GBoxed type '~A'" (gtype-name (gtype g-type-designator
)))))
27 (defgeneric make-foreign-type
(info &key return-p
))
29 (define-parse-method g-boxed-foreign
(name &rest options
)
30 (let ((info (get-g-boxed-foreign-info name
)))
31 (assert info nil
"Unknown foreign GBoxed type ~A" name
)
32 (make-foreign-type info
:return-p
(member :return options
))))
34 (defgeneric boxed-copy-fn
(type-info native
)
35 (:method
(type-info native
)
36 (g-boxed-copy (g-boxed-info-g-type type-info
) native
)))
38 #+nil
(defmethod boxed-copy-fn :before
(type-info native
)
39 (format t
"(boxed-copy-fn ~A ~A)~%" (g-boxed-info-name type-info
) native
))
41 (defgeneric boxed-free-fn
(type-info native
)
42 (:method
(type-info native
)
43 (g-boxed-free (g-boxed-info-g-type type-info
) native
)))
45 #+nil
(defmethod boxed-free-fn :before
(type-info native
)
46 (format t
"(boxed-free-fn ~A ~A)~%" (g-boxed-info-name type-info
) native
))
48 (defgeneric has-callback-cleanup
(foreign-type))
49 (defgeneric cleanup-translated-object-for-callback
(foreign-type converted-object native-object
))
51 (defmethod has-callback-cleanup ((type g-boxed-foreign-type
))
54 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
55 (defstruct (g-boxed-cstruct-wrapper-info (:include g-boxed-info
))
58 (defclass boxed-cstruct-foreign-type
(g-boxed-foreign-type) ())
60 (defstruct cstruct-slot-description
67 (defstruct (cstruct-inline-slot-description (:include cstruct-slot-description
))
70 (defmethod make-load-form ((object cstruct-slot-description
) &optional environment
)
71 (make-load-form-saving-slots object
:environment environment
))
73 (defmethod make-load-form ((object cstruct-inline-slot-description
) &optional environment
)
74 (make-load-form-saving-slots object
:environment environment
))
76 (defstruct cstruct-description
80 (defmethod make-load-form ((object cstruct-description
) &optional environment
)
81 (make-load-form-saving-slots object
:environment environment
))
83 (defun parse-cstruct-slot (slot)
84 (destructuring-bind (name type
&key count initform inline
) slot
86 (make-cstruct-inline-slot-description :name name
:type
(generated-cunion-name type
)
87 :count count
:initform initform
:inline-p inline
88 :boxed-type-name type
)
89 (make-cstruct-inline-slot-description :name name
:type type
90 :count count
:initform initform
:inline-p inline
))))
92 (defun parse-cstruct-definition (name slots
)
93 (make-cstruct-description :name name
94 :slots
(mapcar #'parse-cstruct-slot slots
)))
96 (defmacro define-g-boxed-cstruct
(name g-type-name
&body slots
)
97 (let ((cstruct-description (parse-cstruct-definition name slots
)))
100 ,@(iter (for slot in
(cstruct-description-slots cstruct-description
))
101 (for name
= (cstruct-slot-description-name slot
))
102 (for initform
= (cstruct-slot-description-initform slot
))
103 (collect (list name initform
))))
104 (defcstruct ,(generated-cstruct-name name
)
105 ,@(iter (for slot in
(cstruct-description-slots cstruct-description
))
106 (for name
= (cstruct-slot-description-name slot
))
107 (for type
= (cstruct-slot-description-type slot
))
108 (for count
= (cstruct-slot-description-count slot
))
109 (collect `(,name
,type
,@(when count
`(:count
,count
))))))
110 (defcunion ,(generated-cunion-name name
)
111 (,name
,(generated-cstruct-name name
)))
112 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
113 (setf (get ',name
'g-boxed-foreign-info
)
114 (make-g-boxed-cstruct-wrapper-info :name
',name
116 :cstruct-description
,cstruct-description
)
117 (gethash ,g-type-name
*g-type-name-
>g-boxed-foreign-info
*)
118 (get ',name
'g-boxed-foreign-info
)
119 (get ',name
'structure-constructor
)
120 ',(intern (format nil
"MAKE-~A" (symbol-name name
)) (symbol-package name
)))))))
122 (defmethod make-foreign-type ((info g-boxed-cstruct-wrapper-info
) &key return-p
)
123 (make-instance 'boxed-cstruct-foreign-type
:info info
:return-p return-p
))
125 (defun memcpy (target source bytes
)
126 (iter (for i from
0 below bytes
)
127 (setf (mem-aref target
:uchar i
)
128 (mem-aref source
:uchar i
))))
130 (defmethod boxed-copy-fn ((info g-boxed-cstruct-wrapper-info
) native
)
131 (if (g-boxed-info-g-type info
)
132 (g-boxed-copy (g-boxed-info-g-type info
) native
)
133 (let ((copy (foreign-alloc (generated-cstruct-name (g-boxed-info-name info
)))))
134 (memcpy copy native
(foreign-type-size (generated-cstruct-name (g-boxed-info-name info
))))
137 (defmethod boxed-free-fn ((info g-boxed-cstruct-wrapper-info
) native
)
138 (if (g-boxed-info-g-type info
)
139 (g-boxed-free (g-boxed-info-g-type info
) native
)
140 (foreign-free native
)))
142 (defun copy-slots-to-native (proxy native cstruct-description
)
143 (iter (with cstruct-type
= (generated-cstruct-name (cstruct-description-name cstruct-description
)))
144 (for slot in
(cstruct-description-slots cstruct-description
))
145 (for slot-name
= (cstruct-slot-description-name slot
))
147 ((cstruct-slot-description-count slot
)
148 (iter (with ptr
= (foreign-slot-pointer native cstruct-type slot-name
))
149 (with array
= (slot-value proxy slot-name
))
150 (for i from
0 below
(cstruct-slot-description-count slot
))
151 (setf (mem-aref ptr
(cstruct-slot-description-type slot
) i
)
153 ((cstruct-slot-description-inline-p slot
)
154 (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot
))))
155 (copy-slots-to-native (slot-value proxy slot-name
)
156 (foreign-slot-pointer native cstruct-type slot-name
)
157 (g-boxed-cstruct-wrapper-info-cstruct-description info
))))
159 (setf (foreign-slot-value native cstruct-type slot-name
)
160 (slot-value proxy slot-name
))))))
162 (defun create-structure (structure-name)
163 (let ((constructor (get structure-name
'structure-constructor
)))
164 (assert constructor nil
"Don't know how to create structure of type ~A" structure-name
)
165 (funcall constructor
)))
167 (defun copy-slots-to-proxy (proxy native cstruct-description
)
168 (iter (with cstruct-type
= (generated-cstruct-name (cstruct-description-name cstruct-description
)))
169 (for slot in
(cstruct-description-slots cstruct-description
))
170 (for slot-name
= (cstruct-slot-description-name slot
))
172 ((cstruct-slot-description-count slot
)
173 (setf (slot-value proxy slot-name
) (make-array (list (cstruct-slot-description-count slot
))))
174 (iter (with ptr
= (foreign-slot-pointer native cstruct-type slot-name
))
175 (with array
= (slot-value proxy slot-name
))
176 (for i from
0 below
(cstruct-slot-description-count slot
))
178 (mem-aref ptr
(cstruct-slot-description-type slot
) i
))))
179 ((cstruct-slot-description-inline-p slot
)
180 (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot
))))
181 (setf (slot-value proxy slot-name
) (create-structure (cstruct-inline-slot-description-boxed-type-name slot
)))
182 (copy-slots-to-proxy (slot-value proxy slot-name
)
183 (foreign-slot-pointer native cstruct-type slot-name
)
184 (g-boxed-cstruct-wrapper-info-cstruct-description info
))))
185 (t (setf (slot-value proxy slot-name
)
186 (foreign-slot-value native cstruct-type slot-name
))))))
188 (defmethod translate-to-foreign (proxy (type boxed-cstruct-foreign-type
))
191 (let* ((info (g-boxed-foreign-info type
))
192 (native-structure-type (generated-cstruct-name (g-boxed-info-name info
))))
193 (with-foreign-object (native-structure native-structure-type
)
194 (copy-slots-to-native proxy native-structure
(g-boxed-cstruct-wrapper-info-cstruct-description info
))
195 (values (boxed-copy-fn info native-structure
) proxy
)))))
197 (defmethod free-translated-object (native-structure (type boxed-cstruct-foreign-type
) proxy
)
199 (let ((info (g-boxed-foreign-info type
)))
200 (copy-slots-to-proxy proxy native-structure
(g-boxed-cstruct-wrapper-info-cstruct-description info
))
201 (boxed-free-fn info native-structure
))))
203 (defmethod translate-from-foreign (native-structure (type boxed-cstruct-foreign-type
))
204 (unless (null-pointer-p native-structure
)
205 (let* ((info (g-boxed-foreign-info type
))
206 (proxy-structure-type (g-boxed-info-name info
))
207 (proxy (create-structure proxy-structure-type
)))
208 (copy-slots-to-proxy proxy native-structure
(g-boxed-cstruct-wrapper-info-cstruct-description info
))
209 (when (g-boxed-foreign-return-p type
)
210 (boxed-free-fn info native-structure
))
213 (defmethod cleanup-translated-object-for-callback ((type boxed-cstruct-foreign-type
) proxy native-structure
)
215 (let ((info (g-boxed-foreign-info type
)))
216 (copy-slots-to-native proxy native-structure
(g-boxed-cstruct-wrapper-info-cstruct-description info
)))))
218 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
219 (defstruct (g-boxed-opaque-wrapper-info (:include g-boxed-info
))
222 (define-foreign-type boxed-opaque-foreign-type
(g-boxed-foreign-type) ())
224 (defclass g-boxed-opaque
()
225 ((pointer :initarg
:pointer
227 :accessor g-boxed-opaque-pointer
)))
229 (defmethod pointer ((object g-boxed-opaque
))
230 (g-boxed-opaque-pointer object
))
232 (defmethod make-foreign-type ((info g-boxed-opaque-wrapper-info
) &key return-p
)
233 (make-instance 'boxed-opaque-foreign-type
:info info
:return-p return-p
))
235 (defmethod translate-to-foreign (proxy (type boxed-opaque-foreign-type
))
238 (prog1 (g-boxed-opaque-pointer proxy
)
239 (when (g-boxed-foreign-return-p type
)
240 (tg:cancel-finalization proxy
)
241 (setf (g-boxed-opaque-pointer proxy
) nil
)))))
243 (defmethod free-translated-object (native (type boxed-opaque-foreign-type
) param
)
244 (declare (ignore native type param
)))
246 (defvar *gboxed-gc-hooks-lock
* (make-recursive-lock "gboxed-gc-hooks-lock"))
247 (defvar *gboxed-gc-hooks
* nil
);;pointers to objects to be freed
249 (defun activate-gboxed-gc-hooks ()
250 (with-recursive-lock-held (*gboxed-gc-hooks-lock
*)
251 (when *gboxed-gc-hooks
*
252 (log-for :gc
"activating gc hooks for boxeds: ~A~%" *gboxed-gc-hooks
*)
254 for
(pointer type
) in
*gboxed-gc-hooks
*
255 do
(boxed-free-fn type pointer
))
256 (setf *gboxed-gc-hooks
* nil
))))
258 (defcallback gboxed-idle-gc-hook
:boolean
((data :pointer
))
259 (declare (ignore data
))
260 (activate-gboxed-gc-hooks)
263 (defun register-gboxed-for-gc (type pointer
)
264 (with-recursive-lock-held (*gboxed-gc-hooks-lock
*)
265 (let ((locks-were-present (not (null *gboxed-gc-hooks
*))))
266 (push (list pointer type
) *gboxed-gc-hooks
*)
267 (unless locks-were-present
268 (log-for :gc
"adding gboxed idle-gc-hook to main loop~%")
269 (g-idle-add (callback gboxed-idle-gc-hook
) (null-pointer))))))
271 (defun make-boxed-free-finalizer (type pointer
)
272 (lambda () (register-gboxed-for-gc type pointer
)))
274 (defmethod translate-from-foreign (native (foreign-type boxed-opaque-foreign-type
))
275 (let* ((type (g-boxed-foreign-info foreign-type
))
276 (proxy (make-instance (g-boxed-info-name type
) :pointer native
)))
279 (defmethod cleanup-translated-object-for-callback ((type boxed-opaque-foreign-type
) proxy native
)
280 (declare (ignore native
))
281 (tg:cancel-finalization proxy
)
282 (setf (g-boxed-opaque-pointer proxy
) nil
))
284 (defmacro define-g-boxed-opaque
(name g-type-name
&key
285 (alloc (error "Alloc must be specified")))
286 (let ((native-copy (gensym "NATIVE-COPY-"))
287 (instance (gensym "INSTANCE-")))
288 `(progn (defclass ,name
(g-boxed-opaque) ())
289 (defmethod initialize-instance :after
((,instance
,name
) &key
&allow-other-keys
)
290 (unless (g-boxed-opaque-pointer ,instance
)
291 (let ((,native-copy
,alloc
))
292 (setf (g-boxed-opaque-pointer ,instance
) ,native-copy
)
293 (finalize ,instance
(make-boxed-free-finalizer (get ',name
'g-boxed-foreign-info
) ,native-copy
)))))
294 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
295 (setf (get ',name
'g-boxed-foreign-info
)
296 (make-g-boxed-opaque-wrapper-info :name
',name
297 :g-type
,g-type-name
)
298 (gethash ,g-type-name
*g-type-name-
>g-boxed-foreign-info
*)
299 (get ',name
'g-boxed-foreign-info
))))))
301 (defstruct var-structure
307 resulting-cstruct-description
)
309 (defstruct var-structure-variant
310 discriminating-values
313 (defmethod make-load-form ((object var-structure
) &optional env
)
314 (make-load-form-saving-slots object
:environment env
))
316 (defmethod make-load-form ((object var-structure-variant
) &optional env
)
317 (make-load-form-saving-slots object
:environment env
))
319 (defun var-struct-all-slots (struct)
321 (append (var-struct-all-slots (var-structure-parent struct
))
322 (var-structure-slots struct
))))
324 (defun all-structures (structure)
325 (append (iter (for variant in
(var-structure-variants structure
))
326 (appending (all-structures (var-structure-variant-structure variant
))))
329 (defun parse-variant-structure-definition (name slots
&optional parent
)
330 (iter (with result
= (make-var-structure :name name
333 :discriminator-slot nil
336 (if (eq :variant
(first slot
))
338 (when (var-structure-discriminator-slot result
)
339 (error "Structure has more than one discriminator slot"))
340 (setf (var-structure-discriminator-slot result
) (second slot
)
341 (var-structure-variants result
) (parse-variants result
(nthcdr 2 slot
))))
342 (push (parse-cstruct-slot slot
) (var-structure-slots result
)))
343 (finally (setf (var-structure-slots result
)
344 (reverse (var-structure-slots result
)))
346 (set-variant-result-structure result
))
349 (defun set-variant-result-structure (var-structure)
350 (setf (var-structure-resulting-cstruct-description var-structure
)
351 (make-cstruct-description
353 (var-structure-name var-structure
)
356 (when (var-structure-parent var-structure
)
357 (cstruct-description-slots (var-structure-resulting-cstruct-description (var-structure-parent var-structure
))))
358 (var-structure-slots var-structure
))))
359 (iter (for variant in
(var-structure-variants var-structure
))
360 (for child-var-structure
= (var-structure-variant-structure variant
))
361 (set-variant-result-structure child-var-structure
)))
363 (defun ensure-list (thing)
368 (defun parse-variants (parent variants
)
369 (iter (for (options variant-name . slots
) in variants
)
371 (make-var-structure-variant
372 :discriminating-values
(ensure-list options
)
373 :structure
(parse-variant-structure-definition variant-name slots parent
)))
376 (defun generated-cstruct-name (symbol)
377 (intern (format nil
"~A-CSTRUCT" (symbol-name symbol
)) (symbol-package symbol
)))
379 (defun generated-cunion-name (symbol)
380 (intern (format nil
"~A-CUNION" (symbol-name symbol
)) (symbol-package symbol
)))
382 (defun generate-cstruct-1 (struct)
383 `(defcstruct ,(generated-cstruct-name (cstruct-description-name struct
))
384 ,@(iter (for slot in
(cstruct-description-slots struct
))
385 (collect `(,(cstruct-slot-description-name slot
) ,(cstruct-slot-description-type slot
)
386 ,@(when (cstruct-slot-description-count slot
)
387 `(:count
,(cstruct-slot-description-count slot
))))))))
389 (defun generate-c-structures (structure)
390 (iter (for str in
(all-structures structure
))
391 (for cstruct
= (var-structure-resulting-cstruct-description str
))
392 (collect (generate-cstruct-1 cstruct
))))
394 (defun generate-variant-union (struct)
395 `(defcunion ,(generated-cunion-name (var-structure-name struct
))
396 ,@(iter (for str in
(all-structures struct
))
397 (collect `(,(var-structure-name str
)
398 ,(generated-cstruct-name (var-structure-name str
)))))))
400 (defun generate-structure-1 (str)
401 (let ((name (var-structure-name str
)))
403 (defstruct ,(if (var-structure-parent str
)
404 `(,(var-structure-name str
) (:include
,(var-structure-name (var-structure-parent str
))
405 (,(var-structure-discriminator-slot (var-structure-parent str
))
406 ,(first (var-structure-variant-discriminating-values
408 (var-structure-variants
409 (var-structure-parent str
))
410 :key
#'var-structure-variant-structure
))))))
411 `,(var-structure-name str
))
412 ,@(iter (for slot in
(var-structure-slots str
))
413 (collect `(,(cstruct-slot-description-name slot
)
414 ,(cstruct-slot-description-initform slot
)))))
415 (setf (get ',name
'structure-constructor
)
416 ',(intern (format nil
"MAKE-~A" (symbol-name name
)) (symbol-package name
))))))
418 (defun generate-structures (str)
419 (iter (for variant in
(reverse (all-structures str
)))
420 (collect (generate-structure-1 variant
))))
422 (defun generate-native-type-decision-procedure-1 (str proxy-var
)
423 (if (null (var-structure-discriminator-slot str
))
424 `(values ',(var-structure-resulting-cstruct-description str
))
425 `(typecase ,proxy-var
426 ,@(iter (for variant in
(var-structure-variants str
))
427 (for v-str
= (var-structure-variant-structure variant
))
428 (collect `(,(var-structure-name v-str
)
429 ,(generate-native-type-decision-procedure-1 v-str proxy-var
))))
430 (,(var-structure-name str
)
431 (values ',(var-structure-resulting-cstruct-description str
))))))
433 (defun generate-proxy-type-decision-procedure-1 (str native-var
)
434 (if (null (var-structure-discriminator-slot str
))
435 `(values ',(var-structure-name str
)
436 ',(var-structure-resulting-cstruct-description str
))
437 `(case (foreign-slot-value ,native-var
438 ',(generated-cstruct-name (var-structure-name str
))
439 ',(var-structure-discriminator-slot str
))
440 ,@(iter (for variant in
(var-structure-variants str
))
441 (for v-str
= (var-structure-variant-structure variant
))
442 (collect `(,(var-structure-variant-discriminating-values variant
)
443 ,(generate-proxy-type-decision-procedure-1
446 (t (values ',(var-structure-name str
)
447 ',(var-structure-resulting-cstruct-description str
))))))
449 (defun generate-proxy-type-decision-procedure (str)
450 (let ((native (gensym "NATIVE-")))
452 (declare (ignorable ,native
))
453 ,(generate-proxy-type-decision-procedure-1 str native
))))
455 (defun generate-native-type-decision-procedure (str)
456 (let ((proxy (gensym "PROXY-")))
458 (declare (ignorable ,proxy
))
459 ,(generate-native-type-decision-procedure-1 str proxy
))))
461 (defun compile-proxy-type-decision-procedure (str)
462 (compile nil
(generate-proxy-type-decision-procedure str
)))
464 (defun compile-native-type-decision-procedure (str)
465 (compile nil
(generate-native-type-decision-procedure str
)))
467 (defstruct (g-boxed-variant-cstruct-info (:include g-boxed-info
))
469 native-type-decision-procedure
470 proxy-type-decision-procedure
)
472 (defmethod make-load-form ((object g-boxed-variant-cstruct-info
) &optional env
)
473 (make-load-form-saving-slots object
:environment env
))
475 (define-foreign-type boxed-variant-cstruct-foreign-type
(g-boxed-foreign-type) ())
477 (defmethod make-foreign-type ((info g-boxed-variant-cstruct-info
) &key return-p
)
478 (make-instance 'boxed-variant-cstruct-foreign-type
:info info
:return-p return-p
))
480 (defmacro define-g-boxed-variant-cstruct
(name g-type-name
&body slots
)
481 (let* ((structure (parse-variant-structure-definition name slots
)))
482 `(progn ,@(generate-c-structures structure
)
483 ,(generate-variant-union structure
)
484 ,@(generate-structures structure
)
485 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
486 (setf (get ',name
'g-boxed-foreign-info
)
487 (make-g-boxed-variant-cstruct-info :name
',name
490 :native-type-decision-procedure
491 ,(generate-native-type-decision-procedure structure
)
492 :proxy-type-decision-procedure
493 ,(generate-proxy-type-decision-procedure structure
))
494 (gethash ,g-type-name
*g-type-name-
>g-boxed-foreign-info
*)
495 (get ',name
'g-boxed-foreign-info
))))))
497 (defun decide-native-type (info proxy
)
498 (funcall (g-boxed-variant-cstruct-info-native-type-decision-procedure info
) proxy
))
500 (defmethod boxed-copy-fn ((info g-boxed-variant-cstruct-info
) native
)
501 (if (g-boxed-info-g-type info
)
502 (g-boxed-copy (g-boxed-info-g-type info
) native
)
503 (let ((copy (foreign-alloc (generated-cunion-name (g-boxed-info-name info
)))))
504 (memcpy copy native
(foreign-type-size (generated-cunion-name (g-boxed-info-name info
))))
507 (defmethod boxed-free-fn ((info g-boxed-variant-cstruct-info
) native
)
508 (if (g-boxed-info-g-type info
)
509 (g-boxed-free (g-boxed-info-g-type info
) native
)
510 (foreign-free native
)))
512 (defmethod translate-to-foreign (proxy (foreign-type boxed-variant-cstruct-foreign-type
))
515 (let* ((type (g-boxed-foreign-info foreign-type
))
516 (cstruct-description (decide-native-type type proxy
)))
517 (with-foreign-object (native-structure (generated-cunion-name
519 (g-boxed-variant-cstruct-info-root type
))))
520 (copy-slots-to-native proxy native-structure cstruct-description
)
521 (values (boxed-copy-fn type native-structure
) proxy
)))))
523 (defun decide-proxy-type (info native-structure
)
524 (funcall (g-boxed-variant-cstruct-info-proxy-type-decision-procedure info
) native-structure
))
526 (defmethod free-translated-object (native (foreign-type boxed-variant-cstruct-foreign-type
) proxy
)
528 (let ((type (g-boxed-foreign-info foreign-type
)))
529 (multiple-value-bind (actual-struct cstruct-description
) (decide-proxy-type type native
)
530 (unless (eq (type-of proxy
) actual-struct
)
532 (error "Expected type of boxed variant structure ~A and actual type ~A do not match"
533 (type-of proxy
) actual-struct
)
534 (skip-parsing-values () (return-from free-translated-object
))))
535 (copy-slots-to-proxy proxy native cstruct-description
)
536 (boxed-free-fn type native
)))))
538 (defmethod translate-from-foreign (native (foreign-type boxed-variant-cstruct-foreign-type
))
539 (unless (null-pointer-p native
)
540 (let ((type (g-boxed-foreign-info foreign-type
)))
541 (multiple-value-bind (actual-struct cstruct-description
) (decide-proxy-type type native
)
542 (let ((proxy (create-structure actual-struct
)))
543 (copy-slots-to-proxy proxy native cstruct-description
)
544 (when (g-boxed-foreign-return-p foreign-type
)
545 (boxed-free-fn type native
))
548 (defmethod cleanup-translated-object-for-callback ((foreign-type boxed-variant-cstruct-foreign-type
) proxy native
)
550 (let ((type (g-boxed-foreign-info foreign-type
)))
551 (let ((cstruct-description (decide-native-type type proxy
)))
552 (copy-slots-to-native proxy native cstruct-description
)))))
554 (defgeneric boxed-parse-g-value
(gvalue-ptr info
))
556 (defgeneric boxed-set-g-value
(gvalue-ptr info proxy
))
558 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql (gtype +g-type-boxed
+))) parse-kind
)
559 (declare (ignore parse-kind
))
560 (if (g-type= (g-value-type gvalue-ptr
) (g-strv-get-type))
561 (convert-from-foreign (g-value-get-boxed gvalue-ptr
) '(glib:gstrv
:free-from-foreign nil
))
562 (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr
))))
563 (boxed-parse-g-value gvalue-ptr boxed-type
))))
565 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql (gtype +g-type-boxed
+))) value
)
566 (if (g-type= (g-value-type gvalue-ptr
) (g-strv-get-type))
567 (g-value-set-boxed gvalue-ptr
(convert-to-foreign value
'(glib:gstrv
:free-from-foreign nil
)))
568 (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr
))))
569 (boxed-set-g-value gvalue-ptr boxed-type value
))))
571 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info
))
572 (translate-from-foreign (g-value-get-boxed gvalue-ptr
) (make-foreign-type info
:return-p nil
)))
574 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info
) proxy
)
575 (g-value-take-boxed gvalue-ptr
(translate-to-foreign proxy
(make-foreign-type info
:return-p nil
))))
577 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info
))
578 (translate-from-foreign (g-value-get-boxed gvalue-ptr
) (make-foreign-type info
:return-p nil
)))
580 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info
) proxy
)
581 (g-value-take-boxed gvalue-ptr
(translate-to-foreign proxy
(make-foreign-type info
:return-p nil
))))
583 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info
))
584 (translate-from-foreign (boxed-copy-fn info
(g-value-get-boxed gvalue-ptr
)) (make-foreign-type info
:return-p nil
)))
586 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info
) proxy
)
587 (g-value-set-boxed gvalue-ptr
(translate-to-foreign proxy
(make-foreign-type info
:return-p nil
))))
589 (defun boxed-related-symbols (name)
590 (let ((info (get-g-boxed-foreign-info name
)))
592 (g-boxed-cstruct-wrapper-info
594 (intern (format nil
"MAKE-~A" (symbol-name name
)))
595 (intern (format nil
"COPY-~A" (symbol-name name
))))
596 (iter (for slot in
(cstruct-description-slots (g-boxed-cstruct-wrapper-info-cstruct-description info
)))
597 (for slot-name
= (cstruct-slot-description-name slot
))
598 (collect (intern (format nil
"~A-~A" (symbol-name name
) (symbol-name slot-name
)))))))
599 (g-boxed-opaque-wrapper-info
601 (g-boxed-variant-cstruct-info
603 (iter (for var-struct in
(all-structures (g-boxed-variant-cstruct-info-root info
)))
604 (for s-name
= (var-structure-name var-struct
))
605 (for cstruct-description
= (var-structure-resulting-cstruct-description var-struct
))
606 (appending (append (list s-name
)
607 (list (intern (format nil
"MAKE-~A" (symbol-name s-name
)))
608 (intern (format nil
"COPY-~A" (symbol-name s-name
))))
609 (iter (for slot in
(cstruct-description-slots cstruct-description
))
610 (for slot-name
= (cstruct-slot-description-name slot
))
611 (collect (intern (format nil
"~A-~A" (symbol-name s-name
)
612 (symbol-name slot-name
)))))))))))))
614 (defmacro define-boxed-opaque-accessor
(boxed-name accessor-name
&key type reader writer
)
617 `(progn ,@(when reader
618 (list (etypecase reader
619 (symbol `(defun ,accessor-name
(,var
)
620 (funcall ,reader
,var
)))
621 (string `(defcfun (,accessor-name
,reader
) ,type
622 (,var
(g-boxed-foreign ,boxed-name
)))))))
624 (list (etypecase reader
625 (symbol `(defun (setf ,accessor-name
) (,n-var
,var
)
626 (funcall ,reader
,n-var
,var
)))
627 (string `(defun (setf ,accessor-name
) (,n-var
,var
)
628 (foreign-funcall ,writer
(g-boxed-foreign ,boxed-name
) ,var
,type
,n-var
:void
)))))))))
630 (defun copy-boxed-slots-to-foreign (structure native-ptr
&optional
(type (and structure
(type-of structure
))))
632 (copy-slots-to-native
635 (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info type
)))))
637 (define-compiler-macro copy-boxed-slots-to-foreign
(&whole whole structure native-ptr
&optional type
)
640 (let* ((type-r (eval type
))
641 (f-i (get-g-boxed-foreign-info type-r
)))
643 (warn "Unknown foreign GBoxed type ~S" type-r
)
644 (return-from copy-boxed-slots-to-foreign whole
))
645 (unless (typep f-i
'g-boxed-cstruct-wrapper-info
)
646 (warn "Foreign GBoxed type ~S is not a C structure wrapper" type-r
)
647 (return-from copy-boxed-slots-to-foreign whole
))
649 (copy-slots-to-native
652 (load-time-value (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info ',type-r
))))))
655 (defmacro with-foreign-boxed-array
((n-var array-var type values-seq
) &body body
)
656 (let ((values-seq-1 (gensym "VALUES-SEQ-"))
657 (cstruct (generated-cstruct-name type
))
660 `(let* ((,values-seq-1
,values-seq
)
661 (,n-var
(length ,values-seq-1
)))
662 (with-foreign-object (,array-var
',cstruct
,n-var
)
664 (map nil
(lambda (,x
)
665 (copy-boxed-slots-to-foreign
667 (inc-pointer ,array-var
(* ,i
(foreign-type-size ',cstruct
)))