glib: Changed generated-c{struct,union}-name to generate the same names every times...
[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 (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 (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 (defmethod has-callback-cleanup ((type g-boxed-foreign-type))
49   t)
50
51 (eval-when (:load-toplevel :compile-toplevel :execute)
52   (defstruct (g-boxed-cstruct-wrapper-info (:include g-boxed-info))
53     cstruct-description))
54
55 (defclass boxed-cstruct-foreign-type (g-boxed-foreign-type) ())
56
57 (defstruct cstruct-slot-description
58   name
59   type
60   count
61   initform
62   inline-p)
63
64 (defstruct (cstruct-inline-slot-description (:include cstruct-slot-description))
65   boxed-type-name)
66
67 (defmethod make-load-form ((object cstruct-slot-description) &optional environment)
68   (make-load-form-saving-slots object :environment environment))
69
70 (defmethod make-load-form ((object cstruct-inline-slot-description) &optional environment)
71   (make-load-form-saving-slots object :environment environment))
72
73 (defstruct cstruct-description
74   name
75   slots)
76
77 (defmethod make-load-form ((object cstruct-description) &optional environment)
78   (make-load-form-saving-slots object :environment environment))
79
80 (defun parse-cstruct-slot (slot)
81   (destructuring-bind (name type &key count initform inline) slot
82     (if inline
83         (make-cstruct-inline-slot-description :name name :type (generated-cunion-name type)
84                                        :count count :initform initform :inline-p inline
85                                        :boxed-type-name type)
86         (make-cstruct-inline-slot-description :name name :type type
87                                               :count count :initform initform :inline-p inline))))
88
89 (defun parse-cstruct-definition (name slots)
90   (make-cstruct-description :name name
91                             :slots (mapcar #'parse-cstruct-slot slots)))
92
93 (defmacro define-g-boxed-cstruct (name g-type-name &body slots)
94   (let ((cstruct-description (parse-cstruct-definition name slots)))
95     `(progn
96        (defstruct ,name
97          ,@(iter (for slot in (cstruct-description-slots cstruct-description))
98                  (for name = (cstruct-slot-description-name slot))
99                  (for initform = (cstruct-slot-description-initform slot))
100                  (collect (list name initform))))
101        (defcstruct ,(generated-cstruct-name name)
102          ,@(iter (for slot in (cstruct-description-slots cstruct-description))
103                  (for name = (cstruct-slot-description-name slot))
104                  (for type = (cstruct-slot-description-type slot))
105                  (for count = (cstruct-slot-description-count slot))
106                  (collect `(,name ,type ,@(when count `(:count ,count))))))
107        (defcunion ,(generated-cunion-name name)
108          (,name ,(generated-cstruct-name name)))
109        (eval-when (:compile-toplevel :load-toplevel :execute)
110          (setf (get ',name 'g-boxed-foreign-info)
111                (make-g-boxed-cstruct-wrapper-info :name ',name
112                                                   :g-type ,g-type-name
113                                                   :cstruct-description ,cstruct-description)
114                (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
115                (get ',name 'g-boxed-foreign-info))))))
116
117 (defmethod make-foreign-type ((info g-boxed-cstruct-wrapper-info) &key return-p)
118   (make-instance 'boxed-cstruct-foreign-type :info info :return-p return-p))
119
120 (defun memcpy (target source bytes)
121   (iter (for i from 0 below bytes)
122         (setf (mem-aref target :uchar i)
123               (mem-aref source :uchar i))))
124
125 (defmethod boxed-copy-fn ((info g-boxed-cstruct-wrapper-info) native)
126   (if (g-boxed-info-g-type info)
127       (g-boxed-copy (g-boxed-info-g-type info) native)
128       (let ((copy (foreign-alloc (generated-cstruct-name (g-boxed-info-name info)))))
129         (memcpy copy native (foreign-type-size (generated-cstruct-name (g-boxed-info-name info))))
130         copy)))
131
132 (defmethod boxed-free-fn ((info g-boxed-cstruct-wrapper-info) native)
133   (if (g-boxed-info-g-type info)
134       (g-boxed-free (g-boxed-info-g-type info) native)
135       (foreign-free native)))
136
137 (defun copy-slots-to-native (proxy native cstruct-description)
138   (iter (with cstruct-type = (generated-cstruct-name (cstruct-description-name cstruct-description)))
139         (for slot in (cstruct-description-slots cstruct-description))
140         (for slot-name = (cstruct-slot-description-name slot))
141         (cond
142           ((cstruct-slot-description-count slot)
143            (iter (with ptr = (foreign-slot-pointer native cstruct-type slot-name))
144                  (with array = (slot-value proxy slot-name))
145                  (for i from 0 below (cstruct-slot-description-count slot))
146                  (setf (mem-aref ptr (cstruct-slot-description-type slot) i)
147                        (aref array i))))
148           ((cstruct-slot-description-inline-p slot)
149            (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot))))
150              (copy-slots-to-native (slot-value proxy slot-name)
151                                    (foreign-slot-pointer native cstruct-type slot-name)
152                                    (g-boxed-cstruct-wrapper-info-cstruct-description info))))
153           (t
154            (setf (foreign-slot-value native cstruct-type slot-name)
155                  (slot-value proxy slot-name))))))
156
157 (defun copy-slots-to-proxy (proxy native cstruct-description)
158   (iter (with cstruct-type = (generated-cstruct-name (cstruct-description-name cstruct-description)))
159         (for slot in (cstruct-description-slots cstruct-description))
160         (for slot-name = (cstruct-slot-description-name slot))
161         (cond
162           ((cstruct-slot-description-count slot)
163            (setf (slot-value proxy slot-name) (make-array (list (cstruct-slot-description-count slot))))
164            (iter (with ptr = (foreign-slot-pointer native cstruct-type slot-name))
165                  (with array = (slot-value proxy slot-name))
166                  (for i from 0 below (cstruct-slot-description-count slot))
167                  (setf (aref array i)
168                        (mem-aref ptr (cstruct-slot-description-type slot) i))))
169           ((cstruct-slot-description-inline-p slot)
170            (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot))))
171              (setf (slot-value proxy slot-name) (make-instance (cstruct-inline-slot-description-boxed-type-name slot)))
172              (copy-slots-to-proxy (slot-value proxy slot-name)
173                                   (foreign-slot-pointer native cstruct-type slot-name)
174                                   (g-boxed-cstruct-wrapper-info-cstruct-description info))))
175           (t (setf (slot-value proxy slot-name)
176                    (foreign-slot-value native cstruct-type slot-name))))))
177
178 (defmethod translate-to-foreign (proxy (type boxed-cstruct-foreign-type))
179   (if (null proxy)
180       (null-pointer)
181       (let* ((info (g-boxed-foreign-info type))
182              (native-structure-type (generated-cstruct-name (g-boxed-info-name info))))
183         (with-foreign-object (native-structure native-structure-type)
184           (copy-slots-to-native proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
185           (values (boxed-copy-fn info native-structure) proxy)))))
186
187 (defmethod free-translated-object (native-structure (type boxed-cstruct-foreign-type) proxy)
188   (when proxy
189     (let ((info (g-boxed-foreign-info type)))
190       (copy-slots-to-proxy proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
191       (boxed-free-fn info native-structure))))
192
193 (defmethod translate-from-foreign (native-structure (type boxed-cstruct-foreign-type))
194   (unless (null-pointer-p native-structure)
195     (let* ((info (g-boxed-foreign-info type))
196            (proxy-structure-type (g-boxed-info-name info))
197            (proxy (make-instance proxy-structure-type)))
198       (copy-slots-to-proxy proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
199       (when (g-boxed-foreign-return-p type)
200         (boxed-free-fn info native-structure))
201       proxy)))
202
203 (defmethod cleanup-translated-object-for-callback ((type boxed-cstruct-foreign-type) proxy native-structure)
204   (when proxy
205     (let ((info (g-boxed-foreign-info type)))
206       (copy-slots-to-native proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info)))))
207
208 (eval-when (:compile-toplevel :load-toplevel :execute)
209   (defstruct (g-boxed-opaque-wrapper-info (:include g-boxed-info))
210     alloc free))
211
212 (define-foreign-type boxed-opaque-foreign-type (g-boxed-foreign-type) ())
213
214 (defclass g-boxed-opaque ()
215   ((pointer :initarg :pointer
216             :initform nil
217             :accessor g-boxed-opaque-pointer)))
218
219 (defmethod pointer ((object g-boxed-opaque))
220   (g-boxed-opaque-pointer object))
221
222 (defmethod make-foreign-type ((info g-boxed-opaque-wrapper-info) &key return-p)
223   (make-instance 'boxed-opaque-foreign-type :info info :return-p return-p))
224
225 (defmethod translate-to-foreign (proxy (type boxed-opaque-foreign-type))
226   (prog1 (g-boxed-opaque-pointer proxy)
227     (when (g-boxed-foreign-return-p type)
228       (tg:cancel-finalization proxy)
229       (setf (g-boxed-opaque-pointer proxy) nil))))
230
231 (defmethod free-translated-object (native (type boxed-opaque-foreign-type) param)
232   (declare (ignore native type param)))
233
234 (defun make-boxed-free-finalizer (type pointer)
235   (lambda () (boxed-free-fn type pointer)))
236
237 (defmethod translate-from-foreign (native (foreign-type boxed-opaque-foreign-type))
238   (let* ((type (g-boxed-foreign-info foreign-type))
239          (proxy (make-instance (g-boxed-info-name type) :pointer native)))
240     (tg:finalize proxy (make-boxed-free-finalizer type native))))
241
242 (defmethod cleanup-translated-object-for-callback ((type boxed-opaque-foreign-type) proxy native)
243   (tg:cancel-finalization proxy)
244   (setf (g-boxed-opaque-pointer proxy) nil))
245
246 (defmacro define-g-boxed-opaque (name g-type-name &key
247                                  (alloc (error "Alloc must be specified")))
248   (let ((native-copy (gensym "NATIVE-COPY-"))
249         (instance (gensym "INSTANCE-")))
250     `(progn (defclass ,name (g-boxed-opaque) ())
251             (defmethod initialize-instance :after ((,instance ,name) &key &allow-other-keys)
252               (unless (g-boxed-opaque-pointer ,instance)
253                 (let ((,native-copy ,alloc))
254                   (setf (g-boxed-opaque-pointer ,instance) ,native-copy)
255                   (finalize ,instance (make-boxed-free-finalizer (get ',name 'g-boxed-foreign-info) ,native-copy)))))
256             (eval-when (:compile-toplevel :load-toplevel :execute)
257               (setf (get ',name 'g-boxed-foreign-info)
258                     (make-g-boxed-opaque-wrapper-info :name ',name
259                                                       :g-type ,g-type-name)
260                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
261                     (get ',name 'g-boxed-foreign-info))))))
262
263 (defstruct var-structure
264   name
265   parent
266   slots
267   discriminator-slot
268   variants
269   resulting-cstruct-description)
270
271 (defstruct var-structure-variant
272   discriminating-values
273   structure)
274
275 (defmethod make-load-form ((object var-structure) &optional env)
276   (make-load-form-saving-slots object :environment env))
277
278 (defmethod make-load-form ((object var-structure-variant) &optional env)
279   (make-load-form-saving-slots object :environment env))
280
281 (defun var-struct-all-slots (struct)
282   (when struct
283     (append (var-struct-all-slots (var-structure-parent struct))
284             (var-structure-slots struct))))
285
286 (defun all-structures (structure)
287   (append (iter (for variant in (var-structure-variants structure))
288                 (appending (all-structures (var-structure-variant-structure variant))))
289           (list structure)))
290
291 (defun parse-variant-structure-definition (name slots &optional parent)
292   (iter (with result = (make-var-structure :name name
293                                            :parent parent
294                                            :slots nil
295                                            :discriminator-slot nil
296                                            :variants nil))
297         (for slot in slots)
298         (if (eq :variant (first slot))
299             (progn
300               (when (var-structure-discriminator-slot result)
301                 (error "Structure has more than one discriminator slot"))
302               (setf (var-structure-discriminator-slot result) (second slot)
303                     (var-structure-variants result) (parse-variants result (nthcdr 2 slot))))
304             (push (parse-cstruct-slot slot) (var-structure-slots result)))
305         (finally (setf (var-structure-slots result)
306                        (reverse (var-structure-slots result)))
307                  (unless parent
308                    (set-variant-result-structure result))
309                  (return result))))
310
311 (defun set-variant-result-structure (var-structure)
312   (setf (var-structure-resulting-cstruct-description var-structure)
313         (make-cstruct-description
314          :name
315          (var-structure-name var-structure)
316          :slots
317          (append
318           (when (var-structure-parent var-structure)
319             (cstruct-description-slots (var-structure-resulting-cstruct-description (var-structure-parent var-structure))))
320           (var-structure-slots var-structure))))
321   (iter (for variant in (var-structure-variants var-structure))
322         (for child-var-structure = (var-structure-variant-structure variant))
323         (set-variant-result-structure child-var-structure)))
324
325 (defun ensure-list (thing)
326   (if (listp thing)
327       thing
328       (list thing)))
329
330 (defun parse-variants (parent variants)
331   (iter (for var-descr in variants)
332         (for (options variant-name . slots) in variants)
333         (for variant =
334              (make-var-structure-variant
335               :discriminating-values (ensure-list options)
336               :structure (parse-variant-structure-definition variant-name slots parent)))
337         (collect variant)))
338
339 (defun generated-cstruct-name (symbol)
340   (intern (format nil "~A-CSTRUCT-GENERATED-BY-GOBJECT-BOXED" (symbol-name symbol)) (symbol-package symbol)))
341
342 (defun generated-cunion-name (symbol)
343   (intern (format nil "~A-CUNION-GENERATED-BY-GOBJECT-BOXED" (symbol-name symbol)) (symbol-package symbol)))
344
345 (defun generate-cstruct-1 (struct)
346   `(defcstruct ,(generated-cstruct-name (cstruct-description-name struct))
347      ,@(iter (for slot in (cstruct-description-slots struct))
348              (collect `(,(cstruct-slot-description-name slot) ,(cstruct-slot-description-type slot)
349                          ,@(when (cstruct-slot-description-count slot)
350                                  `(:count ,(cstruct-slot-description-count slot))))))))
351
352 (defun generate-c-structures (structure)
353   (iter (for str in (all-structures structure))
354         (for cstruct = (var-structure-resulting-cstruct-description str))
355         (collect (generate-cstruct-1 cstruct))))
356
357 (defun generate-variant-union (struct)
358   `(defcunion ,(generated-cunion-name (var-structure-name struct))
359      ,@(iter (for str in (all-structures struct))
360              (collect `(,(var-structure-name str)
361                          ,(generated-cstruct-name (var-structure-name str)))))))
362
363 (defun generate-structure-1 (str)
364   `(defstruct ,(if (var-structure-parent str)
365                    `(,(var-structure-name str) (:include ,(var-structure-name (var-structure-parent str))
366                                                          (,(var-structure-discriminator-slot (var-structure-parent str))
367                                                            ,(first (var-structure-variant-discriminating-values
368                                                                     (find str
369                                                                           (var-structure-variants
370                                                                            (var-structure-parent str))
371                                                                           :key #'var-structure-variant-structure))))))
372                    `,(var-structure-name str))
373      ,@(iter (for slot in (var-structure-slots str))
374              (collect `(,(cstruct-slot-description-name slot)
375                          ,(cstruct-slot-description-initform slot))))))
376
377 (defun generate-structures (str)
378   (iter (for variant in (reverse (all-structures str)))
379         (collect (generate-structure-1 variant))))
380
381 (defun generate-native-type-decision-procedure-1 (str proxy-var)
382   (if (null (var-structure-discriminator-slot str))
383       `(values ',(var-structure-resulting-cstruct-description str))
384       `(typecase ,proxy-var
385          ,@(iter (for variant in (var-structure-variants str))
386                  (for v-str = (var-structure-variant-structure variant))
387                  (collect `(,(var-structure-name v-str)
388                              ,(generate-native-type-decision-procedure-1 v-str proxy-var))))
389          (,(var-structure-name str)
390           (values ',(var-structure-resulting-cstruct-description str))))))
391
392 (defun generate-proxy-type-decision-procedure-1 (str native-var)
393   (if (null (var-structure-discriminator-slot str))
394       `(values ',(var-structure-name str)
395                ',(var-structure-resulting-cstruct-description str))
396       `(case (foreign-slot-value ,native-var
397                                  ',(generated-cstruct-name (var-structure-name str))
398                                  ',(var-structure-discriminator-slot str))
399          ,@(iter (for variant in (var-structure-variants str))
400                  (for v-str = (var-structure-variant-structure variant))
401                  (collect `(,(var-structure-variant-discriminating-values variant)
402                              ,(generate-proxy-type-decision-procedure-1
403                                v-str
404                                native-var))))
405          (t (values ',(var-structure-name str)
406                     ',(var-structure-resulting-cstruct-description str))))))
407
408 (defun generate-proxy-type-decision-procedure (str)
409   (let ((native (gensym "NATIVE-")))
410     `(lambda (,native)
411        (declare (ignorable ,native))
412        ,(generate-proxy-type-decision-procedure-1 str native))))
413
414 (defun generate-native-type-decision-procedure (str)
415   (let ((proxy (gensym "PROXY-")))
416     `(lambda (,proxy)
417        (declare (ignorable ,proxy))
418        ,(generate-native-type-decision-procedure-1 str proxy))))
419
420 (defun compile-proxy-type-decision-procedure (str)
421   (compile nil (generate-proxy-type-decision-procedure str)))
422
423 (defun compile-native-type-decision-procedure (str)
424   (compile nil (generate-native-type-decision-procedure str)))
425
426 (defstruct (g-boxed-variant-cstruct-info (:include g-boxed-info))
427   root
428   native-type-decision-procedure
429   proxy-type-decision-procedure)
430
431 (defmethod make-load-form ((object g-boxed-variant-cstruct-info) &optional env)
432   (make-load-form-saving-slots object :environment env))
433
434 (define-foreign-type boxed-variant-cstruct-foreign-type (g-boxed-foreign-type) ())
435
436 (defmethod make-foreign-type ((info g-boxed-variant-cstruct-info) &key return-p)
437   (make-instance 'boxed-variant-cstruct-foreign-type :info info :return-p return-p))
438
439 (defmacro define-g-boxed-variant-cstruct (name g-type-name &body slots)
440   (let* ((structure (parse-variant-structure-definition name slots)))
441     `(progn ,@(generate-c-structures structure)
442             ,(generate-variant-union structure)
443             ,@(generate-structures structure)
444             (eval-when (:compile-toplevel :load-toplevel :execute)
445               (setf (get ',name 'g-boxed-foreign-info)
446                     (make-g-boxed-variant-cstruct-info :name ',name
447                                                        :g-type ,g-type-name
448                                                        :root ,structure
449                                                        :native-type-decision-procedure
450                                                        ,(generate-native-type-decision-procedure structure)
451                                                        :proxy-type-decision-procedure
452                                                        ,(generate-proxy-type-decision-procedure structure))
453                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
454                     (get ',name 'g-boxed-foreign-info))))))
455
456 (defun decide-native-type (info proxy)
457   (funcall (g-boxed-variant-cstruct-info-native-type-decision-procedure info) proxy))
458
459 (defmethod boxed-copy-fn ((info g-boxed-variant-cstruct-info) native)
460   (if (g-boxed-info-g-type info)
461       (g-boxed-copy (g-boxed-info-g-type info) native)
462       (let ((copy (foreign-alloc (generated-cunion-name (g-boxed-info-name info)))))
463         (memcpy copy native (foreign-type-size (generated-cunion-name (g-boxed-info-name info))))
464         copy)))
465
466 (defmethod boxed-free-fn ((info g-boxed-variant-cstruct-info) native)
467   (if (g-boxed-info-g-type info)
468       (g-boxed-free (g-boxed-info-g-type info) native)
469       (foreign-free native)))
470
471 (defmethod translate-to-foreign (proxy (foreign-type boxed-variant-cstruct-foreign-type))
472   (if (null proxy)
473       (null-pointer)
474       (let* ((type (g-boxed-foreign-info foreign-type))
475              (cstruct-description (decide-native-type type proxy)))
476         (with-foreign-object (native-structure (generated-cunion-name
477                                                 (var-structure-name
478                                                  (g-boxed-variant-cstruct-info-root type))))
479           (copy-slots-to-native proxy native-structure cstruct-description)
480           (values (boxed-copy-fn type native-structure) proxy)))))
481
482 (defun decide-proxy-type (info native-structure)
483   (funcall (g-boxed-variant-cstruct-info-proxy-type-decision-procedure info) native-structure))
484
485 (defmethod free-translated-object (native (foreign-type boxed-variant-cstruct-foreign-type) proxy)
486   (when proxy
487     (let ((type (g-boxed-foreign-info foreign-type)))
488       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
489         (unless (eq (type-of proxy) actual-struct)
490           (restart-case
491               (error "Expected type of boxed variant structure ~A and actual type ~A do not match"
492                      (type-of proxy) actual-struct)
493             (skip-parsing-values () (return-from free-translated-object))))
494         (copy-slots-to-proxy proxy native cstruct-description)
495         (boxed-free-fn type native)))))
496
497 (defmethod translate-from-foreign (native (foreign-type boxed-variant-cstruct-foreign-type))
498   (unless (null-pointer-p native)
499     (let ((type (g-boxed-foreign-info foreign-type)))
500       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
501         (let ((proxy (make-instance actual-struct)))
502           (copy-slots-to-proxy proxy native cstruct-description)
503           (when (g-boxed-foreign-return-p foreign-type)
504             (boxed-free-fn type native))
505           proxy)))))
506
507 (defmethod cleanup-translated-object-for-callback ((foreign-type boxed-variant-cstruct-foreign-type) proxy native)
508   (when proxy
509     (let ((type (g-boxed-foreign-info foreign-type)))
510       (let ((cstruct-description (decide-native-type type proxy)))
511         (copy-slots-to-native proxy native cstruct-description)))))
512
513 (defgeneric boxed-parse-g-value (gvalue-ptr info))
514
515 (defgeneric boxed-set-g-value (gvalue-ptr info proxy))
516
517 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) parse-kind)
518   (declare (ignore parse-kind))
519   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
520       (convert-from-foreign (g-value-get-boxed gvalue-ptr) '(glib:gstrv :free-from-foreign nil))
521       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
522         (boxed-parse-g-value gvalue-ptr boxed-type))))
523
524 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) value)
525   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
526       (g-value-set-boxed gvalue-ptr (convert-to-foreign value '(glib:gstrv :free-from-foreign nil)))
527       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
528         (boxed-set-g-value gvalue-ptr boxed-type value))))
529
530 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info))
531   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
532
533 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info) proxy)
534   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
535
536 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info))
537   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
538
539 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info) proxy)
540   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
541
542 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info))
543   (translate-from-foreign (boxed-copy-fn info (g-value-get-boxed gvalue-ptr)) (make-foreign-type info :return-p nil)))
544
545 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info) proxy)
546   (g-value-set-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
547
548 (defun boxed-related-symbols (name)
549   (let ((info (get-g-boxed-foreign-info name)))
550     (etypecase info
551       (g-boxed-cstruct-wrapper-info
552        (append (list name
553                      (intern (format nil "MAKE-~A" (symbol-name name)))
554                      (intern (format nil "COPY-~A" (symbol-name name))))
555                (iter (for slot in (cstruct-description-slots (g-boxed-cstruct-wrapper-info-cstruct-description info)))
556                      (for slot-name = (cstruct-slot-description-name slot))
557                      (collect (intern (format nil "~A-~A" (symbol-name name) (symbol-name slot-name)))))))
558       (g-boxed-opaque-wrapper-info
559        (list name))
560       (g-boxed-variant-cstruct-info
561        (append (list name)
562                (iter (for var-struct in (all-structures (g-boxed-variant-cstruct-info-root info)))
563                      (for s-name = (var-structure-name var-struct))
564                      (for cstruct-description = (var-structure-resulting-cstruct-description var-struct))
565                      (appending (append (list (intern (format nil "MAKE-~A" (symbol-name s-name)))
566                                               (intern (format nil "COPY-~A" (symbol-name s-name))))
567                                         (iter (for slot in (cstruct-description-slots cstruct-description))
568                                               (for slot-name = (cstruct-slot-description-name slot))
569                                               (collect (intern (format nil "~A-~A" (symbol-name s-name)
570                                                                        (symbol-name slot-name)))))))))))))
571
572 (defmacro define-boxed-opaque-accessor (boxed-name accessor-name &key type reader writer)
573   (let ((var (gensym))
574         (n-var (gensym)))
575     `(progn ,@(when reader
576                     (list (etypecase reader
577                             (symbol `(defun ,accessor-name (,var)
578                                        (funcall ,reader ,var)))
579                             (string `(defcfun (,accessor-name ,reader) ,type
580                                        (,var (g-boxed-foreign ,boxed-name)))))))
581             ,@(when writer
582                     (list (etypecase reader
583                             (symbol `(defun (setf ,accessor-name) (,n-var ,var)
584                                        (funcall ,reader ,n-var ,var)))
585                             (string `(defun (setf ,accessor-name) (,n-var ,var)
586                                        (foreign-funcall ,writer (g-boxed-foreign ,boxed-name) ,var ,type ,n-var :void)))))))))