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 (when (gobject-class-g-type-initializer object)
17 (let ((type (foreign-funcall-pointer (foreign-symbol-pointer (gobject-class-g-type-initializer object)) nil
19 (assert (/= +g-type-invalid+ type) nil "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
20 (gobject-class-g-type-name object) (class-name object))
21 (assert (string= (gobject-class-g-type-name object)
23 nil "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
24 (gobject-class-g-type-name object)
26 (g-type-name type)))))
28 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
29 ((g-property-type :initform nil
30 :initarg :g-property-type
31 :reader gobject-direct-slot-definition-g-property-type)))
33 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
34 ((g-property-type :initform nil
35 :initarg :g-property-type
36 :accessor gobject-effective-slot-definition-g-property-type)))
38 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
39 ((g-property-name :initform nil
40 :initarg :g-property-name
41 :reader gobject-property-direct-slot-definition-g-property-name)))
43 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
44 ((g-property-name :initform nil
45 :initarg :g-property-name
46 :accessor gobject-property-effective-slot-definition-g-property-name)))
48 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
49 ((g-getter-name :initform nil
51 :reader gobject-fn-direct-slot-definition-g-getter-name)
52 (g-setter-name :initform nil
54 :reader gobject-fn-direct-slot-definition-g-setter-name)))
56 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
57 ((g-getter-name :initform nil
59 :accessor gobject-fn-effective-slot-definition-g-getter-name)
60 (g-setter-name :initform nil
62 :accessor gobject-fn-effective-slot-definition-g-setter-name)
63 (g-getter-fn :initform nil
64 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
65 (g-setter-fn :initform nil
66 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
68 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
71 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
74 (defmethod compute-class-precedence-list ((class gobject-class))
75 (let ((classes (call-next-method)))
76 (if (member (find-class 'g-object) classes)
78 `(,class ,(find-class 'g-object) ,@(rest classes)))))
80 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
81 (declare (ignore initargs))
83 (:gobject-property 'gobject-property-direct-slot-definition)
84 (:gobject-fn 'gobject-fn-direct-slot-definition)
85 (otherwise (call-next-method))))
87 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
88 (declare (ignore initargs))
90 (:gobject-property 'gobject-property-effective-slot-definition)
91 (:gobject-fn 'gobject-fn-effective-slot-definition)
92 (otherwise (call-next-method))))
94 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
95 (let ((effective-slot (call-next-method)))
96 (when (typep effective-slot 'gobject-effective-slot-definition)
97 (let ((allocation (loop
98 for direct-slot in direct-slots
99 when (slot-definition-allocation direct-slot)
100 return (slot-definition-allocation direct-slot)))
102 for direct-slot in direct-slots
103 when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
104 return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
106 for direct-slot in direct-slots
107 when (gobject-direct-slot-definition-g-property-type direct-slot)
108 return (gobject-direct-slot-definition-g-property-type direct-slot)))
109 (property-getter (loop
110 for direct-slot in direct-slots
111 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
112 return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
113 (property-setter (loop
114 for direct-slot in direct-slots
115 when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
116 return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
117 (setf (gobject-effective-slot-definition-g-property-type effective-slot)
118 (gobject-effective-slot-definition-g-property-type effective-slot))
120 (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
121 (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
123 (: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"
124 name (class-name class))
125 (when (or (and property-getter (stringp property-getter))
126 (and property-setter (stringp property-setter)))
127 (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)))
129 (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
130 (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
131 (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
133 (if (stringp property-getter)
134 (compile nil `(lambda (object)
135 (foreign-funcall ,property-getter
139 (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
141 (if (stringp property-setter)
142 (compile nil `(lambda (object new-value)
143 (foreign-funcall ,property-setter
145 ,property-type new-value
147 property-setter)))))))
150 (defun create-gobject-from-class-and-initargs (class initargs)
151 (when (gobject-class-interface-p class)
152 (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
153 (let (arg-names arg-values arg-types nc-setters nc-arg-values)
154 (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
156 for (arg-name arg-value) on initargs by #'cddr
157 for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
158 when (and slot (typep slot 'gobject-effective-slot-definition))
160 (gobject-property-effective-slot-definition
161 (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
162 (push arg-value arg-values)
163 (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
164 (gobject-fn-effective-slot-definition
165 (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
166 (push arg-value nc-arg-values))))
167 (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
170 for value in nc-arg-values
171 do (funcall fn object value))
174 (defun filter-initargs-by-class (class initargs)
175 (iter (with slots = (class-slots class))
176 (for (arg-name arg-value) on initargs by #'cddr)
177 (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
178 (unless (and slot (typep slot 'gobject-effective-slot-definition))
179 (nconcing (list arg-name arg-value)))))
181 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
182 (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
183 (apply #'call-next-method instance filtered-initargs)))
185 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
188 (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
190 (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
191 (apply #'call-next-method class :pointer pointer initargs))))
193 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
195 (progn (g-object-property-type object (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t)
196 (property-unreadable-error () nil)))
198 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
199 (g-object-call-get-property object
200 (gobject-property-effective-slot-definition-g-property-name slot)
201 (gobject-effective-slot-definition-g-property-type slot)))
203 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
204 (g-object-call-set-property object
205 (gobject-property-effective-slot-definition-g-property-name slot)
207 (gobject-effective-slot-definition-g-property-type slot)))
209 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
210 (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
212 (funcall fn object))))
214 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
215 (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value))
217 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
220 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))