in slot-boundp-using-class, check that pointer slot is bound before checking its...
[cl-gtk2.git] / glib / gobject.meta.lisp
1 (in-package :gobject)
2
3 (defclass gobject-class (standard-class)
4   ((g-type-name :initform nil
5                 :accessor gobject-class-g-type-name)
6    (direct-g-type-name :initform nil
7                        :initarg :g-type-name
8                        :accessor gobject-class-direct-g-type-name)
9    (g-type-initializer :initform nil
10                        :initarg :g-type-initializer
11                        :reader gobject-class-g-type-initializer)
12    (interface-p :initform nil
13                 :initarg :g-interface-p
14                 :reader gobject-class-interface-p))
15   (:documentation "Metaclass for GObject-based classes."))
16
17 (defun initialize-gobject-class-g-type (class)
18   (if (gobject-class-g-type-initializer class)
19       (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class)))
20              (type (when initializer-fn-ptr
21                      (foreign-funcall-pointer initializer-fn-ptr nil
22                                               g-type))))
23         (if (null initializer-fn-ptr)
24             (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'"
25                   (gobject-class-direct-g-type-name class) (class-name class) (gobject-class-g-type-initializer class))
26             (progn
27               (when (g-type= +g-type-invalid+ type)
28                 (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)"
29                       (gobject-class-direct-g-type-name class) (class-name class)
30                       (gobject-class-g-type-initializer class)))
31               (unless (g-type= (gobject-class-direct-g-type-name class) type)
32                 (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'"
33                       (gobject-class-direct-g-type-name class)
34                       (class-name class)
35                       (g-type-name type))))))
36       (unless (g-type-from-name (gobject-class-direct-g-type-name class))
37         (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)"
38               (gobject-class-direct-g-type-name class) (class-name class)))))
39
40 (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys)
41   (when (gobject-class-direct-g-type-name object)
42     (register-object-type (gobject-class-direct-g-type-name object) (class-name object))
43     (at-init (object) (initialize-gobject-class-g-type object))))
44
45 (defmethod finalize-inheritance :after ((class gobject-class))
46   (setf (gobject-class-g-type-name class)
47         (or (gobject-class-direct-g-type-name class)
48             (let ((gobject-superclass (iter (for superclass in (class-direct-superclasses class))
49                                             (finding superclass such-that (typep superclass 'gobject-class)))))
50               (assert gobject-superclass)
51               (gobject-class-g-type-name gobject-superclass)))))
52
53 (defclass gobject-direct-slot-definition (standard-direct-slot-definition)
54   ((g-property-type :initform nil
55                     :initarg :g-property-type
56                     :reader gobject-direct-slot-definition-g-property-type)))
57
58 (defclass gobject-effective-slot-definition (standard-effective-slot-definition)
59   ((g-property-type :initform nil
60                     :initarg :g-property-type
61                     :accessor gobject-effective-slot-definition-g-property-type)))
62
63 (defclass gobject-property-direct-slot-definition (gobject-direct-slot-definition)
64   ((g-property-name :initform nil
65                     :initarg :g-property-name
66                     :reader gobject-property-direct-slot-definition-g-property-name)))
67
68 (defclass gobject-property-effective-slot-definition (gobject-effective-slot-definition)
69   ((g-property-name :initform nil
70                     :initarg :g-property-name
71                     :accessor gobject-property-effective-slot-definition-g-property-name)))
72
73 (defclass gobject-fn-direct-slot-definition (gobject-direct-slot-definition)
74   ((g-getter-name :initform nil
75                   :initarg :g-getter
76                   :reader gobject-fn-direct-slot-definition-g-getter-name)
77    (g-setter-name :initform nil
78                   :initarg :g-setter
79                   :reader gobject-fn-direct-slot-definition-g-setter-name)))
80
81 (defclass gobject-fn-effective-slot-definition (gobject-effective-slot-definition)
82   ((g-getter-name :initform nil
83                   :initarg :g-getter
84                   :accessor gobject-fn-effective-slot-definition-g-getter-name)
85    (g-setter-name :initform nil
86                   :initarg :g-setter
87                   :accessor gobject-fn-effective-slot-definition-g-setter-name)
88    (g-getter-fn :initform nil
89                 :accessor gobject-fn-effective-slot-definition-g-getter-fn)
90    (g-setter-fn :initform nil
91                 :accessor gobject-fn-effective-slot-definition-g-setter-fn)))
92
93 (defmethod validate-superclass ((class gobject-class) (superclass standard-class))
94   t)
95
96 (defmethod compute-class-precedence-list ((class gobject-class))
97   (let ((classes (call-next-method)))
98     (if (member (find-class 'g-object) classes)
99         classes
100         `(,class ,(find-class 'g-object) ,@(rest classes)))))
101
102 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
103   (declare (ignore initargs))
104   (case allocation
105     (:gobject-property 'gobject-property-direct-slot-definition)
106     (:gobject-fn 'gobject-fn-direct-slot-definition)
107     (otherwise (call-next-method))))
108
109 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs &key allocation)
110   (declare (ignore initargs))
111   (case allocation
112     (:gobject-property 'gobject-property-effective-slot-definition)
113     (:gobject-fn 'gobject-fn-effective-slot-definition)
114     (otherwise (call-next-method))))
115
116 (defmethod compute-effective-slot-definition ((class gobject-class) name direct-slots)
117   (let ((effective-slot (call-next-method)))
118     (when (typep effective-slot 'gobject-effective-slot-definition)
119       (let ((allocation (loop
120                               for direct-slot in direct-slots
121                               when (slot-definition-allocation direct-slot)
122                               return (slot-definition-allocation direct-slot)))
123             (property-name (loop
124                               for direct-slot in direct-slots
125                               when (and (typep direct-slot 'gobject-property-direct-slot-definition) (gobject-property-direct-slot-definition-g-property-name direct-slot))
126                               return (gobject-property-direct-slot-definition-g-property-name direct-slot)))
127             (property-type (loop
128                               for direct-slot in direct-slots
129                               when (gobject-direct-slot-definition-g-property-type direct-slot)
130                               return (gobject-direct-slot-definition-g-property-type direct-slot)))
131             (property-getter (loop
132                               for direct-slot in direct-slots
133                               when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-getter-name direct-slot))
134                               return (gobject-fn-direct-slot-definition-g-getter-name direct-slot)))
135             (property-setter (loop
136                               for direct-slot in direct-slots
137                               when (and (typep direct-slot 'gobject-fn-direct-slot-definition) (gobject-fn-direct-slot-definition-g-setter-name direct-slot))
138                               return (gobject-fn-direct-slot-definition-g-setter-name direct-slot))))
139         (setf (gobject-effective-slot-definition-g-property-type effective-slot)
140               (gobject-effective-slot-definition-g-property-type effective-slot))
141         (ecase allocation
142           (:gobject-property (assert property-name nil "G-PROPERTY-NAME for slot ~A on class ~A must be specified" name (class-name class))
143                              (setf (gobject-property-effective-slot-definition-g-property-name effective-slot)
144                                    property-name))
145           (: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"
146                                name (class-name class))
147                        (when (or (and property-getter (stringp property-getter))
148                                  (and property-setter (stringp property-setter)))
149                         (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)))
150                        
151                        (setf (gobject-fn-effective-slot-definition-g-getter-name effective-slot) property-getter
152                              (gobject-fn-effective-slot-definition-g-setter-name effective-slot) property-setter
153                              (gobject-fn-effective-slot-definition-g-getter-fn effective-slot)
154                              (and property-getter
155                                   (if (stringp property-getter)
156                                       (if (foreign-symbol-pointer property-getter)
157                                           (compile nil `(lambda (object)
158                                                           (foreign-funcall ,property-getter
159                                                                            g-object object
160                                                                            ,property-type)))
161                                           (progn 
162                                             (warn "Property reader function '~A' has not been found" property-getter)
163                                             (lambda (object)
164                                               (declare (ignore object))
165                                               (error "Property reader function '~A' has not been found" property-getter))))
166                                       property-getter))
167                              (gobject-fn-effective-slot-definition-g-setter-fn effective-slot)
168                              (and property-setter
169                                   (if (stringp property-setter)
170                                       (if (foreign-symbol-pointer property-setter)
171                                           (compile nil `(lambda (object new-value)
172                                                           (foreign-funcall ,property-setter
173                                                                            g-object object
174                                                                            ,property-type new-value
175                                                                            :void)))
176                                           (progn
177                                             (warn "Property writer function '~A' has not been found" property-setter)
178                                             (lambda (object)
179                                               (declare (ignore object))
180                                               (error "Property writer function '~A' has not been found" property-setter))))
181                                       property-setter)))))))
182     effective-slot))
183
184 (defun create-gobject-from-class-and-initargs (class initargs)
185   (when (gobject-class-interface-p class)
186     (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class)))
187   (let (arg-names arg-values arg-types nc-setters nc-arg-values)
188     (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values))
189     (loop
190        for (arg-name arg-value) on initargs by #'cddr
191        for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member)
192        when (and slot (typep slot 'gobject-effective-slot-definition))
193        do (typecase slot
194             (gobject-property-effective-slot-definition
195              (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names)
196              (push arg-value arg-values)
197              (push (gobject-effective-slot-definition-g-property-type slot) arg-types))
198             (gobject-fn-effective-slot-definition
199              (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters)
200              (push arg-value nc-arg-values))))
201     (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types)))
202       (loop
203          for fn in nc-setters
204          for value in nc-arg-values
205          do (funcall fn object value))
206       object)))
207
208 (defun filter-initargs-by-class (class initargs)
209   (iter (with slots = (class-slots class))
210         (for (arg-name arg-value) on initargs by #'cddr)
211         (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member))
212         (unless (and slot (typep slot 'gobject-effective-slot-definition))
213           (nconcing (list arg-name arg-value)))))
214
215 (defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys)
216   (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs)))
217     (apply #'call-next-method instance filtered-initargs)))
218
219 (defmethod make-instance ((class gobject-class) &rest initargs &key pointer)
220   (log-for :subclass "(make-instance ~A ~{~A~^ ~})~%" class initargs)
221   (let ((*currently-making-object-p* t))
222     (if pointer
223         (progn
224           (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs)
225           (call-next-method))
226         (let* ((default-initargs (iter (for (arg value) in (class-default-initargs class))
227                                        (nconcing (list arg value))))
228                (effective-initargs (append initargs default-initargs))
229                (pointer (create-gobject-from-class-and-initargs class effective-initargs)))
230           (apply #'call-next-method class :pointer pointer effective-initargs)))))
231
232 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
233   (handler-case
234       (and (slot-boundp object 'pointer)
235            (pointer object)
236            (progn (g-object-property-type (pointer object) (gobject-property-effective-slot-definition-g-property-name slot) :assert-readable t) t))
237     (property-unreadable-error () nil)))
238
239 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition))
240   (g-object-call-get-property (pointer object)
241                               (gobject-property-effective-slot-definition-g-property-name slot)
242                               (gobject-effective-slot-definition-g-property-type slot)))
243
244 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-property-effective-slot-definition))
245   (g-object-call-set-property (pointer object)
246                               (gobject-property-effective-slot-definition-g-property-name slot)
247                               new-value
248                               (gobject-effective-slot-definition-g-property-type slot))
249   new-value)
250
251 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
252   (and (slot-boundp object 'pointer)
253        (pointer object)
254        (not (null (gobject-fn-effective-slot-definition-g-getter-fn slot)))))
255
256 (defmethod slot-value-using-class ((class gobject-class) object (slot gobject-fn-effective-slot-definition))
257   (let ((fn (gobject-fn-effective-slot-definition-g-getter-fn slot)))
258     (funcall fn object)))
259
260 (defmethod (setf slot-value-using-class) (new-value (class gobject-class) object (slot gobject-fn-effective-slot-definition))
261   (funcall (gobject-fn-effective-slot-definition-g-setter-fn slot) object new-value)
262   new-value)
263
264 (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
265   (slot-boundp object 'pointer))
266
267 (defmethod slot-makunbound-using-class ((class gobject-class) object (slot gobject-effective-slot-definition))
268   nil)