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-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 (string= (gobject-class-g-type-name class)
31 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
32 (gobject-class-g-type-name class)
34 (g-type-name type))))))
35 (unless (g-type-from-name (gobject-class-g-type-name class))
36 (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
37 (gobject-class-g-type-name class) (class-name class)))))
39 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
40 (register-object-type (gobject-class-g-type-name object) (class-name object))
41 (at-init (object) (initialize-gobject-class-g-type object)))
43 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
44 ((g-property-type :initform nil
45 :initarg :g-property-type
46 :reader gobject-direct-slot-definition-g-property-type)))
48 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
49 ((g-property-type :initform nil
50 :initarg :g-property-type
51 :accessor gobject-effective-slot-definition-g-property-type)))
53 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
54 ((g-property-name :initform nil
55 :initarg :g-property-name
56 :reader gobject-property-direct-slot-definition-g-property-name)))
58 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
59 ((g-property-name :initform nil
60 :initarg :g-property-name
61 :accessor gobject-property-effective-slot-definition-g-property-name)))
63 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
64 ((g-getter-name :initform nil
66 :reader gobject-fn-direct-slot-definition-g-getter-name)
67 (g-setter-name :initform nil
69 :reader gobject-fn-direct-slot-definition-g-setter-name)))
71 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
72 ((g-getter-name :initform nil
74 :accessor gobject-fn-effective-slot-definition-g-getter-name)
75 (g-setter-name :initform nil
77 :accessor gobject-fn-effective-slot-definition-g-setter-name)
78 (g-getter-fn :initform nil
79 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
80 (g-setter-fn :initform nil
81 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
83 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
86 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
89 (defmethod compute-class-precedence-list ((class gobject-class))
90 (let ((classes (call-next-method)))
91 (if (member (find-class 'g-object) classes)
93 `(,class ,(find-class 'g-object) ,@(rest classes)))))
95 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
96 (declare (ignore initargs))
98 (:gobject-property 'gobject-property-direct-slot-definition)
99 (:gobject-fn 'gobject-fn-direct-slot-definition)
100 (otherwise (call-next-method))))
102 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
103 (declare (ignore initargs))
105 (:gobject-property 'gobject-property-effective-slot-definition)
106 (:gobject-fn 'gobject-fn-effective-slot-definition)
107 (otherwise (call-next-method))))
109 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
110 (let ((effective-slot (call-next-method)))
111 (when (typep effective-slot 'gobject-effective-slot-definition)
112 (let ((allocation (loop
113 for direct-slot in direct-slots
114 when (slot-definition-allocation direct-slot)
115 return (slot-definition-allocation direct-slot)))
117 for direct-slot in direct-slots
118 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
119 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
121 for direct-slot in direct-slots
122 when (gobject-direct-slot-definition-g-property-type direct-slot)
123 return (gobject-direct-slot-definition-g-property-type direct-slot)))
124 (property-getter (loop
125 for direct-slot in direct-slots
126 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
127 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
128 (property-setter (loop
129 for direct-slot in direct-slots
130 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
131 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
132 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
133 (gobject-effective-slot-definition-g-property-type effective-slot))
135 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
136 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
138 (: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"
139 name (class-name class))
140 (when (or (and property-getter (stringp property-getter))
141 (and property-setter (stringp property-setter)))
142 (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)))
144 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
145 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
146 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
148 (if (stringp property-getter)
149 (compile nil `(lambda (object)
150 (foreign-funcall ,property-getter
154 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
156 (if (stringp property-setter)
157 (compile nil `(lambda (object new-value)
158 (foreign-funcall ,property-setter
160 ,property-type new-value
162 property-setter)))))))
165 (defun create-gobject-from-class-and-initargs (class initargs)
166 (when (gobject-class-interface-p class)
167 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
168 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
169 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
171 for (arg-name arg-value) on initargs by #'cddr
172 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
173 when (and slot (typep slot 'gobject-effective-slot-definition))
175 (gobject-property-effective-slot-definition
176 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
177 (push arg-value arg-values)
178 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
179 (gobject-fn-effective-slot-definition
180 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
181 (push arg-value nc-arg-values))))
182 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
185 for value in nc-arg-values
186 do (funcall fn object value))
189 (defun filter-initargs-by-class (class initargs)
190 (iter (with slots = (class-slots class))
191 (for (arg-name arg-value) on initargs by #'cddr)
192 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
193 (unless (and slot (typep slot 'gobject-effective-slot-definition))
194 (nconcing (list arg-name arg-value)))))
196 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
197 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
198 (apply #'call-next-method instance filtered-initargs)))
200 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
203 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
205 (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
206 (apply #'call-next-method class :pointer pointer initargs))))
208 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
210 (progn (g-object-property-type object (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
211 (property-unreadable-error () nil)))
213 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
214 (g-object-call-get-property object
215 (gobject-property-effective-slot-definition-g-property-name slot)
216 (gobject-effective-slot-definition-g-property-type slot)))
218 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
219 (g-object-call-set-property object
220 (gobject-property-effective-slot-definition-g-property-name slot)
222 (gobject-effective-slot-definition-g-property-type slot)))
224 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
225 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
227 (funcall fn object))))
229 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
230 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
232 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
235 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))