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
)
208 (log-for :subclass
"(make-instance ~A ~{~A~^ ~})~%" class initargs
)
209 (let ((*currently-making-object-p
* t
))
212 (assert (= (length initargs
) 2) nil
"POINTER can not be combined with other initargs (~A)" initargs
)
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
))
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
)
235 (gobject-effective-slot-definition-g-property-type slot
))
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
)
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
))