Support for gobject:release method for GObjects
[cl-gtk2.git] / glib / gobject.meta.lisp
blobd7cb8c7195daf8f2e1e685c89241079fdc3b1747
1 (in-package :gobject)
3 (defclass gobject-class (standard-class)
4 ((g-type-name :initform nil
5 :accessor gobject-class-g-type-name)
6 (direct-g-type-name :initform nil
7 :initarg :g-type-name
8 :accessor gobject-class-direct-g-type-name)
9 (g-type-initializer :initform nil
10 :initarg :g-type-initializer
11 :reader gobject-class-g-type-initializer)
12 (interface-p :initform nil
13 :initarg :g-interface-p
14 :reader gobject-class-interface-p))
15 (:documentation "Metaclass for GObject-based classes."))
17 (defun initialize-gobject-class-g-type (class)
18 (if (gobject-class-g-type-initializer class)
19 (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class)))
20 (type (when initializer-fn-ptr
21 (foreign-funcall-pointer initializer-fn-ptr nil
22 g-type))))
23 (if (null initializer-fn-ptr)
24 (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'"
25 (gobject-class-direct-g-type-name class) (class-name class) (gobject-class-g-type-initializer class))
26 (progn
27 (when (g-type= +g-type-invalid+ type)
28 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
29 (gobject-class-direct-g-type-name class) (class-name class)
30 (gobject-class-g-type-initializer class)))
31 (unless (g-type= (gobject-class-direct-g-type-name class) type)
32 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
33 (gobject-class-direct-g-type-name class)
34 (class-name class)
35 (g-type-name type))))))
36 (unless (g-type-from-name (gobject-class-direct-g-type-name class))
37 (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
38 (gobject-class-direct-g-type-name class) (class-name class)))))
40 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
41 (when (gobject-class-direct-g-type-name object)
42 (register-object-type (gobject-class-direct-g-type-name object) (class-name object))
43 (at-init (object) (initialize-gobject-class-g-type object))))
45 (defmethod finalize-inheritance :after ((class gobject-class))
46 (setf (gobject-class-g-type-name class)
47 (or (gobject-class-direct-g-type-name class)
48 (let ((gobject-superclass (iter (for superclass in (class-direct-superclasses class))
49 (finding superclass such-that (typep superclass 'gobject-class)))))
50 (assert gobject-superclass)
51 (gobject-class-g-type-name gobject-superclass)))))
53 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
54 ((g-property-type :initform nil
55 :initarg :g-property-type
56 :reader gobject-direct-slot-definition-g-property-type)))
58 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
59 ((g-property-type :initform nil
60 :initarg :g-property-type
61 :accessor gobject-effective-slot-definition-g-property-type)))
63 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
64 ((g-property-name :initform nil
65 :initarg :g-property-name
66 :reader gobject-property-direct-slot-definition-g-property-name)))
68 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
69 ((g-property-name :initform nil
70 :initarg :g-property-name
71 :accessor gobject-property-effective-slot-definition-g-property-name)))
73 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
74 ((g-getter-name :initform nil
75 :initarg :g-getter
76 :reader gobject-fn-direct-slot-definition-g-getter-name)
77 (g-setter-name :initform nil
78 :initarg :g-setter
79 :reader gobject-fn-direct-slot-definition-g-setter-name)))
81 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
82 ((g-getter-name :initform nil
83 :initarg :g-getter
84 :accessor gobject-fn-effective-slot-definition-g-getter-name)
85 (g-setter-name :initform nil
86 :initarg :g-setter
87 :accessor gobject-fn-effective-slot-definition-g-setter-name)
88 (g-getter-fn :initform nil
89 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
90 (g-setter-fn :initform nil
91 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
93 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
96 (defmethod compute-class-precedence-list ((class gobject-class))
97 (let ((classes (call-next-method)))
98 (if (member (find-class 'g-object) classes)
99 classes
100 `(,class ,(find-class 'g-object) ,@(rest classes)))))
102 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
103 (declare (ignore initargs))
104 (case allocation
105 (:gobject-property 'gobject-property-direct-slot-definition)
106 (:gobject-fn 'gobject-fn-direct-slot-definition)
107 (otherwise (call-next-method))))
109 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
110 (declare (ignore initargs))
111 (case allocation
112 (:gobject-property 'gobject-property-effective-slot-definition)
113 (:gobject-fn 'gobject-fn-effective-slot-definition)
114 (otherwise (call-next-method))))
116 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
117 (let ((effective-slot (call-next-method)))
118 (when (typep effective-slot 'gobject-effective-slot-definition)
119 (let ((allocation (loop
120 for direct-slot in direct-slots
121 when (slot-definition-allocation direct-slot)
122 return (slot-definition-allocation direct-slot)))
123 (property-name (loop
124 for direct-slot in direct-slots
125 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
126 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
127 (property-type (loop
128 for direct-slot in direct-slots
129 when (gobject-direct-slot-definition-g-property-type direct-slot)
130 return (gobject-direct-slot-definition-g-property-type direct-slot)))
131 (property-getter (loop
132 for direct-slot in direct-slots
133 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
134 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
135 (property-setter (loop
136 for direct-slot in direct-slots
137 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
138 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
139 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
140 (gobject-effective-slot-definition-g-property-type effective-slot))
141 (ecase allocation
142 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
143 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
144 property-name))
145 (:gobject-fn (assert (or property-getter property-setter) nil "At least one of G-PROPERTY-GETTER or G-PROPERTY-SETTER for slot ~A on class ~A must be specified"
146 name (class-name class))
147 (when (or (and property-getter (stringp property-getter))
148 (and property-setter (stringp property-setter)))
149 (assert property-type nil "G-PROPERTY-TYPE for slot ~A on class ~A must be specified because at least one of accessor is specified as a foreign function" name (class-name class)))
151 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
152 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
153 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
154 (and property-getter
155 (if (stringp property-getter)
156 (compile nil `(lambda (object)
157 (foreign-funcall ,property-getter
158 g-object object
159 ,property-type)))
160 property-getter))
161 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
162 (and property-setter
163 (if (stringp property-setter)
164 (compile nil `(lambda (object new-value)
165 (foreign-funcall ,property-setter
166 g-object object
167 ,property-type new-value
168 :void)))
169 property-setter)))))))
170 effective-slot))
172 (defun create-gobject-from-class-and-initargs (class initargs)
173 (when (gobject-class-interface-p class)
174 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
175 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
176 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
177 (loop
178 for (arg-name arg-value) on initargs by #'cddr
179 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
180 when (and slot (typep slot 'gobject-effective-slot-definition))
181 do (typecase slot
182 (gobject-property-effective-slot-definition
183 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
184 (push arg-value arg-values)
185 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
186 (gobject-fn-effective-slot-definition
187 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
188 (push arg-value nc-arg-values))))
189 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
190 (loop
191 for fn in nc-setters
192 for value in nc-arg-values
193 do (funcall fn object value))
194 object)))
196 (defun filter-initargs-by-class (class initargs)
197 (iter (with slots = (class-slots class))
198 (for (arg-name arg-value) on initargs by #'cddr)
199 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
200 (unless (and slot (typep slot 'gobject-effective-slot-definition))
201 (nconcing (list arg-name arg-value)))))
203 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
204 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
205 (apply #'call-next-method instance filtered-initargs)))
207 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
208 (log-for :subclass "(make-instance ~A ~{~A~^ ~})~%" class initargs)
209 (let ((*currently-making-object-p* t))
210 (if pointer
211 (progn
212 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
213 (call-next-method))
214 (let* ((default-initargs (iter (for (arg value) in (class-default-initargs class))
215 (nconcing (list arg value))))
216 (effective-initargs (append initargs default-initargs))
217 (pointer (create-gobject-from-class-and-initargs class effective-initargs)))
218 (apply #'call-next-method class :pointer pointer effective-initargs)))))
220 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
221 (handler-case
222 (and (pointer object)
223 (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t))
224 (property-unreadable-error () nil)))
226 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
227 (g-object-call-get-property (pointer object)
228 (gobject-property-effective-slot-definition-g-property-name slot)
229 (gobject-effective-slot-definition-g-property-type slot)))
231 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
232 (g-object-call-set-property (pointer object)
233 (gobject-property-effective-slot-definition-g-property-name slot)
234 new-value
235 (gobject-effective-slot-definition-g-property-type slot))
236 new-value)
238 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
239 (and (pointer object)
240 (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot)))))
242 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
243 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
244 (funcall fn object)))
246 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
247 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value)
248 new-value)
250 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
253 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
254 nil)