author | Stefan Vogel <sv@exept.de> |
Wed, 04 Aug 1999 18:58:08 +0200 | |
changeset 4554 | db264efac2c0 |
parent 4550 | 66adee5e8491 |
child 4605 | 7d5e97290c97 |
permissions | -rw-r--r-- |
4466
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
1 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
3 |
All Rights Reserved |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
4 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
5 |
This software is furnished under a license and may be used |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
6 |
only in accordance with the terms of that license and with the |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
7 |
inclusion of the above copyright notice. This software may not |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
8 |
be provided or otherwise made available to, or used by, any |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
9 |
other person. No title to or ownership of the software is |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
10 |
hereby transferred. |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
11 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
12 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
13 |
|
4443 | 14 |
Exception subclass:#Notification |
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Kernel-Exceptions' |
|
19 |
! |
|
20 |
||
21 |
!Notification class methodsFor:'documentation'! |
|
22 |
||
4466
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
23 |
copyright |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
24 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
25 |
COPYRIGHT (c) 1999 by eXept Software AG |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
26 |
All Rights Reserved |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
27 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
28 |
This software is furnished under a license and may be used |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
29 |
only in accordance with the terms of that license and with the |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
30 |
inclusion of the above copyright notice. This software may not |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
31 |
be provided or otherwise made available to, or used by, any |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
32 |
other person. No title to or ownership of the software is |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
33 |
hereby transferred. |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
34 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
35 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
36 |
! |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
37 |
|
4443 | 38 |
documentation |
39 |
" |
|
4550 | 40 |
Notification is the superclass of all notification exceptions in the system. |
41 |
These notifications are ignored, if no handler is present. |
|
42 |
In this case a default value is returned. |
|
4443 | 43 |
|
44 |
[author:] |
|
45 |
Stefan Vogel |
|
46 |
||
47 |
[see also:] |
|
4550 | 48 |
Signal QuerySignal |
4443 | 49 |
" |
4550 | 50 |
|
4443 | 51 |
! |
52 |
||
4550 | 53 |
examples |
4443 | 54 |
" |
4550 | 55 |
an up-query from a deeply nested operation to a higher level: |
56 |
[exBegin] |
|
57 |
|zero| |
|
58 |
||
59 |
zero := 0. |
|
60 |
Notification handle:[:ex | |
|
61 |
Transcript showCR:'query'. |
|
62 |
ex proceedWith:true |
|
63 |
] do:[ |
|
64 |
'nesting'. |
|
65 |
[ |
|
66 |
[ |
|
67 |
Object errorSignal handle:[:ex | |
|
68 |
Transcript showCR:'some error: ' , ex errorString. |
|
69 |
ex proceed |
|
70 |
] do:[ |
|
71 |
[ |
|
72 |
1 // zero. 'an error which is caught in the handler'. |
|
73 |
(Notification query) == true ifTrue:[ |
|
74 |
Transcript showCR:'query says: ok'. |
|
75 |
] ifFalse:[ |
|
76 |
Transcript showCR:'query says: no' |
|
77 |
] |
|
78 |
] value |
|
79 |
] |
|
80 |
] value |
|
81 |
] value |
|
82 |
] |
|
83 |
[exEnd] |
|
84 |
for lazy typists, a more compact interface is also provided |
|
85 |
(which is also easier to read): |
|
86 |
[exBegin] |
|
87 |
Notification answer:true do:[ |
|
88 |
'nesting'. |
|
89 |
[ |
|
90 |
[ |
|
91 |
(Notification query) == true ifTrue:[ |
|
92 |
Transcript showCR:'query says: ok'. |
|
93 |
] ifFalse:[ |
|
94 |
Transcript showCR:'query says: no' |
|
95 |
] |
|
96 |
] value |
|
97 |
] value |
|
98 |
] |
|
99 |
[exEnd] |
|
100 |
an up-query from a deeply nested operation, for which there |
|
101 |
is no handler: |
|
102 |
(notice, this would not work with normal signals, which would raise |
|
103 |
another unhandled exception-exception; |
|
104 |
also notice the == check #raise's return value being true, |
|
105 |
instead of a simple ifTrue; this handles a nil-value from |
|
106 |
the unhandled query) |
|
107 |
[exBegin] |
|
108 |
|zero| |
|
109 |
||
110 |
zero := 0. |
|
111 |
[ |
|
112 |
'nesting'. |
|
113 |
[ |
|
114 |
[ |
|
115 |
Object errorSignal handle:[:ex | |
|
116 |
Transcript showCR:'some error: ' , ex errorString. |
|
117 |
ex proceed |
|
118 |
] do:[ |
|
119 |
[ |
|
120 |
1 // zero. 'an error which is caught in the handler'. |
|
121 |
(Notification raise) == true ifTrue:[ |
|
122 |
Transcript showCR:'query says: ok'. |
|
123 |
] ifFalse:[ |
|
124 |
Transcript showCR:'query says: no' |
|
125 |
] |
|
126 |
] value |
|
127 |
] |
|
128 |
] value |
|
129 |
] value |
|
130 |
] value |
|
131 |
[exEnd] |
|
132 |
counter-example, just to show that things would not work this way |
|
133 |
with regular signals: |
|
134 |
[exBegin] |
|
135 |
|signal| |
|
136 |
||
137 |
signal := Signal new. |
|
138 |
'nesting deeply'. |
|
139 |
[ |
|
140 |
[ |
|
141 |
[ |
|
142 |
[ |
|
143 |
[ |
|
144 |
(signal raise) == true ifTrue:[ |
|
145 |
Transcript showCR:'query says: ok'. |
|
146 |
] ifFalse:[ |
|
147 |
Transcript showCR:'query says: no' |
|
148 |
] |
|
149 |
] value |
|
150 |
] value |
|
151 |
] value |
|
152 |
] value |
|
153 |
] value |
|
154 |
[exEnd] |
|
155 |
||
156 |
except, by handling the unhandled exception |
|
157 |
(but we think, that querySignals are easier to use and |
|
158 |
better document the intent): |
|
159 |
[exBegin] |
|
160 |
|signal| |
|
161 |
||
162 |
signal := Signal new. |
|
163 |
'nesting deeply'. |
|
164 |
[ |
|
165 |
[ |
|
166 |
[ |
|
167 |
[ |
|
168 |
[ |
|
169 |
Signal noHandlerSignal handle:[:ex | |
|
170 |
ex proceedWith:nil |
|
171 |
] do:[ |
|
172 |
(signal raise) == true ifTrue:[ |
|
173 |
Transcript showCR:'query says: ok'. |
|
174 |
] ifFalse:[ |
|
175 |
Transcript showCR:'query says: no' |
|
176 |
] |
|
177 |
] |
|
178 |
] value |
|
179 |
] value |
|
180 |
] value |
|
181 |
] value |
|
182 |
] value |
|
183 |
[exEnd] |
|
4443 | 184 |
" |
185 |
||
186 |
! ! |
|
187 |
||
188 |
!Notification class methodsFor:'initialization'! |
|
189 |
||
190 |
initialize |
|
191 |
||
4550 | 192 |
NotifierString := 'Notification:' |
193 |
! ! |
|
194 |
||
195 |
!Notification class methodsFor:'answering queries'! |
|
196 |
||
197 |
answer:someAnswer do:aBlock |
|
198 |
"evaluate the argument, aBlock. |
|
199 |
If the receiver is queried during evaluation, answer with someAnswer. |
|
200 |
This is a wrapper for #handle:do: for lazy typists; no new functionality." |
|
201 |
||
202 |
<exception: #handle> |
|
203 |
||
204 |
"/ thisContext markForHandle. -- same as above pragma |
|
205 |
aBlock value. "the real logic is in Exception>>doRaise" |
|
206 |
||
207 |
" |
|
208 |
Notification answer:true do:[ |
|
209 |
Transcript showCR:'query answers: ' , (Query query printString). |
|
210 |
] |
|
211 |
" |
|
4443 | 212 |
|
213 |
" |
|
4550 | 214 |
Notification answer:false do:[ |
215 |
Transcript showCR:'first query answers: ' , (Query query printString). |
|
216 |
Query answer:true do:[ |
|
217 |
Transcript showCR:'second query answers: ' , (Query query printString). |
|
218 |
] |
|
219 |
] |
|
4443 | 220 |
" |
221 |
||
4550 | 222 |
"Created: / 10.7.1996 / 15:08:20 / cg" |
223 |
"Modified: / 14.10.1996 / 16:59:18 / cg" |
|
224 |
"Modified: / 25.7.1999 / 23:12:19 / stefan" |
|
225 |
! ! |
|
226 |
||
227 |
!Notification class methodsFor:'queries'! |
|
228 |
||
229 |
accepts:aSignal |
|
230 |
"return true, if the receiver accepts the argument, aSignal. |
|
231 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
232 |
||
233 |
|s| |
|
234 |
||
235 |
self == aSignal ifTrue:[^ true]. |
|
236 |
aSignal isQuerySignal ifFalse:[^ false]. |
|
237 |
||
238 |
s := aSignal. |
|
239 |
[s notNil] whileTrue:[ |
|
240 |
self == s ifTrue:[^ true]. |
|
241 |
s := s parent |
|
242 |
]. |
|
243 |
^ false |
|
244 |
||
245 |
"Modified: / 22.3.1999 / 12:45:32 / stefan" |
|
246 |
"Created: / 23.7.1999 / 15:18:00 / stefan" |
|
247 |
! |
|
248 |
||
249 |
defaultAnswer |
|
250 |
"Return the default answer to the Query. This method is called, |
|
251 |
if nobody catches the signal. |
|
252 |
||
253 |
Subclasses may redefine this method." |
|
254 |
||
255 |
"Calling raiseRequest here will execute the exception's action method" |
|
256 |
||
257 |
^ super raiseRequest |
|
258 |
||
259 |
"Created: / 23.7.1999 / 15:16:03 / stefan" |
|
260 |
! |
|
261 |
||
262 |
handlerForSignal:signal context:theContext originator:originator |
|
263 |
"answer the handler block for the signal from originator. |
|
264 |
The block is retrieved from aContext. |
|
265 |
Answer nil if the signal is not handled" |
|
266 |
||
267 |
|arg| |
|
268 |
||
269 |
theContext selector == #'answer:do:' ifTrue:[ |
|
270 |
(self == signal or:[self accepts:signal]) ifTrue:[ |
|
271 |
arg := theContext argAt:1. |
|
272 |
^ [:ex| ex proceedWith:arg]. |
|
273 |
] |
|
274 |
] ifFalse:[ |
|
275 |
^ super handlerForSignal:signal context:theContext originator:originator. |
|
276 |
]. |
|
277 |
||
278 |
^ nil |
|
279 |
||
280 |
"Created: / 25.7.1999 / 23:11:55 / stefan" |
|
281 |
! |
|
282 |
||
283 |
isQuerySignal |
|
284 |
||
285 |
^ true |
|
286 |
||
287 |
"Created: / 23.7.1999 / 14:59:50 / stefan" |
|
288 |
! |
|
289 |
||
290 |
query |
|
291 |
"raise the query - return the handlers value, or the default |
|
292 |
value, if there is no handler. |
|
293 |
Invoking the handler is exactly the functionality of Signal>>raiseRequest, |
|
294 |
but we can do it faster here." |
|
295 |
||
296 |
|con s| |
|
297 |
||
298 |
con := Context findFirstSpecialHandle:true raise:false. |
|
299 |
[con notNil] whileTrue:[ |
|
300 |
(con selector == #answer:do:) ifFalse:[ |
|
301 |
^ super raiseRequest |
|
302 |
]. |
|
303 |
(s := con receiver) == self ifTrue:[ |
|
304 |
^ con argAt:1 |
|
305 |
]. |
|
306 |
(s == self or:[s accepts:self]) ifTrue:[ |
|
307 |
^ super raiseRequest |
|
308 |
]. |
|
309 |
con := con findSpecialHandle:true raise:false. |
|
310 |
]. |
|
311 |
||
312 |
"/ no handler found - return the default value |
|
313 |
^ self defaultAnswer |
|
314 |
||
315 |
"Modified: / 15.6.1998 / 21:27:37 / cg" |
|
316 |
"Modified: / 25.7.1999 / 23:15:16 / stefan" |
|
317 |
! |
|
318 |
||
319 |
raise |
|
320 |
"QuerySignals are proceedable by definition, |
|
321 |
so they should be raised with #query or #raiseRequest" |
|
322 |
||
323 |
^ self shouldNotImplement |
|
324 |
||
325 |
"Created: / 23.7.1999 / 15:19:17 / stefan" |
|
326 |
! |
|
327 |
||
328 |
raiseRequest |
|
329 |
"redefined to use #query" |
|
330 |
||
331 |
^ self query |
|
332 |
||
333 |
"Created: / 25.7.1999 / 23:25:59 / stefan" |
|
4443 | 334 |
! ! |
335 |
||
336 |
!Notification methodsFor:'default actions'! |
|
337 |
||
4525 | 338 |
defaultAction |
4550 | 339 |
"the default action is to return the default value. |
340 |
Subclasses may redefine this" |
|
4443 | 341 |
|
4550 | 342 |
^ nil |
4443 | 343 |
|
4550 | 344 |
"Modified: / 23.7.1999 / 15:13:34 / stefan" |
4443 | 345 |
! ! |
346 |
||
347 |
!Notification class methodsFor:'documentation'! |
|
348 |
||
349 |
version |
|
4554 | 350 |
^ '$Header: /cvs/stx/stx/libbasic/Notification.st,v 1.7 1999-08-04 16:57:51 stefan Exp $' |
4443 | 351 |
! ! |
352 |
Notification initialize! |