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 (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
76 :reader gobject-fn-direct-slot-definition-g-getter-name)
77 (g-setter-name :initform nil
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
84 :accessor gobject-fn-effective-slot-definition-g-getter-name)
85 (g-setter-name :initform nil
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)
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))
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))
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)))
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)))
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))
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)
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)
155 (if (stringp property-getter)
156 (compile nil `(lambda (object)
157 (foreign-funcall ,property-getter
161 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
163 (if (stringp property-setter)
164 (compile nil `(lambda (object new-value)
165 (foreign-funcall ,property-setter
167 ,property-type new-value
169 property-setter)))))))
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))
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))
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)))
192 for value in nc-arg-values
193 do (funcall fn object value))
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)
210 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
212 (let* ((default-initargs (iter (for (arg value) in (class-default-initargs class))
213 (nconcing (list arg value))))
214 (effective-initargs (append initargs default-initargs))
215 (pointer (create-gobject-from-class-and-initargs class effective-initargs)))
216 (apply #'call-next-method class :pointer pointer effective-initargs))))
218 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
220 (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
221 (property-unreadable-error () nil)))
223 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
224 (g-object-call-get-property (pointer object)
225 (gobject-property-effective-slot-definition-g-property-name slot)
226 (gobject-effective-slot-definition-g-property-type slot)))
228 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
229 (g-object-call-set-property (pointer object)
230 (gobject-property-effective-slot-definition-g-property-name slot)
232 (gobject-effective-slot-definition-g-property-type slot)))
234 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
235 (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot))))
237 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
238 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
239 (funcall fn object)))
241 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
242 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
244 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
247 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))