3 (define-foreign-type glist-type ()
4 ((type :reader glist-type-type :initarg :type :initform :pointer)
5 (free-from-foreign :reader glist-type-free-from-foreign :initarg :free-from-foreign :initform t)
6 (free-to-foreign :reader glist-type-free-to-foreign :initarg :free-to-foreign :initform t))
7 (:actual-type :pointer))
9 (define-parse-method glist (type &key (free-from-foreign t) (free-to-foreign t))
10 (make-instance 'glist-type
12 :free-from-foreign free-from-foreign
13 :free-to-foreign free-to-foreign))
19 (defctype g-list (:struct g-list))
21 (defcfun g-list-first (:pointer g-list) (list (:pointer g-list)))
23 (defcfun g-list-free :void (list (:pointer g-list)))
25 (defun g-list-next (list)
26 (if (null-pointer-p list)
28 (foreign-slot-value list 'g-list 'next)))
30 (defmethod translate-from-foreign (pointer (type glist-type))
32 (iter (for c initially pointer then (g-list-next c))
33 (until (null-pointer-p c))
34 (collect (convert-from-foreign (foreign-slot-value c 'g-list 'data) (glist-type-type type))))
35 (when (glist-type-free-from-foreign type)
36 (g-list-free pointer))))
39 (define-foreign-type gslist-type ()
40 ((type :reader gslist-type-type :initarg :type :initform :pointer)
41 (free-from-foreign :reader gslist-type-free-from-foreign :initarg :free-from-foreign :initform t)
42 (free-to-foreign :reader gslist-type-free-to-foreign :initarg :free-to-foreign :initform t))
43 (:actual-type :pointer))
45 (define-parse-method gslist (type &key (free-from-foreign t) (free-to-foreign t))
46 (make-instance 'gslist-type
48 :free-from-foreign free-from-foreign
49 :free-to-foreign free-to-foreign))
54 (defctype g-slist (:struct g-slist))
56 (defcfun g-slist-alloc (:pointer g-slist))
58 (defcfun g-slist-free :void (list (:pointer g-slist)))
60 (defun g-slist-next (list)
61 (if (null-pointer-p list)
63 (foreign-slot-value list 'g-slist 'next)))
65 (defmethod translate-from-foreign (pointer (type gslist-type))
67 (iter (for c initially pointer then (g-slist-next c))
68 (until (null-pointer-p c))
69 (collect (convert-from-foreign (foreign-slot-value c 'g-slist 'data) (gslist-type-type type))))
70 (when (gslist-type-free-from-foreign type)
71 (g-slist-free pointer))))
73 (defmethod translate-to-foreign (list (type gslist-type))
74 (let ((result (null-pointer)) last)
75 (iter (for item in list)
76 (for n = (g-slist-alloc))
77 (for ptr = (convert-to-foreign item (gslist-type-type type)))
78 (setf (foreign-slot-value n 'g-slist 'data) ptr)
79 (setf (foreign-slot-value n 'g-slist 'next) (null-pointer))
81 (setf (foreign-slot-value last 'g-slist 'next) n))
83 (when (first-iteration-p)