author | Stefan Vogel <sv@exept.de> |
Fri, 21 Jan 2005 11:33:05 +0100 | |
changeset 8686 | 708fa53592f6 |
parent 6790 | 34f661b45d13 |
child 8693 | e9543a43bb71 |
permissions | -rw-r--r-- |
420 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5871 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
420 | 15 |
Signal subclass:#QuerySignal |
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
16 |
instanceVariableNames:'defaultAnswer hasDefault' |
1248 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Exceptions' |
|
420 | 20 |
! |
21 |
||
1954 | 22 |
!QuerySignal class methodsFor:'documentation'! |
420 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
QuerySignals are like signals, except that they are not accepted |
|
41 |
by handlers for ordinary signals. |
|
42 |
I.e. a signal handler for a normal signal will not handle a query |
|
580 | 43 |
signal. Thus, these bypass anySignal handlers. |
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
44 |
|
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
45 |
However, if unhandled, no error is raised, instead they are simply ignored |
1954 | 46 |
and nil is returned from the raise |
1248 | 47 |
(as opposed to normal signals, which raise an unhandled signal exception). |
1363 | 48 |
QuerySignals are also ignored, if a handler exists, but rejects. |
1248 | 49 |
|
1954 | 50 |
Their main use is to implement upQueries via signals, that work even |
1363 | 51 |
if intermediate errorSignal handlers are present |
52 |
(which is not possible with ordinary signals, since errorSignal handlers |
|
53 |
would catch those signals). |
|
54 |
||
1248 | 55 |
Code deep down in the calling hierarchy can post such an up-Query to ask |
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
56 |
for some information or to pass some information upward. |
580 | 57 |
|
1248 | 58 |
For example, the activityNotification mechanism is built on top of this: |
59 |
everyone can send such a notification which is either handled by someone |
|
60 |
up in the hierarchy (to show it in the windows info area) or simply |
|
61 |
ignored. |
|
62 |
||
63 |
Using QuerySignals for this (instead of regular Signals) helps in documenting |
|
64 |
the intended usage of those signals. |
|
1275 | 65 |
|
1954 | 66 |
Another use of querySignals is to provide additional information to |
67 |
deeply nested methods, which is only required in the uncommon case; |
|
68 |
or if another parameter is required by some method, which was not planned |
|
69 |
for in the beginning, and you do not want to hand this value (via an |
|
70 |
additional argument) through all intermediate levels. |
|
71 |
A highly elegant solution to this problem is to provide a handler somewhere |
|
72 |
at the top of the calling hierarchy, and raise an upQuery from whereever |
|
73 |
that value is required. |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
74 |
A concrete application can be found in the windowGroup-lastEvent |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
75 |
queries. If anyone is interested in the windowEvent which was responible for |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
76 |
being invoked, all he needs to do is to raise the lastEventQuerySignal, |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
77 |
which returns that event. |
1954 | 78 |
No intermediate methods are required to know anything about that. |
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
79 |
Another example is found in the way Metaclass asks for the nameSpace |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
80 |
when new classes are to be installed. A Browser may simply answer such |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
81 |
a query and provide a namespace (no need to pass that information down |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
82 |
the calling chain). |
1954 | 83 |
|
84 |
A final note (to C++ and Java fans): |
|
85 |
such upQueries are only possible, if the exception handling mechanism |
|
86 |
does not automatically unwind the stack for the handler invokation. |
|
87 |
Since the handler must be able to proceed the execution and return |
|
88 |
a value to the raiser .... |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
89 |
... another demonstration of why ST's exception mechanisms are superior. |
1954 | 90 |
|
1275 | 91 |
[see also:] |
92 |
Signal SignalSet Exception |
|
93 |
Object |
|
1363 | 94 |
(``Exception handling and signals'': programming/exceptions.html) |
1292 | 95 |
|
96 |
[author:] |
|
97 |
Claus Gittinger |
|
580 | 98 |
" |
99 |
! |
|
100 |
||
101 |
examples |
|
102 |
" |
|
1379 | 103 |
an up-query from a deeply nested operation to a higher level: |
1316 | 104 |
[exBegin] |
1379 | 105 |
|querySignal zero| |
580 | 106 |
|
1379 | 107 |
zero := 0. |
108 |
querySignal := QuerySignal new. |
|
109 |
querySignal handle:[:ex | |
|
1422 | 110 |
Transcript showCR:'query'. |
1379 | 111 |
ex proceedWith:true |
112 |
] do:[ |
|
113 |
'nesting'. |
|
114 |
[ |
|
115 |
[ |
|
116 |
Object errorSignal handle:[:ex | |
|
1422 | 117 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 118 |
ex proceed |
119 |
] do:[ |
|
120 |
[ |
|
2689 | 121 |
1 // zero. 'an error which is caught in the handler'. |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
122 |
(querySignal query) == true ifTrue:[ |
1422 | 123 |
Transcript showCR:'query says: ok'. |
1379 | 124 |
] ifFalse:[ |
1422 | 125 |
Transcript showCR:'query says: no' |
1379 | 126 |
] |
127 |
] value |
|
128 |
] |
|
129 |
] value |
|
130 |
] value |
|
131 |
] |
|
132 |
[exEnd] |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
133 |
for lazy typists, a more compact interface is also provided |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
134 |
(which is also easier to read): |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
135 |
[exBegin] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
136 |
|querySignal| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
137 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
138 |
querySignal := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
139 |
querySignal answer:true do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
140 |
'nesting'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
141 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
142 |
[ |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
143 |
(querySignal query) == true ifTrue:[ |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
144 |
Transcript showCR:'query says: ok'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
145 |
] ifFalse:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
146 |
Transcript showCR:'query says: no' |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
147 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
148 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
149 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
150 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
151 |
[exEnd] |
1379 | 152 |
an up-query from a deeply nested operation, for which there |
153 |
is no handler: |
|
154 |
(notice, this would not work with normal signals, which would raise |
|
155 |
another unhandled exception-exception; |
|
156 |
also notice the == check #raise's return value being true, |
|
157 |
instead of a simple ifTrue; this handles a nil-value from |
|
158 |
the unhandled query) |
|
159 |
[exBegin] |
|
160 |
|querySignal zero| |
|
161 |
||
162 |
zero := 0. |
|
163 |
querySignal := QuerySignal new. |
|
164 |
||
165 |
[ |
|
166 |
'nesting'. |
|
167 |
[ |
|
168 |
[ |
|
169 |
Object errorSignal handle:[:ex | |
|
1422 | 170 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 171 |
ex proceed |
172 |
] do:[ |
|
173 |
[ |
|
2689 | 174 |
1 // zero. 'an error which is caught in the handler'. |
1379 | 175 |
(querySignal raise) == true ifTrue:[ |
1422 | 176 |
Transcript showCR:'query says: ok'. |
1379 | 177 |
] ifFalse:[ |
1422 | 178 |
Transcript showCR:'query says: no' |
1379 | 179 |
] |
180 |
] value |
|
181 |
] |
|
182 |
] value |
|
183 |
] value |
|
184 |
] value |
|
185 |
[exEnd] |
|
186 |
counter-example, just to show that things would not work this way |
|
187 |
with regular signals: |
|
188 |
[exBegin] |
|
189 |
|signal| |
|
190 |
||
191 |
signal := Signal new. |
|
192 |
'nesting deeply'. |
|
193 |
[ |
|
194 |
[ |
|
1316 | 195 |
[ |
196 |
[ |
|
1379 | 197 |
[ |
198 |
(signal raise) == true ifTrue:[ |
|
1422 | 199 |
Transcript showCR:'query says: ok'. |
1379 | 200 |
] ifFalse:[ |
1422 | 201 |
Transcript showCR:'query says: no' |
1379 | 202 |
] |
203 |
] value |
|
1316 | 204 |
] value |
205 |
] value |
|
1379 | 206 |
] value |
207 |
] value |
|
208 |
[exEnd] |
|
209 |
||
210 |
except, by handling the unhandled exception |
|
211 |
(but we think, that querySignals are easier to use and |
|
212 |
better document the intent): |
|
213 |
[exBegin] |
|
214 |
|signal| |
|
215 |
||
216 |
signal := Signal new. |
|
217 |
'nesting deeply'. |
|
218 |
[ |
|
219 |
[ |
|
220 |
[ |
|
221 |
[ |
|
222 |
[ |
|
223 |
Signal noHandlerSignal handle:[:ex | |
|
224 |
ex proceedWith:nil |
|
225 |
] do:[ |
|
226 |
(signal raise) == true ifTrue:[ |
|
1422 | 227 |
Transcript showCR:'query says: ok'. |
1379 | 228 |
] ifFalse:[ |
1422 | 229 |
Transcript showCR:'query says: no' |
1379 | 230 |
] |
231 |
] |
|
232 |
] value |
|
233 |
] value |
|
234 |
] value |
|
235 |
] value |
|
236 |
] value |
|
237 |
[exEnd] |
|
420 | 238 |
" |
239 |
! ! |
|
240 |
||
2893 | 241 |
!QuerySignal methodsFor:'answering queries'! |
242 |
||
243 |
answer:someAnswer do:aBlock |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
244 |
"evaluate the argument, aBlock. |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
245 |
If the receiver is queried during evaluation, answer with someAnswer. |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
246 |
This is a wrapper for #handle:do: for lazy typists; no new functionality." |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
247 |
|
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
248 |
<exception: #handle> |
2893 | 249 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
250 |
"/ thisContext markForHandle. -- same as above pragma |
5227
73a2dc34cb42
#answer:do: now returns the evaluated blocks value
Claus Gittinger <cg@exept.de>
parents:
4908
diff
changeset
|
251 |
^ aBlock value. "the real logic is in Exception>>doRaise" |
2893 | 252 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
253 |
" |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
254 |
|q| |
2893 | 255 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
256 |
q := QuerySignal new. |
2893 | 257 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
258 |
q answer:true do:[ |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
259 |
Transcript showCR:'query answers: ' , (q query printString). |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
260 |
] |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
261 |
" |
2893 | 262 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
263 |
" |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
264 |
|q| |
2893 | 265 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
266 |
q := QuerySignal new. |
2893 | 267 |
|
4491
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
268 |
q answer:false do:[ |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
269 |
Transcript showCR:'first query answers: ' , (q query printString). |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
270 |
q answer:true do:[ |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
271 |
Transcript showCR:'second query answers: ' , (q query printString). |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
272 |
] |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
273 |
] |
5041cae5651c
use new pragma to flag exception frames.
Claus Gittinger <cg@exept.de>
parents:
4466
diff
changeset
|
274 |
" |
2893 | 275 |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
276 |
"Created: / 10.7.1996 / 15:08:20 / cg" |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
277 |
"Modified: / 14.10.1996 / 16:59:18 / cg" |
4464
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
278 |
"Modified: / 25.7.1999 / 23:34:02 / stefan" |
2893 | 279 |
! ! |
280 |
||
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
281 |
!QuerySignal methodsFor:'exception creation'! |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
282 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
283 |
newException |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
284 |
"{ Pragma: +inlineNew }" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
285 |
"answer a new exception object for this signal" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
286 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
287 |
^ Notification basicNew setSignal:self. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
288 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
289 |
" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
290 |
|querySignal| |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
291 |
querySignal := QuerySignal new. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
292 |
querySignal handle:[:ex| self halt] do:[querySignal raiseRequest]. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
293 |
" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
294 |
! ! |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
295 |
|
2037 | 296 |
!QuerySignal methodsFor:'initialization'! |
297 |
||
298 |
defaultAnswer:someValue |
|
299 |
"define the queries defaultAnswer to be someValue. |
|
300 |
This is the same as defining an appropriate handlerBlock." |
|
301 |
||
302 |
|handler| |
|
303 |
||
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
304 |
defaultAnswer := someValue. |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
305 |
hasDefault := true. |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
306 |
|
2037 | 307 |
"/ avoid creating a fullBlock, in the most common cases |
4514 | 308 |
"/ therefore, do not replace the code below by |
309 |
"/ handler := [:ex | ex proceedWith:someValue] |
|
310 |
"/ although it is semantically equivalent. |
|
311 |
||
2037 | 312 |
someValue == true ifTrue:[ |
313 |
handler := [:ex | ex proceedWith:true] |
|
314 |
] ifFalse:[ |
|
315 |
someValue == false ifTrue:[ |
|
316 |
handler := [:ex | ex proceedWith:false] |
|
317 |
] ifFalse:[ |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
318 |
someValue isNil ifTrue:[ |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
319 |
handler := [:ex | ex proceedWith:nil] |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
320 |
] ifFalse:[ |
6790 | 321 |
handler := [:ex | ex proceedWith:someValue value]. |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
322 |
] |
2037 | 323 |
] |
324 |
]. |
|
325 |
self handlerBlock:handler. |
|
326 |
||
327 |
" |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
328 |
QuerySignal new query |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
329 |
(QuerySignal new defaultAnswer:true) query |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
330 |
(QuerySignal new defaultAnswer:false) query |
2037 | 331 |
" |
332 |
||
333 |
" |
|
334 |
|sig rslt| |
|
335 |
||
336 |
sig := QuerySignal new. |
|
337 |
sig defaultAnswer:false. |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
338 |
rslt := sig query. |
2037 | 339 |
Transcript showCR:rslt. |
340 |
" |
|
341 |
||
342 |
" |
|
343 |
|sig rslt| |
|
344 |
||
345 |
sig := QuerySignal new. |
|
346 |
sig defaultAnswer:false. |
|
347 |
sig answer:true |
|
348 |
do:[ |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
349 |
rslt := sig query |
2037 | 350 |
]. |
351 |
Transcript showCR:rslt. |
|
352 |
" |
|
353 |
||
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
354 |
"Modified: / 3.1.1997 / 15:14:40 / cg" |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
355 |
"Modified: / 2.3.1998 / 10:42:14 / stefan" |
2037 | 356 |
! ! |
357 |
||
420 | 358 |
!QuerySignal methodsFor:'queries'! |
359 |
||
360 |
accepts:aSignal |
|
361 |
"return true, if the receiver accepts the argument, aSignal. |
|
362 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
363 |
||
364 |
|s| |
|
365 |
||
4527 | 366 |
self == aSignal ifTrue:[^ true]. |
421 | 367 |
aSignal isQuerySignal ifFalse:[^ false]. |
420 | 368 |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
369 |
s := aSignal parent. |
420 | 370 |
[s notNil] whileTrue:[ |
4061
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
371 |
self == s ifTrue:[^ true]. |
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
372 |
s := s parent |
420 | 373 |
]. |
374 |
^ false |
|
4061
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
375 |
|
4438 | 376 |
"Modified: / 22.3.1999 / 12:45:32 / stefan" |
691 | 377 |
! |
378 |
||
4464
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
379 |
handlerForSignal:signal context:theContext originator:originator |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
380 |
"answer the handler block for the signal from originator. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
381 |
The block is retrieved from aContext. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
382 |
Answer nil if the signal is not handled" |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
383 |
|
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
384 |
|arg| |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
385 |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
386 |
theContext selector == #answer:do: ifTrue:[ |
4514 | 387 |
(self == signal or:[self accepts:signal]) ifTrue:[ |
4464
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
388 |
arg := theContext argAt:1. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
389 |
^ [:ex| ex proceedWith:arg]. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
390 |
] |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
391 |
] ifFalse:[ |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
392 |
^ super handlerForSignal:signal context:theContext originator:originator. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
393 |
]. |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
394 |
|
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
395 |
^ nil |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
396 |
|
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
397 |
"Created: / 25.7.1999 / 23:33:05 / stefan" |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
398 |
! |
cec93c942c14
Use context flag for exception handling instead of searching for
Stefan Vogel <sv@exept.de>
parents:
4442
diff
changeset
|
399 |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
400 |
handles:anException |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
401 |
"return true, if the receiver handles the argument, anException. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
402 |
(i.e. the receiver is anExceptions signal or a parent of it)" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
403 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
404 |
|signal| |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
405 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
406 |
signal := anException signal. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
407 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
408 |
self == signal ifTrue:[^ true]. "quick check" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
409 |
anException isNotification ifFalse:[^ false]. "speed up non-queries by not traversing the parent chain" |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
410 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
411 |
[(signal := signal parent) notNil] whileTrue:[ |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
412 |
self == signal ifTrue:[^ true]. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
413 |
]. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
414 |
^ false |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
415 |
! |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
416 |
|
691 | 417 |
isQuerySignal |
1249 | 418 |
"return true, if this is a querySignal - always return true here" |
419 |
||
691 | 420 |
^ true |
1249 | 421 |
|
422 |
"Modified: 22.4.1996 / 13:45:10 / cg" |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
423 |
! ! |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
424 |
|
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
425 |
!QuerySignal methodsFor:'raising'! |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
426 |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
427 |
query |
2893 | 428 |
"raise the query - return the handlers value, or the default |
4908 | 429 |
value, if there is no handler." |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
430 |
|
4514 | 431 |
^ self raiseRequest |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
432 |
! |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
433 |
|
4908 | 434 |
queryWith:aParameter |
435 |
"raise the query passing a parameter |
|
436 |
- return the handlers value, or the default value, if there is no handler." |
|
437 |
||
438 |
^ self raiseRequestWith:aParameter |
|
439 |
! |
|
440 |
||
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
441 |
raise |
4061
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
442 |
"QuerySignals are proceedable by definition, |
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
443 |
so they should be raised with #query or #raiseRequest" |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
444 |
|
5871 | 445 |
<resource:#obsolete> |
446 |
||
4061
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
447 |
self obsoleteMethodWarning:'use #query or #raiseRequest'. |
4514 | 448 |
^ self raiseRequest |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
449 |
|
4061
ef62e88fd0ac
Use #query instead of #raise when invoking QuerySignals
Stefan Vogel <sv@exept.de>
parents:
3581
diff
changeset
|
450 |
"Modified: / 18.3.1999 / 18:33:57 / stefan" |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
451 |
! |
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
452 |
|
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
453 |
raiseRequest |
4514 | 454 |
"raise the query - return the handlers value, or the default |
455 |
value, if there is no handler. |
|
456 |
Invoking the handler is exactly the functionality of Signal>>raiseRequest, |
|
457 |
but we can do it faster here, by avoiding the creation of a full-blown |
|
458 |
exception object." |
|
459 |
||
5935
f7e334a3fdbb
chech exception/signal against nil, to avoid recursive
Claus Gittinger <cg@exept.de>
parents:
5871
diff
changeset
|
460 |
|con sel sig| |
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
461 |
|
4514 | 462 |
con := Context findFirstSpecialHandle:true raise:false. |
463 |
[con notNil] whileTrue:[ |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
464 |
sel := con selector. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
465 |
sel == #answer:do: ifTrue:[ |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
466 |
sig := con receiver. |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
467 |
sig == self ifTrue:[ |
4514 | 468 |
^ con argAt:1 |
469 |
]. |
|
5935
f7e334a3fdbb
chech exception/signal against nil, to avoid recursive
Claus Gittinger <cg@exept.de>
parents:
5871
diff
changeset
|
470 |
sig isNil ifTrue:[ |
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
471 |
self error:'nil receiver in #answer:do: - send'. |
5935
f7e334a3fdbb
chech exception/signal against nil, to avoid recursive
Claus Gittinger <cg@exept.de>
parents:
5871
diff
changeset
|
472 |
]. |
f7e334a3fdbb
chech exception/signal against nil, to avoid recursive
Claus Gittinger <cg@exept.de>
parents:
5871
diff
changeset
|
473 |
(sig accepts:self) ifTrue:[ |
4514 | 474 |
^ super raiseRequest |
475 |
]. |
|
476 |
] ifFalse:[ |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
477 |
(sel ~~ #handle:do: |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
478 |
or:[(sig := con receiver) == self |
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
479 |
or:[sig accepts:self]]) ifTrue:[ |
4514 | 480 |
^ super raiseRequest |
481 |
]. |
|
482 |
]. |
|
483 |
con := con findSpecialHandle:true raise:false. |
|
484 |
]. |
|
3307
08ad28464911
Use #raiseRequest to invoke the (inherently) proceedable QuerySignal.
Stefan Vogel <sv@exept.de>
parents:
2900
diff
changeset
|
485 |
|
4514 | 486 |
"/ no handler found - return the default value |
487 |
hasDefault == true ifTrue:[ |
|
6790 | 488 |
^ defaultAnswer value |
4514 | 489 |
]. |
490 |
handlerBlock isNil ifTrue:[ |
|
491 |
^ nil |
|
492 |
]. |
|
493 |
^ super raiseRequest |
|
494 |
||
495 |
"Modified: / 15.6.1998 / 21:27:37 / cg" |
|
496 |
"Modified: / 25.7.1999 / 23:35:11 / stefan" |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
497 |
! ! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
498 |
|
1954 | 499 |
!QuerySignal class methodsFor:'documentation'! |
691 | 500 |
|
501 |
version |
|
8686
708fa53592f6
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
6790
diff
changeset
|
502 |
^ '$Header: /cvs/stx/stx/libbasic/QuerySignal.st,v 1.39 2005-01-21 10:33:05 stefan Exp $' |
691 | 503 |
! ! |