bd691e21 |
1 | ;; Common Lisp bindings for GTK+ v2.x |
2 | ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net> |
3 | ;; |
4 | ;; Permission is hereby granted, free of charge, to any person obtaining |
5 | ;; a copy of this software and associated documentation files (the |
6 | ;; "Software"), to deal in the Software without restriction, including |
7 | ;; without limitation the rights to use, copy, modify, merge, publish, |
8 | ;; distribute, sublicense, and/or sell copies of the Software, and to |
9 | ;; permit persons to whom the Software is furnished to do so, subject to |
10 | ;; the following conditions: |
11 | ;; |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
14 | ;; |
15 | ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | |
553aa0e3 |
23 | ;; $Id: virtual-slots.lisp,v 1.5 2006/09/13 10:52:16 espen Exp $ |
bd691e21 |
24 | |
25 | (in-package "GFFI") |
26 | |
27 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots |
28 | |
6b716036 |
29 | (defclass virtual-slots-class (standard-class) |
30 | ()) |
31 | |
32 | (defclass direct-virtual-slot-definition (standard-direct-slot-definition) |
33 | ((setter :reader slot-definition-setter :initarg :setter) |
34 | (getter :reader slot-definition-getter :initarg :getter) |
35 | (unbound :reader slot-definition-unbound :initarg :unbound) |
36 | (boundp :reader slot-definition-boundp :initarg :boundp) |
37 | (makunbound :reader slot-definition-makunbound :initarg :makunbound) |
38 | #+clisp(type :initarg :type :reader slot-definition-type))) |
bd691e21 |
39 | |
6b716036 |
40 | (defclass effective-virtual-slot-definition (standard-effective-slot-definition) |
41 | ((setter :reader slot-definition-setter :initarg :setter) |
42 | (getter :reader slot-definition-getter :initarg :getter) |
43 | (unbound :reader slot-definition-unbound :initarg :unbound) |
44 | (boundp :reader slot-definition-boundp :initarg :boundp) |
45 | (makunbound :reader slot-definition-makunbound :initarg :makunbound) |
46 | #+clisp(reader-function) |
47 | #+clisp(writer-function) |
48 | #+clisp(boundp-function) |
49 | makunbound-function |
50 | #+clisp(type :initarg :type :reader slot-definition-type))) |
51 | |
52 | (defclass direct-special-slot-definition (standard-direct-slot-definition) |
53 | ((special :initarg :special :accessor slot-definition-special))) |
54 | |
55 | (defclass effective-special-slot-definition (standard-effective-slot-definition) |
56 | ((special :initarg :special :accessor slot-definition-special))) |
57 | |
58 | (defclass virtual-slots-object (standard-object) |
59 | ()) |
60 | |
376809b0 |
61 | (defgeneric slot-readable-p (slotd)) |
62 | (defgeneric slot-writable-p (slotd)) |
6b716036 |
63 | (defgeneric compute-slot-reader-function (slotd &optional signal-unbound-p)) |
bd691e21 |
64 | (defgeneric compute-slot-boundp-function (slotd)) |
65 | (defgeneric compute-slot-writer-function (slotd)) |
66 | (defgeneric compute-slot-makunbound-function (slotd)) |
67 | |
376809b0 |
68 | (defmethod slot-readable-p ((slotd standard-effective-slot-definition)) |
69 | (declare (ignore slotd)) |
70 | t) |
71 | |
72 | (defmethod slot-writable-p ((slotd standard-effective-slot-definition)) |
73 | (declare (ignore slotd)) |
74 | t) |
75 | |
bd691e21 |
76 | |
77 | #+clisp |
78 | (defmethod slot-definition-type ((slotd t)) |
79 | (clos:slot-definition-type slotd)) |
80 | |
81 | |
82 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) |
83 | (cond |
84 | ((eq (getf initargs :allocation) :virtual) |
85 | (find-class 'direct-virtual-slot-definition)) |
86 | ((getf initargs :special) |
87 | (find-class 'direct-special-slot-definition)) |
88 | (t (call-next-method)))) |
89 | |
90 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) |
91 | (cond |
92 | ((eq (getf initargs :allocation) :virtual) |
93 | (find-class 'effective-virtual-slot-definition)) |
94 | ((getf initargs :special) |
95 | (find-class 'effective-special-slot-definition)) |
96 | (t (call-next-method)))) |
97 | |
98 | |
376809b0 |
99 | (defmethod slot-readable-p ((slotd effective-virtual-slot-definition)) |
100 | (slot-boundp slotd 'getter)) |
101 | |
bd691e21 |
102 | (define-condition unreadable-slot (cell-error) |
103 | ((instance :reader unreadable-slot-instance :initarg :instance)) |
104 | (:report (lambda (condition stream) |
105 | (format stream "~@<The slot ~S in the object ~S is not readable.~@:>" |
106 | (cell-error-name condition) |
107 | (unreadable-slot-instance condition))))) |
108 | |
6b716036 |
109 | (defmethod compute-slot-reader-function :around ((slotd effective-virtual-slot-definition) &optional (signal-unbound-p t)) |
376809b0 |
110 | (if (not (slot-readable-p slotd)) |
111 | #'(lambda (object) |
112 | (error 'unreadable-slot :name (slot-definition-name slotd) :instance object)) |
113 | (let ((reader-function (call-next-method))) |
114 | (cond |
9b773fc8 |
115 | ;; Don't create wrapper to signal unbound value |
376809b0 |
116 | ((not signal-unbound-p) reader-function) |
117 | |
118 | ;; An explicit boundp function has been supplied |
119 | ((slot-boundp slotd 'boundp) |
120 | (let ((unbound-value (slot-value slotd 'boundp))) |
121 | #'(lambda (object) |
122 | (let ((value (funcall reader-function object))) |
123 | (if (eq value unbound-value) |
124 | (slot-unbound (class-of object) object (slot-definition-name slotd)) |
125 | value))))) |
126 | |
127 | ;; A type unbound value exists |
128 | ((let ((unbound-method (find-applicable-type-method 'unbound-value |
129 | (slot-definition-type slotd) nil))) |
130 | (when unbound-method |
131 | (let ((unbound-value (funcall unbound-method (slot-definition-type slotd)))) |
132 | #'(lambda (object) |
133 | (let ((value (funcall reader-function object))) |
134 | (if (eq value unbound-value) |
135 | (slot-unbound (class-of object) object (slot-definition-name slotd)) |
136 | value))))))) |
137 | |
138 | ((let ((boundp-function (compute-slot-boundp-function slotd))) |
139 | #'(lambda (object) |
140 | (if (funcall boundp-function object) |
141 | (funcall reader-function object) |
142 | (slot-unbound (class-of object) object (slot-definition-name slotd)))))))))) |
6b716036 |
143 | |
144 | (defmethod compute-slot-reader-function ((slotd effective-virtual-slot-definition) &optional signal-unbound-p) |
145 | (declare (ignore signal-unbound-p)) |
553aa0e3 |
146 | (let ((getter (slot-value slotd 'getter))) |
147 | #-sbcl getter |
148 | #+sbcl |
149 | (etypecase getter |
150 | (symbol #'(lambda (object) (funcall getter object))) |
151 | (function getter)))) |
bd691e21 |
152 | |
153 | (defmethod compute-slot-boundp-function ((slotd effective-virtual-slot-definition)) |
154 | (cond |
376809b0 |
155 | ;; Non readable slots are not bound per definition |
156 | ((not (slot-readable-p slotd)) |
157 | #'(lambda (object) (declare (ignore object)) nil)) |
158 | |
bd691e21 |
159 | ;; An explicit boundp function has been supplied |
553aa0e3 |
160 | ((slot-boundp slotd 'boundp) |
161 | (let ((boundp (slot-value slotd 'boundp))) |
162 | #-sbcl boundp |
163 | #+sbcl |
164 | (etypecase boundp |
165 | (symbol #'(lambda (object) (funcall boundp object))) |
166 | (function boundp)))) |
167 | |
bd691e21 |
168 | ;; An unbound value has been supplied |
169 | ((slot-boundp slotd 'unbound) |
6b716036 |
170 | (let ((reader-function (compute-slot-reader-function slotd nil)) |
bd691e21 |
171 | (unbound-value (slot-value slotd 'unbound))) |
172 | #'(lambda (object) |
173 | (not (eql (funcall reader-function object) unbound-value))))) |
174 | |
175 | ;; A type unbound value exists |
176 | ((let ((unbound-method (find-applicable-type-method 'unbound-value |
177 | (slot-definition-type slotd) nil))) |
178 | (when unbound-method |
6b716036 |
179 | (let ((reader-function (compute-slot-reader-function slotd nil)) |
bd691e21 |
180 | (unbound-value (funcall unbound-method (slot-definition-type slotd)))) |
181 | #'(lambda (object) |
182 | (not (eql (funcall reader-function object) unbound-value))))))) |
183 | |
184 | ;; Slot has no unbound state |
185 | (#'(lambda (object) (declare (ignore object)) t)))) |
186 | |
376809b0 |
187 | (defmethod slot-writable-p ((slotd effective-virtual-slot-definition)) |
188 | (slot-boundp slotd 'setter)) |
189 | |
bd691e21 |
190 | (define-condition unwritable-slot (cell-error) |
191 | ((instance :reader unwritable-slot-instance :initarg :instance)) |
192 | (:report (lambda (condition stream) |
193 | (format stream "~@<The slot ~S in the object ~S is not writable.~@:>" |
194 | (cell-error-name condition) |
195 | (unwritable-slot-instance condition))))) |
196 | |
376809b0 |
197 | (defmethod compute-slot-writer-function :around ((slotd effective-virtual-slot-definition)) |
198 | (if (not (slot-writable-p slotd)) |
199 | #'(lambda (value object) |
200 | (declare (ignore value)) |
201 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)) |
202 | (call-next-method))) |
203 | |
bd691e21 |
204 | (defmethod compute-slot-writer-function ((slotd effective-virtual-slot-definition)) |
553aa0e3 |
205 | (let ((setter (slot-value slotd 'setter))) |
206 | #-sbcl setter |
207 | #+sbcl |
208 | (etypecase setter |
209 | (symbol #'(lambda (object value) (funcall setter object value))) |
210 | (function setter)))) |
376809b0 |
211 | |
212 | (define-condition slot-can-not-be-unbound (cell-error) |
213 | ((instance :reader slot-can-not-be-unbound-instance :initarg :instance)) |
214 | (:report (lambda (condition stream) |
215 | (format stream "~@<The slot ~S in the object ~S can not be made unbound.~@:>" |
216 | (cell-error-name condition) |
217 | (slot-can-not-be-unbound-instance condition))))) |
bd691e21 |
218 | |
219 | (defmethod compute-slot-makunbound-function ((slotd effective-virtual-slot-definition)) |
220 | (cond |
376809b0 |
221 | ((not (slot-writable-p slotd)) |
222 | #'(lambda (object) |
223 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object))) |
553aa0e3 |
224 | ((slot-boundp slotd 'makunbound) |
225 | (let ((makunbound (slot-value slotd 'makunbound))) |
226 | #-sbcl makunbound |
227 | #+sbcl |
228 | (etypecase makunbound |
229 | (symbol #'(lambda (object) (funcall makunbound object))) |
230 | (function makunbound)))) |
bd691e21 |
231 | ((slot-boundp slotd 'unbound) |
232 | #'(lambda (object) |
233 | (funcall (slot-value slotd 'writer-function) (slot-value slotd 'unbound) object))) |
234 | (t |
235 | #'(lambda (object) |
376809b0 |
236 | (error 'slot-can-not-be-unbound :name (slot-definition-name slotd) :instance object))))) |
bd691e21 |
237 | |
238 | |
239 | #-clisp |
240 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) |
6b716036 |
241 | #?-(sbcl>= 0 9 15) ; Delayed to avoid recursive call of finalize-inheritanze |
bd691e21 |
242 | (setf |
243 | (slot-value slotd 'reader-function) (compute-slot-reader-function slotd) |
244 | (slot-value slotd 'boundp-function) (compute-slot-boundp-function slotd) |
245 | (slot-value slotd 'writer-function) (compute-slot-writer-function slotd) |
246 | (slot-value slotd 'makunbound-function) (compute-slot-makunbound-function slotd)) |
247 | |
248 | #?-(sbcl>= 0 9 8)(initialize-internal-slot-gfs (slot-definition-name slotd))) |
249 | |
250 | |
251 | #-clisp |
252 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) |
253 | nil) |
254 | |
6b716036 |
255 | |
bd691e21 |
256 | (defun slot-bound-in-some-p (instances slot) |
257 | (find-if |
258 | #'(lambda (ob) |
259 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) |
260 | instances)) |
261 | |
262 | (defun most-specific-slot-value (instances slot &optional default) |
263 | (let ((object (slot-bound-in-some-p instances slot))) |
264 | (if object |
265 | (slot-value object slot) |
266 | default))) |
267 | |
268 | (defun compute-most-specific-initargs (slotds slots) |
269 | (loop |
270 | for slot in slots |
271 | as (slot-name initarg) = (if (atom slot) |
272 | (list slot (intern (string slot) "KEYWORD")) |
273 | slot) |
274 | when (slot-bound-in-some-p slotds slot-name) |
275 | nconc (list initarg (most-specific-slot-value slotds slot-name)))) |
276 | |
277 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) |
278 | (typecase (first direct-slotds) |
279 | (direct-virtual-slot-definition |
280 | (nconc |
281 | (compute-most-specific-initargs direct-slotds |
282 | '(getter setter unbound boundp makunbound |
283 | #?(or (sbcl>= 0 9 8) (featurep :clisp)) |
284 | (#?-(sbcl>= 0 9 10)type #?(sbcl>= 0 9 10)sb-pcl::%type :type))) |
285 | (call-next-method))) |
286 | (direct-special-slot-definition |
287 | (append '(:special t) (call-next-method))) |
288 | (t (call-next-method)))) |
289 | |
6b716036 |
290 | #?(or (not (sbcl>= 0 9 14)) (featurep :clisp)) |
bd691e21 |
291 | (defmethod slot-value-using-class |
6b716036 |
292 | ((class virtual-slots-class) (object virtual-slots-object) |
bd691e21 |
293 | (slotd effective-virtual-slot-definition)) |
6b716036 |
294 | (funcall (slot-value slotd 'reader-function) object)) |
bd691e21 |
295 | |
6b716036 |
296 | #?(or (not (sbcl>= 0 9 14)) (featurep :clisp)) |
bd691e21 |
297 | (defmethod slot-boundp-using-class |
6b716036 |
298 | ((class virtual-slots-class) (object virtual-slots-object) |
bd691e21 |
299 | (slotd effective-virtual-slot-definition)) |
6b716036 |
300 | (funcall (slot-value slotd 'boundp-function) object)) |
bd691e21 |
301 | |
6b716036 |
302 | #?(or (not (sbcl>= 0 9 14)) (featurep :clisp)) |
bd691e21 |
303 | (defmethod (setf slot-value-using-class) |
6b716036 |
304 | (value (class virtual-slots-class) (object virtual-slots-object) |
bd691e21 |
305 | (slotd effective-virtual-slot-definition)) |
306 | (funcall (slot-value slotd 'writer-function) value object)) |
307 | |
308 | (defmethod slot-makunbound-using-class |
6b716036 |
309 | ((class virtual-slots-class) (object virtual-slots-object) |
bd691e21 |
310 | (slotd effective-virtual-slot-definition)) |
311 | (funcall (slot-value slotd 'makunbound-function) object)) |
312 | |
313 | |
6b716036 |
314 | ;; In CLISP and SBCL (0.9.15 or newler) a class may not have been |
315 | ;; finalized when update-slots are called. So to avoid the possibility |
9b773fc8 |
316 | ;; of finalize-instance being called recursivly we have to delay the |
6b716036 |
317 | ;; initialization of slot functions until after an instance has been |
318 | ;; created. |
319 | #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
320 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'reader-function))) |
321 | (setf (slot-value slotd name) (compute-slot-reader-function slotd))) |
bd691e21 |
322 | |
6b716036 |
323 | #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
324 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'boundp-function))) |
325 | (setf (slot-value slotd name) (compute-slot-boundp-function slotd))) |
326 | |
327 | #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
328 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'writer-function))) |
329 | (setf (slot-value slotd name) (compute-slot-writer-function slotd))) |
330 | |
331 | #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
332 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'makunbound-function))) |
333 | (setf (slot-value slotd name) (compute-slot-makunbound-function slotd))) |
bd691e21 |
334 | |
bd691e21 |
335 | |
336 | (defmethod validate-superclass |
337 | ((class virtual-slots-class) (super standard-class)) |
338 | t) |
339 | |
340 | (defmethod slot-definition-special ((slotd standard-direct-slot-definition)) |
341 | (declare (ignore slotd)) |
342 | nil) |
343 | |
344 | (defmethod slot-definition-special ((slotd standard-effective-slot-definition)) |
345 | (declare (ignore slotd)) |
346 | nil) |
347 | |
348 | |
bd691e21 |
349 | ;;; To determine if a slot should be initialized with the initform, |
350 | ;;; CLISP checks whether it is unbound or not. This doesn't work with |
9b773fc8 |
351 | ;;; virtual slots that does not have an unbound state, so we have to |
bd691e21 |
352 | ;;; implement initform initialization in a way similar to how it is |
353 | ;;; done in PCL. |
354 | #+clisp |
355 | (defmethod shared-initialize ((object virtual-slots-object) names &rest initargs) |
356 | (let* ((class (class-of object)) |
357 | (slotds (class-slots class)) |
358 | (keywords (loop |
359 | for args on initargs by #'cddr |
360 | collect (first args))) |
361 | (names |
362 | (loop |
363 | for slotd in slotds |
364 | as name = (slot-definition-name slotd) |
365 | as initargs = (slot-definition-initargs slotd) |
366 | as init-p = (and |
367 | (or (eq names t) (find name names)) |
368 | (slot-definition-initfunction slotd) |
369 | (not (intersection initargs keywords))) |
370 | as virtual-p = (typep slotd 'effective-virtual-slot-definition) |
371 | when (and init-p virtual-p) |
372 | do (setf |
373 | (slot-value-using-class class object slotd) |
374 | (funcall (slot-definition-initfunction slotd))) |
375 | when (and init-p (not virtual-p)) |
376 | collect name))) |
377 | |
378 | (apply #'call-next-method object names initargs))) |