Commit | Line | Data |
---|---|---|
4e968638 | 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 | ||
0bbf3105 | 23 | ;; $Id: virtual-slots.lisp,v 1.11 2007-11-08 13:49:26 espen Exp $ |
4e968638 | 24 | |
25 | (in-package "GFFI") | |
26 | ||
27 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots | |
28 | ||
584285fb | 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))) | |
4e968638 | 39 | |
584285fb | 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 | |
203681e2 | 50 | boundp-function |
584285fb | 51 | #+clisp(type :initarg :type :reader slot-definition-type))) |
52 | ||
53 | (defclass direct-special-slot-definition (standard-direct-slot-definition) | |
54 | ((special :initarg :special :accessor slot-definition-special))) | |
55 | ||
56 | (defclass effective-special-slot-definition (standard-effective-slot-definition) | |
57 | ((special :initarg :special :accessor slot-definition-special))) | |
58 | ||
59 | (defclass virtual-slots-object (standard-object) | |
60 | ()) | |
61 | ||
2bd78f93 | 62 | (defgeneric slot-readable-p (slotd)) |
63 | (defgeneric slot-writable-p (slotd)) | |
584285fb | 64 | (defgeneric compute-slot-reader-function (slotd &optional signal-unbound-p)) |
4e968638 | 65 | (defgeneric compute-slot-boundp-function (slotd)) |
66 | (defgeneric compute-slot-writer-function (slotd)) | |
67 | (defgeneric compute-slot-makunbound-function (slotd)) | |
68 | ||
2bd78f93 | 69 | (defmethod slot-readable-p ((slotd standard-effective-slot-definition)) |
70 | (declare (ignore slotd)) | |
71 | t) | |
72 | ||
73 | (defmethod slot-writable-p ((slotd standard-effective-slot-definition)) | |
74 | (declare (ignore slotd)) | |
75 | t) | |
76 | ||
4e968638 | 77 | |
78 | #+clisp | |
79 | (defmethod slot-definition-type ((slotd t)) | |
80 | (clos:slot-definition-type slotd)) | |
81 | ||
82 | ||
83 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) | |
84 | (cond | |
85 | ((eq (getf initargs :allocation) :virtual) | |
86 | (find-class 'direct-virtual-slot-definition)) | |
87 | ((getf initargs :special) | |
88 | (find-class 'direct-special-slot-definition)) | |
89 | (t (call-next-method)))) | |
90 | ||
91 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) | |
92 | (cond | |
93 | ((eq (getf initargs :allocation) :virtual) | |
94 | (find-class 'effective-virtual-slot-definition)) | |
95 | ((getf initargs :special) | |
96 | (find-class 'effective-special-slot-definition)) | |
97 | (t (call-next-method)))) | |
98 | ||
99 | ||
2bd78f93 | 100 | (defmethod slot-readable-p ((slotd effective-virtual-slot-definition)) |
101 | (slot-boundp slotd 'getter)) | |
102 | ||
4e968638 | 103 | (define-condition unreadable-slot (cell-error) |
104 | ((instance :reader unreadable-slot-instance :initarg :instance)) | |
105 | (:report (lambda (condition stream) | |
106 | (format stream "~@<The slot ~S in the object ~S is not readable.~@:>" | |
107 | (cell-error-name condition) | |
108 | (unreadable-slot-instance condition))))) | |
109 | ||
584285fb | 110 | (defmethod compute-slot-reader-function :around ((slotd effective-virtual-slot-definition) &optional (signal-unbound-p t)) |
2bd78f93 | 111 | (if (not (slot-readable-p slotd)) |
112 | #'(lambda (object) | |
113 | (error 'unreadable-slot :name (slot-definition-name slotd) :instance object)) | |
114 | (let ((reader-function (call-next-method))) | |
115 | (cond | |
aed39f73 | 116 | ;; Don't create wrapper to signal unbound value |
2bd78f93 | 117 | ((not signal-unbound-p) reader-function) |
118 | ||
119 | ;; An explicit boundp function has been supplied | |
120 | ((slot-boundp slotd 'boundp) | |
2f1ec179 | 121 | (let ((boundp (slot-value slotd 'boundp))) |
2bd78f93 | 122 | #'(lambda (object) |
2f1ec179 | 123 | (if (not (funcall boundp object)) |
124 | (slot-unbound (class-of object) object (slot-definition-name slotd)) | |
125 | (funcall reader-function object))))) | |
2bd78f93 | 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)))))))))) | |
584285fb | 143 | |
144 | (defmethod compute-slot-reader-function ((slotd effective-virtual-slot-definition) &optional signal-unbound-p) | |
145 | (declare (ignore signal-unbound-p)) | |
b977e63b | 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)))) | |
4e968638 | 152 | |
153 | (defmethod compute-slot-boundp-function ((slotd effective-virtual-slot-definition)) | |
154 | (cond | |
2bd78f93 | 155 | ;; Non readable slots are not bound per definition |
156 | ((not (slot-readable-p slotd)) | |
157 | #'(lambda (object) (declare (ignore object)) nil)) | |
158 | ||
4e968638 | 159 | ;; An explicit boundp function has been supplied |
b977e63b | 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 | ||
4e968638 | 168 | ;; An unbound value has been supplied |
169 | ((slot-boundp slotd 'unbound) | |
584285fb | 170 | (let ((reader-function (compute-slot-reader-function slotd nil)) |
4e968638 | 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 | |
584285fb | 179 | (let ((reader-function (compute-slot-reader-function slotd nil)) |
4e968638 | 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 | ||
2bd78f93 | 187 | (defmethod slot-writable-p ((slotd effective-virtual-slot-definition)) |
188 | (slot-boundp slotd 'setter)) | |
189 | ||
4e968638 | 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 | ||
2bd78f93 | 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 | ||
4e968638 | 204 | (defmethod compute-slot-writer-function ((slotd effective-virtual-slot-definition)) |
b977e63b | 205 | (let ((setter (slot-value slotd 'setter))) |
206 | #-sbcl setter | |
207 | #+sbcl | |
208 | (etypecase setter | |
c7a9d346 | 209 | (symbol #'(lambda (value object) (funcall setter value object))) |
210 | (list #'(lambda (value object) | |
9f851ae5 | 211 | (funcall setter value object))) |
b977e63b | 212 | (function setter)))) |
2bd78f93 | 213 | |
214 | (define-condition slot-can-not-be-unbound (cell-error) | |
215 | ((instance :reader slot-can-not-be-unbound-instance :initarg :instance)) | |
216 | (:report (lambda (condition stream) | |
217 | (format stream "~@<The slot ~S in the object ~S can not be made unbound.~@:>" | |
218 | (cell-error-name condition) | |
219 | (slot-can-not-be-unbound-instance condition))))) | |
4e968638 | 220 | |
221 | (defmethod compute-slot-makunbound-function ((slotd effective-virtual-slot-definition)) | |
222 | (cond | |
2bd78f93 | 223 | ((not (slot-writable-p slotd)) |
224 | #'(lambda (object) | |
225 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object))) | |
b977e63b | 226 | ((slot-boundp slotd 'makunbound) |
227 | (let ((makunbound (slot-value slotd 'makunbound))) | |
228 | #-sbcl makunbound | |
229 | #+sbcl | |
230 | (etypecase makunbound | |
231 | (symbol #'(lambda (object) (funcall makunbound object))) | |
232 | (function makunbound)))) | |
4e968638 | 233 | ((slot-boundp slotd 'unbound) |
234 | #'(lambda (object) | |
235 | (funcall (slot-value slotd 'writer-function) (slot-value slotd 'unbound) object))) | |
236 | (t | |
237 | #'(lambda (object) | |
2bd78f93 | 238 | (error 'slot-can-not-be-unbound :name (slot-definition-name slotd) :instance object))))) |
4e968638 | 239 | |
240 | ||
241 | #-clisp | |
242 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) | |
1d2032d4 RS |
243 | #?-(sbcl>= 0 9 8) |
244 | (initialize-internal-slot-gfs (slot-definition-name slotd))) | |
4e968638 | 245 | |
246 | ||
247 | #-clisp | |
248 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) | |
249 | nil) | |
250 | ||
584285fb | 251 | |
4e968638 | 252 | (defun slot-bound-in-some-p (instances slot) |
253 | (find-if | |
254 | #'(lambda (ob) | |
255 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) | |
256 | instances)) | |
257 | ||
258 | (defun most-specific-slot-value (instances slot &optional default) | |
259 | (let ((object (slot-bound-in-some-p instances slot))) | |
260 | (if object | |
261 | (slot-value object slot) | |
262 | default))) | |
263 | ||
264 | (defun compute-most-specific-initargs (slotds slots) | |
265 | (loop | |
266 | for slot in slots | |
267 | as (slot-name initarg) = (if (atom slot) | |
268 | (list slot (intern (string slot) "KEYWORD")) | |
269 | slot) | |
270 | when (slot-bound-in-some-p slotds slot-name) | |
271 | nconc (list initarg (most-specific-slot-value slotds slot-name)))) | |
272 | ||
273 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) | |
274 | (typecase (first direct-slotds) | |
275 | (direct-virtual-slot-definition | |
276 | (nconc | |
277 | (compute-most-specific-initargs direct-slotds | |
278 | '(getter setter unbound boundp makunbound | |
279 | #?(or (sbcl>= 0 9 8) (featurep :clisp)) | |
280 | (#?-(sbcl>= 0 9 10)type #?(sbcl>= 0 9 10)sb-pcl::%type :type))) | |
281 | (call-next-method))) | |
282 | (direct-special-slot-definition | |
283 | (append '(:special t) (call-next-method))) | |
284 | (t (call-next-method)))) | |
285 | ||
b9f34364 RS |
286 | (defmacro vsc-slot-x-using-class (x x-slot-name computer |
287 | &key allow-string-fun-p setf-p) | |
1d2032d4 RS |
288 | (let ((generic-name (intern (concatenate 'string |
289 | "SLOT-" (string x) "-USING-CLASS")))) | |
b9f34364 RS |
290 | `(defmethod ,(if setf-p `(setf ,generic-name) generic-name) |
291 | (,@(when setf-p '(value)) | |
292 | (class virtual-slots-class) (object virtual-slots-object) | |
1d2032d4 | 293 | (slotd effective-virtual-slot-definition)) |
8f49b7a1 RS |
294 | (unless (and (slot-boundp slotd ',x-slot-name) |
295 | ,@(when allow-string-fun-p | |
b9f34364 | 296 | `((not (stringp (slot-value slotd ',x-slot-name)))))) |
1d2032d4 | 297 | (setf (slot-value slotd ',x-slot-name) (,computer slotd))) |
b9f34364 RS |
298 | (funcall (slot-value slotd ',x-slot-name) |
299 | ,@(when setf-p '(value)) | |
300 | object)))) | |
1d2032d4 | 301 | |
8f49b7a1 RS |
302 | (vsc-slot-x-using-class value getter compute-slot-reader-function |
303 | :allow-string-fun-p t) | |
b9f34364 RS |
304 | (vsc-slot-x-using-class value setter compute-slot-writer-function |
305 | :allow-string-fun-p t :setf-p t) | |
306 | ||
1d2032d4 RS |
307 | (vsc-slot-x-using-class boundp boundp-function compute-slot-boundp-function) |
308 | (vsc-slot-x-using-class makunbound makunbound-function | |
309 | compute-slot-makunbound-function) | |
310 | ||
584285fb | 311 | ;; In CLISP and SBCL (0.9.15 or newler) a class may not have been |
312 | ;; finalized when update-slots are called. So to avoid the possibility | |
aed39f73 | 313 | ;; of finalize-instance being called recursivly we have to delay the |
584285fb | 314 | ;; initialization of slot functions until after an instance has been |
315 | ;; created. | |
0bbf3105 | 316 | ;; 2007-11-08: done this for all implementations |
317 | ;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) | |
584285fb | 318 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'reader-function))) |
e3f4fceb | 319 | (declare (ignore class)) |
584285fb | 320 | (setf (slot-value slotd name) (compute-slot-reader-function slotd))) |
4e968638 | 321 | |
0bbf3105 | 322 | ;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
584285fb | 323 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'boundp-function))) |
e3f4fceb | 324 | (declare (ignore class)) |
584285fb | 325 | (setf (slot-value slotd name) (compute-slot-boundp-function slotd))) |
326 | ||
0bbf3105 | 327 | ;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
584285fb | 328 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'writer-function))) |
e3f4fceb | 329 | (declare (ignore class)) |
584285fb | 330 | (setf (slot-value slotd name) (compute-slot-writer-function slotd))) |
331 | ||
0bbf3105 | 332 | ;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) |
584285fb | 333 | (defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'makunbound-function))) |
e3f4fceb | 334 | (declare (ignore class)) |
584285fb | 335 | (setf (slot-value slotd name) (compute-slot-makunbound-function slotd))) |
4e968638 | 336 | |
4e968638 | 337 | |
338 | (defmethod validate-superclass | |
339 | ((class virtual-slots-class) (super standard-class)) | |
340 | t) | |
341 | ||
342 | (defmethod slot-definition-special ((slotd standard-direct-slot-definition)) | |
343 | (declare (ignore slotd)) | |
344 | nil) | |
345 | ||
346 | (defmethod slot-definition-special ((slotd standard-effective-slot-definition)) | |
347 | (declare (ignore slotd)) | |
348 | nil) | |
349 | ||
350 | ||
4e968638 | 351 | ;;; To determine if a slot should be initialized with the initform, |
352 | ;;; CLISP checks whether it is unbound or not. This doesn't work with | |
aed39f73 | 353 | ;;; virtual slots that does not have an unbound state, so we have to |
4e968638 | 354 | ;;; implement initform initialization in a way similar to how it is |
355 | ;;; done in PCL. | |
356 | #+clisp | |
357 | (defmethod shared-initialize ((object virtual-slots-object) names &rest initargs) | |
358 | (let* ((class (class-of object)) | |
359 | (slotds (class-slots class)) | |
360 | (keywords (loop | |
361 | for args on initargs by #'cddr | |
362 | collect (first args))) | |
363 | (names | |
364 | (loop | |
365 | for slotd in slotds | |
366 | as name = (slot-definition-name slotd) | |
367 | as initargs = (slot-definition-initargs slotd) | |
368 | as init-p = (and | |
369 | (or (eq names t) (find name names)) | |
370 | (slot-definition-initfunction slotd) | |
371 | (not (intersection initargs keywords))) | |
372 | as virtual-p = (typep slotd 'effective-virtual-slot-definition) | |
373 | when (and init-p virtual-p) | |
374 | do (setf | |
375 | (slot-value-using-class class object slotd) | |
376 | (funcall (slot-definition-initfunction slotd))) | |
377 | when (and init-p (not virtual-p)) | |
378 | collect name))) | |
379 | ||
380 | (apply #'call-next-method object names initargs))) |