3 (defclass gobject-class (standard-class)
4 ((g-type-name :initform (error "G-TYPE-NAME must be specified")
6 :reader gobject-class-g-type-name)
7 (g-type-initializer :initform nil
8 :initarg :g-type-initializer
9 :reader gobject-class-g-type-initializer)
10 (interface-p :initform nil
11 :initarg :g-interface-p
12 :reader gobject-class-interface-p))
13 (:documentation "Metaclass for GObject-based classes."))
15 (defun initialize-gobject-class-g-type (class)
16 (if (gobject-class-g-type-initializer class)
17 (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class)))
18 (type (when initializer-fn-ptr
19 (foreign-funcall-pointer initializer-fn-ptr nil
21 (if (null initializer-fn-ptr)
22 (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'"
23 (gobject-class-g-type-name class) (class-name class) (gobject-class-g-type-initializer class))
25 (when (g-type= +g-type-invalid+ type)
26 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
27 (gobject-class-g-type-name class) (class-name class)
28 (gobject-class-g-type-initializer class)))
29 (unless (g-type= (gobject-class-g-type-name class) type)
30 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
31 (gobject-class-g-type-name class)
33 (g-type-name type))))))
34 (unless (g-type-from-name (gobject-class-g-type-name class))
35 (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
36 (gobject-class-g-type-name class) (class-name class)))))
38 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
39 (register-object-type (gobject-class-g-type-name object) (class-name object))
40 (at-init (object) (initialize-gobject-class-g-type object)))
42 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
43 ((g-property-type :initform nil
44 :initarg :g-property-type
45 :reader gobject-direct-slot-definition-g-property-type)))
47 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
48 ((g-property-type :initform nil
49 :initarg :g-property-type
50 :accessor gobject-effective-slot-definition-g-property-type)))
52 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
53 ((g-property-name :initform nil
54 :initarg :g-property-name
55 :reader gobject-property-direct-slot-definition-g-property-name)))
57 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
58 ((g-property-name :initform nil
59 :initarg :g-property-name
60 :accessor gobject-property-effective-slot-definition-g-property-name)))
62 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
63 ((g-getter-name :initform nil
65 :reader gobject-fn-direct-slot-definition-g-getter-name)
66 (g-setter-name :initform nil
68 :reader gobject-fn-direct-slot-definition-g-setter-name)))
70 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
71 ((g-getter-name :initform nil
73 :accessor gobject-fn-effective-slot-definition-g-getter-name)
74 (g-setter-name :initform nil
76 :accessor gobject-fn-effective-slot-definition-g-setter-name)
77 (g-getter-fn :initform nil
78 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
79 (g-setter-fn :initform nil
80 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
82 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
85 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
88 (defmethod compute-class-precedence-list ((class gobject-class))
89 (let ((classes (call-next-method)))
90 (if (member (find-class 'g-object) classes)
92 `(,class ,(find-class 'g-object) ,@(rest classes)))))
94 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
95 (declare (ignore initargs))
97 (:gobject-property 'gobject-property-direct-slot-definition)
98 (:gobject-fn 'gobject-fn-direct-slot-definition)
99 (otherwise (call-next-method))))
101 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
102 (declare (ignore initargs))
104 (:gobject-property 'gobject-property-effective-slot-definition)
105 (:gobject-fn 'gobject-fn-effective-slot-definition)
106 (otherwise (call-next-method))))
108 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
109 (let ((effective-slot (call-next-method)))
110 (when (typep effective-slot 'gobject-effective-slot-definition)
111 (let ((allocation (loop
112 for direct-slot in direct-slots
113 when (slot-definition-allocation direct-slot)
114 return (slot-definition-allocation direct-slot)))
116 for direct-slot in direct-slots
117 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
118 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
120 for direct-slot in direct-slots
121 when (gobject-direct-slot-definition-g-property-type direct-slot)
122 return (gobject-direct-slot-definition-g-property-type direct-slot)))
123 (property-getter (loop
124 for direct-slot in direct-slots
125 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
126 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
127 (property-setter (loop
128 for direct-slot in direct-slots
129 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
130 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
131 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
132 (gobject-effective-slot-definition-g-property-type effective-slot))
134 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
135 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
137 (: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"
138 name (class-name class))
139 (when (or (and property-getter (stringp property-getter))
140 (and property-setter (stringp property-setter)))
141 (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)))
143 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
144 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
145 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
147 (if (stringp property-getter)
148 (compile nil `(lambda (object)
149 (foreign-funcall ,property-getter
153 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
155 (if (stringp property-setter)
156 (compile nil `(lambda (object new-value)
157 (foreign-funcall ,property-setter
159 ,property-type new-value
161 property-setter)))))))
164 (defun create-gobject-from-class-and-initargs (class initargs)
165 (when (gobject-class-interface-p class)
166 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
167 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
168 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
170 for (arg-name arg-value) on initargs by #'cddr
171 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
172 when (and slot (typep slot 'gobject-effective-slot-definition))
174 (gobject-property-effective-slot-definition
175 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
176 (push arg-value arg-values)
177 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
178 (gobject-fn-effective-slot-definition
179 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
180 (push arg-value nc-arg-values))))
181 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
184 for value in nc-arg-values
185 do (funcall fn object value))
188 (defun filter-initargs-by-class (class initargs)
189 (iter (with slots = (class-slots class))
190 (for (arg-name arg-value) on initargs by #'cddr)
191 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
192 (unless (and slot (typep slot 'gobject-effective-slot-definition))
193 (nconcing (list arg-name arg-value)))))
195 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
196 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
197 (apply #'call-next-method instance filtered-initargs)))
199 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
202 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
204 (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
205 (apply #'call-next-method class :pointer pointer initargs))))
207 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
209 (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
210 (property-unreadable-error () nil)))
212 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
213 (g-object-call-get-property (pointer object)
214 (gobject-property-effective-slot-definition-g-property-name slot)
215 (gobject-effective-slot-definition-g-property-type slot)))
217 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
218 (g-object-call-set-property (pointer object)
219 (gobject-property-effective-slot-definition-g-property-name slot)
221 (gobject-effective-slot-definition-g-property-type slot)))
223 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
224 (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot))))
226 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
227 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
228 (funcall fn object)))
230 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
231 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
233 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
236 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))