gobject: improved checks in initialize-instance for gobject meta-class
[cl-gtk2.git] / glib / gobject.meta.lisp
1 (in-package :gobject)
2
3 (defclass gobject-class (standard-class)
4   ((g-type-name :initform (error "G-TYPE-NAME must be specified")
5                 :initarg :g-type-name
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
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
20                                               g-type))))
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))
24           
25             (progn
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)
31                                (g-type-name type))
32                 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
33                       (gobject-class-g-type-name object)
34                       (class-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)))))
39
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)))
44
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)))
49
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)))
54
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)))
59
60 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
61   ((g-getter-name :initform nil
62                   :initarg :g-getter
63                   :reader gobject-fn-direct-slot-definition-g-getter-name)
64    (g-setter-name :initform nil
65                   :initarg :g-setter
66                   :reader gobject-fn-direct-slot-definition-g-setter-name)))
67
68 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
69   ((g-getter-name :initform nil
70                   :initarg :g-getter
71                   :accessor gobject-fn-effective-slot-definition-g-getter-name)
72    (g-setter-name :initform nil
73                   :initarg :g-setter
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)))
79
80 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
81   t)
82
83 (defmethod validate-superclass ((class standard-class) (superclass gobject-class))
84   t)
85
86 (defmethod compute-class-precedence-list ((class gobject-class))
87   (let ((classes (call-next-method)))
88     (if (member (find-class 'g-object) classes)
89         classes
90         `(,class ,(find-class 'g-object) ,@(rest classes)))))
91
92 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
93   (declare (ignore initargs))
94   (case allocation
95     (:gobject-property 'gobject-property-direct-slot-definition)
96     (:gobject-fn 'gobject-fn-direct-slot-definition)
97     (otherwise (call-next-method))))
98
99 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
100   (declare (ignore initargs))
101   (case allocation
102     (:gobject-property 'gobject-property-effective-slot-definition)
103     (:gobject-fn 'gobject-fn-effective-slot-definition)
104     (otherwise (call-next-method))))
105
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)))
113             (property-name (loop
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)))
117             (property-type (loop
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))
131         (ecase allocation
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)
134                                    property-name))
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)))
140                        
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)
144                              (and property-getter
145                                   (if (stringp property-getter)
146                                       (compile nil `(lambda (object)
147                                                       (foreign-funcall ,property-getter
148                                                                        g-object object
149                                                                        ,property-type)))
150                                       property-getter))
151                              (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
152                              (and property-setter
153                                   (if (stringp property-setter)
154                                       (compile nil `(lambda (object new-value)
155                                                       (foreign-funcall ,property-setter
156                                                                        g-object object
157                                                                        ,property-type new-value
158                                                                        :void)))
159                                       property-setter)))))))
160     effective-slot))
161
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))
167     (loop
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))
171        do (typecase slot
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)))
180       (loop
181          for fn in nc-setters
182          for value in nc-arg-values
183          do (funcall fn object value))
184       object)))
185
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)))))
192
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)))
196
197 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
198   (if pointer
199       (progn
200         (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
201         (call-next-method))
202       (let ((pointer (create-gobject-from-class-and-initargs class initargs)))
203         (apply #'call-next-method class :pointer pointer initargs))))
204
205 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
206   (handler-case
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)))
209
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)))
214
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)
218                               new-value
219                               (gobject-effective-slot-definition-g-property-type slot)))
220
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)))
223     (when fn
224       (funcall fn object))))
225
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))
228
229 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
230   t)
231
232 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
233   nil)