Add handling of NIL values in translate-to-foreign for boxed-opaque
[cl-gtk2.git] / glib / gobject.boxed.lisp
1 (in-package :gobject)
2
3 (define-foreign-type g-boxed-foreign-type ()
4   ((info :initarg :info
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
9              :initform nil))
10   (:actual-type :pointer))
11
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13   (defstruct g-boxed-info
14     name
15     g-type))
16
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18   (defun get-g-boxed-foreign-info (name)
19     (get name 'g-boxed-foreign-info)))
20
21 (defvar *g-type-name->g-boxed-foreign-info* (make-hash-table :test 'equal))
22
23 (defun get-g-boxed-foreign-info-for-gtype (g-type-designator)
24   (or (gethash (g-type-string g-type-designator) *g-type-name->g-boxed-foreign-info*)
25       (error "Unknown GBoxed type '~A'" (g-type-string g-type-designator))))
26
27 (defgeneric make-foreign-type (info &key return-p))
28
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))))
33
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)))
37
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))
40
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)))
44
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))
47
48 (defgeneric has-callback-cleanup (foreign-type))
49 (defgeneric cleanup-translated-object-for-callback (foreign-type converted-object native-object))
50
51 (defmethod has-callback-cleanup ((type g-boxed-foreign-type))
52   t)
53
54 (eval-when (:load-toplevel :compile-toplevel :execute)
55   (defstruct (g-boxed-cstruct-wrapper-info (:include g-boxed-info))
56     cstruct-description))
57
58 (defclass boxed-cstruct-foreign-type (g-boxed-foreign-type) ())
59
60 (defstruct cstruct-slot-description
61   name
62   type
63   count
64   initform
65   inline-p)
66
67 (defstruct (cstruct-inline-slot-description (:include cstruct-slot-description))
68   boxed-type-name)
69
70 (defmethod make-load-form ((object cstruct-slot-description) &optional environment)
71   (make-load-form-saving-slots object :environment environment))
72
73 (defmethod make-load-form ((object cstruct-inline-slot-description) &optional environment)
74   (make-load-form-saving-slots object :environment environment))
75
76 (defstruct cstruct-description
77   name
78   slots)
79
80 (defmethod make-load-form ((object cstruct-description) &optional environment)
81   (make-load-form-saving-slots object :environment environment))
82
83 (defun parse-cstruct-slot (slot)
84   (destructuring-bind (name type &key count initform inline) slot
85     (if inline
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))))
91
92 (defun parse-cstruct-definition (name slots)
93   (make-cstruct-description :name name
94                             :slots (mapcar #'parse-cstruct-slot slots)))
95
96 (defmacro define-g-boxed-cstruct (name g-type-name &body slots)
97   (let ((cstruct-description (parse-cstruct-definition name slots)))
98     `(progn
99        (defstruct ,name
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
115                                                   :g-type ,g-type-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)))))))
121
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))
124
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))))
129
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))))
135         copy)))
136
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)))
141
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))
146         (cond
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)
152                        (aref array 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))))
158           (t
159            (setf (foreign-slot-value native cstruct-type slot-name)
160                  (slot-value proxy slot-name))))))
161
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)))
166
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))
171         (cond
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))
177                  (setf (aref array i)
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))))))
187
188 (defmethod translate-to-foreign (proxy (type boxed-cstruct-foreign-type))
189   (if (null proxy)
190       (null-pointer)
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)))))
196
197 (defmethod free-translated-object (native-structure (type boxed-cstruct-foreign-type) proxy)
198   (when 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))))
202
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))
211       proxy)))
212
213 (defmethod cleanup-translated-object-for-callback ((type boxed-cstruct-foreign-type) proxy native-structure)
214   (when proxy
215     (let ((info (g-boxed-foreign-info type)))
216       (copy-slots-to-native proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info)))))
217
218 (eval-when (:compile-toplevel :load-toplevel :execute)
219   (defstruct (g-boxed-opaque-wrapper-info (:include g-boxed-info))
220     alloc free))
221
222 (define-foreign-type boxed-opaque-foreign-type (g-boxed-foreign-type) ())
223
224 (defclass g-boxed-opaque ()
225   ((pointer :initarg :pointer
226             :initform nil
227             :accessor g-boxed-opaque-pointer)))
228
229 (defmethod pointer ((object g-boxed-opaque))
230   (g-boxed-opaque-pointer object))
231
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))
234
235 (defmethod translate-to-foreign (proxy (type boxed-opaque-foreign-type))
236   (if (null proxy)
237       (null-pointer)
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)))))
242
243 (defmethod free-translated-object (native (type boxed-opaque-foreign-type) param)
244   (declare (ignore native type param)))
245
246 (defun make-boxed-free-finalizer (type pointer)
247   (lambda () (boxed-free-fn type pointer)))
248
249 (defmethod translate-from-foreign (native (foreign-type boxed-opaque-foreign-type))
250   (let* ((type (g-boxed-foreign-info foreign-type))
251          (proxy (make-instance (g-boxed-info-name type) :pointer native)))
252     (tg:finalize proxy (make-boxed-free-finalizer type native))))
253
254 (defmethod cleanup-translated-object-for-callback ((type boxed-opaque-foreign-type) proxy native)
255   (declare (ignore native))
256   (tg:cancel-finalization proxy)
257   (setf (g-boxed-opaque-pointer proxy) nil))
258
259 (defmacro define-g-boxed-opaque (name g-type-name &key
260                                  (alloc (error "Alloc must be specified")))
261   (let ((native-copy (gensym "NATIVE-COPY-"))
262         (instance (gensym "INSTANCE-")))
263     `(progn (defclass ,name (g-boxed-opaque) ())
264             (defmethod initialize-instance :after ((,instance ,name) &key &allow-other-keys)
265               (unless (g-boxed-opaque-pointer ,instance)
266                 (let ((,native-copy ,alloc))
267                   (setf (g-boxed-opaque-pointer ,instance) ,native-copy)
268                   (finalize ,instance (make-boxed-free-finalizer (get ',name 'g-boxed-foreign-info) ,native-copy)))))
269             (eval-when (:compile-toplevel :load-toplevel :execute)
270               (setf (get ',name 'g-boxed-foreign-info)
271                     (make-g-boxed-opaque-wrapper-info :name ',name
272                                                       :g-type ,g-type-name)
273                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
274                     (get ',name 'g-boxed-foreign-info))))))
275
276 (defstruct var-structure
277   name
278   parent
279   slots
280   discriminator-slot
281   variants
282   resulting-cstruct-description)
283
284 (defstruct var-structure-variant
285   discriminating-values
286   structure)
287
288 (defmethod make-load-form ((object var-structure) &optional env)
289   (make-load-form-saving-slots object :environment env))
290
291 (defmethod make-load-form ((object var-structure-variant) &optional env)
292   (make-load-form-saving-slots object :environment env))
293
294 (defun var-struct-all-slots (struct)
295   (when struct
296     (append (var-struct-all-slots (var-structure-parent struct))
297             (var-structure-slots struct))))
298
299 (defun all-structures (structure)
300   (append (iter (for variant in (var-structure-variants structure))
301                 (appending (all-structures (var-structure-variant-structure variant))))
302           (list structure)))
303
304 (defun parse-variant-structure-definition (name slots &optional parent)
305   (iter (with result = (make-var-structure :name name
306                                            :parent parent
307                                            :slots nil
308                                            :discriminator-slot nil
309                                            :variants nil))
310         (for slot in slots)
311         (if (eq :variant (first slot))
312             (progn
313               (when (var-structure-discriminator-slot result)
314                 (error "Structure has more than one discriminator slot"))
315               (setf (var-structure-discriminator-slot result) (second slot)
316                     (var-structure-variants result) (parse-variants result (nthcdr 2 slot))))
317             (push (parse-cstruct-slot slot) (var-structure-slots result)))
318         (finally (setf (var-structure-slots result)
319                        (reverse (var-structure-slots result)))
320                  (unless parent
321                    (set-variant-result-structure result))
322                  (return result))))
323
324 (defun set-variant-result-structure (var-structure)
325   (setf (var-structure-resulting-cstruct-description var-structure)
326         (make-cstruct-description
327          :name
328          (var-structure-name var-structure)
329          :slots
330          (append
331           (when (var-structure-parent var-structure)
332             (cstruct-description-slots (var-structure-resulting-cstruct-description (var-structure-parent var-structure))))
333           (var-structure-slots var-structure))))
334   (iter (for variant in (var-structure-variants var-structure))
335         (for child-var-structure = (var-structure-variant-structure variant))
336         (set-variant-result-structure child-var-structure)))
337
338 (defun ensure-list (thing)
339   (if (listp thing)
340       thing
341       (list thing)))
342
343 (defun parse-variants (parent variants)
344   (iter (for (options variant-name . slots) in variants)
345         (for variant =
346              (make-var-structure-variant
347               :discriminating-values (ensure-list options)
348               :structure (parse-variant-structure-definition variant-name slots parent)))
349         (collect variant)))
350
351 (defun generated-cstruct-name (symbol)
352   (intern (format nil "~A-CSTRUCT" (symbol-name symbol)) (symbol-package symbol)))
353
354 (defun generated-cunion-name (symbol)
355   (intern (format nil "~A-CUNION" (symbol-name symbol)) (symbol-package symbol)))
356
357 (defun generate-cstruct-1 (struct)
358   `(defcstruct ,(generated-cstruct-name (cstruct-description-name struct))
359      ,@(iter (for slot in (cstruct-description-slots struct))
360              (collect `(,(cstruct-slot-description-name slot) ,(cstruct-slot-description-type slot)
361                          ,@(when (cstruct-slot-description-count slot)
362                                  `(:count ,(cstruct-slot-description-count slot))))))))
363
364 (defun generate-c-structures (structure)
365   (iter (for str in (all-structures structure))
366         (for cstruct = (var-structure-resulting-cstruct-description str))
367         (collect (generate-cstruct-1 cstruct))))
368
369 (defun generate-variant-union (struct)
370   `(defcunion ,(generated-cunion-name (var-structure-name struct))
371      ,@(iter (for str in (all-structures struct))
372              (collect `(,(var-structure-name str)
373                          ,(generated-cstruct-name (var-structure-name str)))))))
374
375 (defun generate-structure-1 (str)
376   (let ((name (var-structure-name str)))
377     `(progn
378        (defstruct ,(if (var-structure-parent str)
379                        `(,(var-structure-name str) (:include ,(var-structure-name (var-structure-parent str))
380                                                              (,(var-structure-discriminator-slot (var-structure-parent str))
381                                                                ,(first (var-structure-variant-discriminating-values
382                                                                         (find str
383                                                                               (var-structure-variants
384                                                                                (var-structure-parent str))
385                                                                               :key #'var-structure-variant-structure))))))
386                        `,(var-structure-name str))
387          ,@(iter (for slot in (var-structure-slots str))
388                  (collect `(,(cstruct-slot-description-name slot)
389                              ,(cstruct-slot-description-initform slot)))))
390        (setf (get ',name 'structure-constructor)
391              ',(intern (format nil "MAKE-~A" (symbol-name name)) (symbol-package name))))))
392
393 (defun generate-structures (str)
394   (iter (for variant in (reverse (all-structures str)))
395         (collect (generate-structure-1 variant))))
396
397 (defun generate-native-type-decision-procedure-1 (str proxy-var)
398   (if (null (var-structure-discriminator-slot str))
399       `(values ',(var-structure-resulting-cstruct-description str))
400       `(typecase ,proxy-var
401          ,@(iter (for variant in (var-structure-variants str))
402                  (for v-str = (var-structure-variant-structure variant))
403                  (collect `(,(var-structure-name v-str)
404                              ,(generate-native-type-decision-procedure-1 v-str proxy-var))))
405          (,(var-structure-name str)
406           (values ',(var-structure-resulting-cstruct-description str))))))
407
408 (defun generate-proxy-type-decision-procedure-1 (str native-var)
409   (if (null (var-structure-discriminator-slot str))
410       `(values ',(var-structure-name str)
411                ',(var-structure-resulting-cstruct-description str))
412       `(case (foreign-slot-value ,native-var
413                                  ',(generated-cstruct-name (var-structure-name str))
414                                  ',(var-structure-discriminator-slot str))
415          ,@(iter (for variant in (var-structure-variants str))
416                  (for v-str = (var-structure-variant-structure variant))
417                  (collect `(,(var-structure-variant-discriminating-values variant)
418                              ,(generate-proxy-type-decision-procedure-1
419                                v-str
420                                native-var))))
421          (t (values ',(var-structure-name str)
422                     ',(var-structure-resulting-cstruct-description str))))))
423
424 (defun generate-proxy-type-decision-procedure (str)
425   (let ((native (gensym "NATIVE-")))
426     `(lambda (,native)
427        (declare (ignorable ,native))
428        ,(generate-proxy-type-decision-procedure-1 str native))))
429
430 (defun generate-native-type-decision-procedure (str)
431   (let ((proxy (gensym "PROXY-")))
432     `(lambda (,proxy)
433        (declare (ignorable ,proxy))
434        ,(generate-native-type-decision-procedure-1 str proxy))))
435
436 (defun compile-proxy-type-decision-procedure (str)
437   (compile nil (generate-proxy-type-decision-procedure str)))
438
439 (defun compile-native-type-decision-procedure (str)
440   (compile nil (generate-native-type-decision-procedure str)))
441
442 (defstruct (g-boxed-variant-cstruct-info (:include g-boxed-info))
443   root
444   native-type-decision-procedure
445   proxy-type-decision-procedure)
446
447 (defmethod make-load-form ((object g-boxed-variant-cstruct-info) &optional env)
448   (make-load-form-saving-slots object :environment env))
449
450 (define-foreign-type boxed-variant-cstruct-foreign-type (g-boxed-foreign-type) ())
451
452 (defmethod make-foreign-type ((info g-boxed-variant-cstruct-info) &key return-p)
453   (make-instance 'boxed-variant-cstruct-foreign-type :info info :return-p return-p))
454
455 (defmacro define-g-boxed-variant-cstruct (name g-type-name &body slots)
456   (let* ((structure (parse-variant-structure-definition name slots)))
457     `(progn ,@(generate-c-structures structure)
458             ,(generate-variant-union structure)
459             ,@(generate-structures structure)
460             (eval-when (:compile-toplevel :load-toplevel :execute)
461               (setf (get ',name 'g-boxed-foreign-info)
462                     (make-g-boxed-variant-cstruct-info :name ',name
463                                                        :g-type ,g-type-name
464                                                        :root ,structure
465                                                        :native-type-decision-procedure
466                                                        ,(generate-native-type-decision-procedure structure)
467                                                        :proxy-type-decision-procedure
468                                                        ,(generate-proxy-type-decision-procedure structure))
469                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
470                     (get ',name 'g-boxed-foreign-info))))))
471
472 (defun decide-native-type (info proxy)
473   (funcall (g-boxed-variant-cstruct-info-native-type-decision-procedure info) proxy))
474
475 (defmethod boxed-copy-fn ((info g-boxed-variant-cstruct-info) native)
476   (if (g-boxed-info-g-type info)
477       (g-boxed-copy (g-boxed-info-g-type info) native)
478       (let ((copy (foreign-alloc (generated-cunion-name (g-boxed-info-name info)))))
479         (memcpy copy native (foreign-type-size (generated-cunion-name (g-boxed-info-name info))))
480         copy)))
481
482 (defmethod boxed-free-fn ((info g-boxed-variant-cstruct-info) native)
483   (if (g-boxed-info-g-type info)
484       (g-boxed-free (g-boxed-info-g-type info) native)
485       (foreign-free native)))
486
487 (defmethod translate-to-foreign (proxy (foreign-type boxed-variant-cstruct-foreign-type))
488   (if (null proxy)
489       (null-pointer)
490       (let* ((type (g-boxed-foreign-info foreign-type))
491              (cstruct-description (decide-native-type type proxy)))
492         (with-foreign-object (native-structure (generated-cunion-name
493                                                 (var-structure-name
494                                                  (g-boxed-variant-cstruct-info-root type))))
495           (copy-slots-to-native proxy native-structure cstruct-description)
496           (values (boxed-copy-fn type native-structure) proxy)))))
497
498 (defun decide-proxy-type (info native-structure)
499   (funcall (g-boxed-variant-cstruct-info-proxy-type-decision-procedure info) native-structure))
500
501 (defmethod free-translated-object (native (foreign-type boxed-variant-cstruct-foreign-type) proxy)
502   (when proxy
503     (let ((type (g-boxed-foreign-info foreign-type)))
504       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
505         (unless (eq (type-of proxy) actual-struct)
506           (restart-case
507               (error "Expected type of boxed variant structure ~A and actual type ~A do not match"
508                      (type-of proxy) actual-struct)
509             (skip-parsing-values () (return-from free-translated-object))))
510         (copy-slots-to-proxy proxy native cstruct-description)
511         (boxed-free-fn type native)))))
512
513 (defmethod translate-from-foreign (native (foreign-type boxed-variant-cstruct-foreign-type))
514   (unless (null-pointer-p native)
515     (let ((type (g-boxed-foreign-info foreign-type)))
516       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
517         (let ((proxy (create-structure actual-struct)))
518           (copy-slots-to-proxy proxy native cstruct-description)
519           (when (g-boxed-foreign-return-p foreign-type)
520             (boxed-free-fn type native))
521           proxy)))))
522
523 (defmethod cleanup-translated-object-for-callback ((foreign-type boxed-variant-cstruct-foreign-type) proxy native)
524   (when proxy
525     (let ((type (g-boxed-foreign-info foreign-type)))
526       (let ((cstruct-description (decide-native-type type proxy)))
527         (copy-slots-to-native proxy native cstruct-description)))))
528
529 (defgeneric boxed-parse-g-value (gvalue-ptr info))
530
531 (defgeneric boxed-set-g-value (gvalue-ptr info proxy))
532
533 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) parse-kind)
534   (declare (ignore parse-kind))
535   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
536       (convert-from-foreign (g-value-get-boxed gvalue-ptr) '(glib:gstrv :free-from-foreign nil))
537       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
538         (boxed-parse-g-value gvalue-ptr boxed-type))))
539
540 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) value)
541   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
542       (g-value-set-boxed gvalue-ptr (convert-to-foreign value '(glib:gstrv :free-from-foreign nil)))
543       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
544         (boxed-set-g-value gvalue-ptr boxed-type value))))
545
546 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info))
547   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
548
549 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info) proxy)
550   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
551
552 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info))
553   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
554
555 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info) proxy)
556   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
557
558 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info))
559   (translate-from-foreign (boxed-copy-fn info (g-value-get-boxed gvalue-ptr)) (make-foreign-type info :return-p nil)))
560
561 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info) proxy)
562   (g-value-set-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
563
564 (defun boxed-related-symbols (name)
565   (let ((info (get-g-boxed-foreign-info name)))
566     (etypecase info
567       (g-boxed-cstruct-wrapper-info
568        (append (list name
569                      (intern (format nil "MAKE-~A" (symbol-name name)))
570                      (intern (format nil "COPY-~A" (symbol-name name))))
571                (iter (for slot in (cstruct-description-slots (g-boxed-cstruct-wrapper-info-cstruct-description info)))
572                      (for slot-name = (cstruct-slot-description-name slot))
573                      (collect (intern (format nil "~A-~A" (symbol-name name) (symbol-name slot-name)))))))
574       (g-boxed-opaque-wrapper-info
575        (list name))
576       (g-boxed-variant-cstruct-info
577        (append (list name)
578                (iter (for var-struct in (all-structures (g-boxed-variant-cstruct-info-root info)))
579                      (for s-name = (var-structure-name var-struct))
580                      (for cstruct-description = (var-structure-resulting-cstruct-description var-struct))
581                      (appending (append (list s-name)
582                                         (list (intern (format nil "MAKE-~A" (symbol-name s-name)))
583                                               (intern (format nil "COPY-~A" (symbol-name s-name))))
584                                         (iter (for slot in (cstruct-description-slots cstruct-description))
585                                               (for slot-name = (cstruct-slot-description-name slot))
586                                               (collect (intern (format nil "~A-~A" (symbol-name s-name)
587                                                                        (symbol-name slot-name)))))))))))))
588
589 (defmacro define-boxed-opaque-accessor (boxed-name accessor-name &key type reader writer)
590   (let ((var (gensym))
591         (n-var (gensym)))
592     `(progn ,@(when reader
593                     (list (etypecase reader
594                             (symbol `(defun ,accessor-name (,var)
595                                        (funcall ,reader ,var)))
596                             (string `(defcfun (,accessor-name ,reader) ,type
597                                        (,var (g-boxed-foreign ,boxed-name)))))))
598             ,@(when writer
599                     (list (etypecase reader
600                             (symbol `(defun (setf ,accessor-name) (,n-var ,var)
601                                        (funcall ,reader ,n-var ,var)))
602                             (string `(defun (setf ,accessor-name) (,n-var ,var)
603                                        (foreign-funcall ,writer (g-boxed-foreign ,boxed-name) ,var ,type ,n-var :void)))))))))
604
605 (defun copy-boxed-slots-to-foreign (structure native-ptr &optional (type (and structure (type-of structure))))
606   (when structure
607     (copy-slots-to-native
608      structure
609      native-ptr
610      (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info type)))))
611
612 (define-compiler-macro copy-boxed-slots-to-foreign (&whole whole structure native-ptr &optional type)
613   (if (and type
614            (constantp type))
615       (let* ((type-r (eval type))
616              (f-i (get-g-boxed-foreign-info type-r)))
617         (unless f-i
618           (warn "Unknown foreign GBoxed type ~S" type-r)
619           (return-from copy-boxed-slots-to-foreign whole))
620         (unless (typep f-i 'g-boxed-cstruct-wrapper-info)
621           (warn "Foreign GBoxed type ~S is not a C structure wrapper" type-r)
622           (return-from copy-boxed-slots-to-foreign whole))
623         `(when ,structure
624            (copy-slots-to-native
625             ,structure
626             ,native-ptr
627             (load-time-value (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info ',type-r))))))
628       whole))
629
630 (defmacro with-foreign-boxed-array ((n-var array-var type values-seq) &body body)
631   (let ((values-seq-1 (gensym "VALUES-SEQ-"))
632         (cstruct (generated-cstruct-name type))
633         (x (gensym "X-"))
634         (i (gensym "I-")))
635     `(let* ((,values-seq-1 ,values-seq)
636             (,n-var (length ,values-seq-1)))
637        (with-foreign-object (,array-var ',cstruct ,n-var)
638          (let ((,i 0))
639            (map nil (lambda (,x)
640                       (copy-boxed-slots-to-foreign
641                        ,x
642                        (inc-pointer ,array-var (* ,i (foreign-type-size ',cstruct)))
643                        ',type)
644                       (incf ,i))
645                 ,values-seq-1))
646          ,@body))))