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
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
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))
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)
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 (defun filter-from-initargs (initargs removed-key)
42 for (key value) on initargs by #'cddr
43 unless (eq key removed-key)
44 collect key and collect value))
46 (defun initargs-have-base-in-superclass (initargs base-class)
47 (let ((d-s (getf initargs :direct-superclasses)))
50 thereis (subtypep class base-class))))
52 (defun compute-new-initargs-for-metaclass (initargs base-class)
53 (if (initargs-have-base-in-superclass initargs base-class)
55 (append (filter-from-initargs initargs :direct-superclasses)
56 (list :direct-superclasses
57 (append (getf initargs :direct-superclasses)
58 (list (find-class base-class)))))))
60 (defmethod initialize-instance :around ((class gobject-class) &rest initargs)
61 (apply #'call-next-method class (compute-new-initargs-for-metaclass initargs 'g-object)))
63 (defmethod reinitialize-instance :around ((class gobject-class) &rest initargs &key (direct-superclasses nil d-s-p) &allow-other-keys)
64 (declare (ignore direct-superclasses))
67 (apply #'call-next-method class (compute-new-initargs-for-metaclass initargs 'g-object))))
69 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
70 (when (gobject-class-direct-g-type-name object)
71 (register-object-type (gobject-class-direct-g-type-name object) (class-name object))
72 (at-init (object) (initialize-gobject-class-g-type object))))
74 (defmethod finalize-inheritance :after ((class gobject-class))
75 (setf (gobject-class-g-type-name class)
76 (or (gobject-class-direct-g-type-name class)
77 (let ((gobject-superclass (iter (for superclass in (class-direct-superclasses class))
78 (finding superclass such-that (typep superclass 'gobject-class)))))
79 (assert gobject-superclass)
80 (gobject-class-g-type-name gobject-superclass)))))
82 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
83 ((g-property-type :initform nil
84 :initarg :g-property-type
85 :reader gobject-direct-slot-definition-g-property-type)))
87 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
88 ((g-property-type :initform nil
89 :initarg :g-property-type
90 :accessor gobject-effective-slot-definition-g-property-type)))
92 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
93 ((g-property-name :initform nil
94 :initarg :g-property-name
95 :reader gobject-property-direct-slot-definition-g-property-name)))
97 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
98 ((g-property-name :initform nil
99 :initarg :g-property-name
100 :accessor gobject-property-effective-slot-definition-g-property-name)))
102 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
103 ((g-getter-name :initform nil
105 :reader gobject-fn-direct-slot-definition-g-getter-name)
106 (g-setter-name :initform nil
108 :reader gobject-fn-direct-slot-definition-g-setter-name)))
110 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
111 ((g-getter-name :initform nil
113 :accessor gobject-fn-effective-slot-definition-g-getter-name)
114 (g-setter-name :initform nil
116 :accessor gobject-fn-effective-slot-definition-g-setter-name)
117 (g-getter-fn :initform nil
118 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
119 (g-setter-fn :initform nil
120 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
122 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
125 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
126 (declare (ignore initargs))
128 (:gobject-property 'gobject-property-direct-slot-definition)
129 (:gobject-fn 'gobject-fn-direct-slot-definition)
130 (otherwise (call-next-method))))
132 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
133 (declare (ignore initargs))
135 (:gobject-property 'gobject-property-effective-slot-definition)
136 (:gobject-fn 'gobject-fn-effective-slot-definition)
137 (otherwise (call-next-method))))
139 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
140 (let ((effective-slot (call-next-method)))
141 (when (typep effective-slot 'gobject-effective-slot-definition)
142 (let ((allocation (loop
143 for direct-slot in direct-slots
144 when (slot-definition-allocation direct-slot)
145 return (slot-definition-allocation direct-slot)))
147 for direct-slot in direct-slots
148 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
149 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
151 for direct-slot in direct-slots
152 when (gobject-direct-slot-definition-g-property-type direct-slot)
153 return (gobject-direct-slot-definition-g-property-type direct-slot)))
154 (property-getter (loop
155 for direct-slot in direct-slots
156 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
157 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
158 (property-setter (loop
159 for direct-slot in direct-slots
160 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
161 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
162 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
163 (gobject-effective-slot-definition-g-property-type effective-slot))
165 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
166 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
168 (: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"
169 name (class-name class))
170 (when (or (and property-getter (stringp property-getter))
171 (and property-setter (stringp property-setter)))
172 (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)))
174 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
175 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
176 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
178 (if (stringp property-getter)
179 (if (foreign-symbol-pointer property-getter)
180 (compile nil `(lambda (object)
181 (foreign-funcall ,property-getter
185 (warn "Property reader function '~A' has not been found" property-getter)
187 (declare (ignore object))
188 (error "Property reader function '~A' has not been found" property-getter))))
190 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
192 (if (stringp property-setter)
193 (if (foreign-symbol-pointer property-setter)
194 (compile nil `(lambda (object new-value)
195 (foreign-funcall ,property-setter
197 ,property-type new-value
200 (warn "Property writer function '~A' has not been found" property-setter)
202 (declare (ignore object))
203 (error "Property writer function '~A' has not been found" property-setter))))
204 property-setter)))))))
207 (defun create-gobject-from-class-and-initargs (class initargs)
208 (when (gobject-class-interface-p class)
209 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
210 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
211 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
213 for (arg-name arg-value) on initargs by #'cddr
214 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
215 when (and slot (typep slot 'gobject-effective-slot-definition))
217 (gobject-property-effective-slot-definition
218 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
219 (push arg-value arg-values)
220 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
221 (gobject-fn-effective-slot-definition
222 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
223 (push arg-value nc-arg-values))))
224 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
227 for value in nc-arg-values
228 do (funcall fn object value))
231 (defun filter-initargs-by-class (class initargs)
232 (iter (with slots = (class-slots class))
233 (for (arg-name arg-value) on initargs by #'cddr)
234 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
235 (unless (and slot (typep slot 'gobject-effective-slot-definition))
236 (nconcing (list arg-name arg-value)))))
238 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
239 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
240 (apply #'call-next-method instance filtered-initargs)))
242 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
243 (log-for :subclass "(make-instance ~A ~{~A~^ ~})~%" class initargs)
244 (let ((*currently-making-object-p* t))
247 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
249 (let* ((default-initargs (iter (for (arg value) in (class-default-initargs class))
250 (nconcing (list arg value))))
251 (effective-initargs (append initargs default-initargs))
252 (pointer (create-gobject-from-class-and-initargs class effective-initargs)))
253 (apply #'call-next-method class :pointer pointer effective-initargs)))))
255 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
257 (and (slot-boundp object 'pointer)
259 (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t))
260 (property-unreadable-error () nil)))
262 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
263 (g-object-call-get-property (pointer object)
264 (gobject-property-effective-slot-definition-g-property-name slot)
265 (gobject-effective-slot-definition-g-property-type slot)))
267 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
268 (g-object-call-set-property (pointer object)
269 (gobject-property-effective-slot-definition-g-property-name slot)
271 (gobject-effective-slot-definition-g-property-type slot))
274 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
275 (and (slot-boundp object 'pointer)
277 (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot)))))
279 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
280 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
281 (funcall fn object)))
283 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
284 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value)
287 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
288 (slot-boundp object 'pointer))
290 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
291 (declare (ignore object))