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)))
14 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
15 (register-object-type (gobject-class-g-type-name object) (class-name object))
16 (if (gobject-class-g-type-initializer object)
17 (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer object)))
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 object) (class-name object) (gobject-class-g-type-initializer object))
26 (when (= +g-type-invalid+ type)
27 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
28 (gobject-class-g-type-name object) (class-name object)
29 (gobject-class-g-type-initializer object)))
30 (unless (string= (gobject-class-g-type-name object)
32 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
33 (gobject-class-g-type-name object)
35 (g-type-name type))))))
36 (unless (g-type-from-name (gobject-class-g-type-name object))
37 (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
38 (gobject-class-g-type-name object) (class-name object)))))
40 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
41 ((g-property-type :initform nil
42 :initarg :g-property-type
43 :reader gobject-direct-slot-definition-g-property-type)))
45 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
46 ((g-property-type :initform nil
47 :initarg :g-property-type
48 :accessor gobject-effective-slot-definition-g-property-type)))
50 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
51 ((g-property-name :initform nil
52 :initarg :g-property-name
53 :reader gobject-property-direct-slot-definition-g-property-name)))
55 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
56 ((g-property-name :initform nil
57 :initarg :g-property-name
58 :accessor gobject-property-effective-slot-definition-g-property-name)))
60 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
61 ((g-getter-name :initform nil
63 :reader gobject-fn-direct-slot-definition-g-getter-name)
64 (g-setter-name :initform nil
66 :reader gobject-fn-direct-slot-definition-g-setter-name)))
68 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
69 ((g-getter-name :initform nil
71 :accessor gobject-fn-effective-slot-definition-g-getter-name)
72 (g-setter-name :initform nil
74 :accessor gobject-fn-effective-slot-definition-g-setter-name)
75 (g-getter-fn :initform nil
76 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
77 (g-setter-fn :initform nil
78 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
80 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
83 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
86 (defmethod compute-class-precedence-list ((class gobject-class))
87 (let ((classes (call-next-method)))
88 (if (member (find-class 'g-object) classes)
90 `(,class ,(find-class 'g-object) ,@(rest classes)))))
92 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
93 (declare (ignore initargs))
95 (:gobject-property 'gobject-property-direct-slot-definition)
96 (:gobject-fn 'gobject-fn-direct-slot-definition)
97 (otherwise (call-next-method))))
99 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
100 (declare (ignore initargs))
102 (:gobject-property 'gobject-property-effective-slot-definition)
103 (:gobject-fn 'gobject-fn-effective-slot-definition)
104 (otherwise (call-next-method))))
106 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
107 (let ((effective-slot (call-next-method)))
108 (when (typep effective-slot 'gobject-effective-slot-definition)
109 (let ((allocation (loop
110 for direct-slot in direct-slots
111 when (slot-definition-allocation direct-slot)
112 return (slot-definition-allocation direct-slot)))
114 for direct-slot in direct-slots
115 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
116 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
118 for direct-slot in direct-slots
119 when (gobject-direct-slot-definition-g-property-type direct-slot)
120 return (gobject-direct-slot-definition-g-property-type direct-slot)))
121 (property-getter (loop
122 for direct-slot in direct-slots
123 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
124 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
125 (property-setter (loop
126 for direct-slot in direct-slots
127 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
128 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
129 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
130 (gobject-effective-slot-definition-g-property-type effective-slot))
132 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
133 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
135 (: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"
136 name (class-name class))
137 (when (or (and property-getter (stringp property-getter))
138 (and property-setter (stringp property-setter)))
139 (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)))
141 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
142 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
143 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
145 (if (stringp property-getter)
146 (compile nil `(lambda (object)
147 (foreign-funcall ,property-getter
151 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
153 (if (stringp property-setter)
154 (compile nil `(lambda (object new-value)
155 (foreign-funcall ,property-setter
157 ,property-type new-value
159 property-setter)))))))
162 (defun create-gobject-from-class-and-initargs (class initargs)
163 (when (gobject-class-interface-p class)
164 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
165 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
166 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
168 for (arg-name arg-value) on initargs by #'cddr
169 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
170 when (and slot (typep slot 'gobject-effective-slot-definition))
172 (gobject-property-effective-slot-definition
173 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
174 (push arg-value arg-values)
175 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
176 (gobject-fn-effective-slot-definition
177 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
178 (push arg-value nc-arg-values))))
179 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
182 for value in nc-arg-values
183 do (funcall fn object value))
186 (defun filter-initargs-by-class (class initargs)
187 (iter (with slots = (class-slots class))
188 (for (arg-name arg-value) on initargs by #'cddr)
189 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
190 (unless (and slot (typep slot 'gobject-effective-slot-definition))
191 (nconcing (list arg-name arg-value)))))
193 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
194 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
195 (apply #'call-next-method instance filtered-initargs)))
197 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
200 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
202 (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
203 (apply #'call-next-method class :pointer pointer initargs))))
205 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
207 (progn (g-object-property-type object (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
208 (property-unreadable-error () nil)))
210 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
211 (g-object-call-get-property object
212 (gobject-property-effective-slot-definition-g-property-name slot)
213 (gobject-effective-slot-definition-g-property-type slot)))
215 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
216 (g-object-call-set-property object
217 (gobject-property-effective-slot-definition-g-property-name slot)
219 (gobject-effective-slot-definition-g-property-type slot)))
221 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
222 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
224 (funcall fn object))))
226 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
227 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
229 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
232 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))