author | Claus Gittinger <cg@exept.de> |
Sat, 06 Sep 1997 19:22:50 +0200 | |
changeset 2900 | 2153bcddc299 |
parent 2893 | 8ba406da6b22 |
child 3307 | 08ad28464911 |
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 |
||
2893 | 13 |
'From Smalltalk/X, Version:3.1.9 on 31-aug-1997 at 8:06:37 pm' ! |
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'. |
1379 | 122 |
(querySignal raise) == 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 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
143 |
(querySignal raise) == true ifTrue:[ |
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 |
|
244 |
"evaluate the argument, aBlock. |
|
245 |
If the receiver is queried during evaluation, answer with someAnswer. |
|
246 |
This is a wrapper for #handle:do: for lazy typists; no new functionality." |
|
247 |
||
248 |
^ self handle:[:ex | ex proceedWith:someAnswer] do:aBlock. |
|
249 |
||
250 |
" |
|
251 |
|q| |
|
252 |
||
253 |
q := QuerySignal new. |
|
254 |
||
255 |
q answer:true do:[ |
|
256 |
Transcript showCR:'query answers: ' , (q raise printString). |
|
257 |
] |
|
258 |
" |
|
259 |
||
260 |
" |
|
261 |
|q| |
|
262 |
||
263 |
q := QuerySignal new. |
|
264 |
||
265 |
q answer:false do:[ |
|
266 |
Transcript showCR:'first query answers: ' , (q raise printString). |
|
267 |
q answer:true do:[ |
|
268 |
Transcript showCR:'second query answers: ' , (q raise printString). |
|
269 |
] |
|
270 |
] |
|
271 |
" |
|
272 |
||
273 |
"Created: 10.7.1996 / 15:08:20 / cg" |
|
274 |
"Modified: 14.10.1996 / 16:59:18 / cg" |
|
275 |
! ! |
|
276 |
||
2037 | 277 |
!QuerySignal methodsFor:'initialization'! |
278 |
||
279 |
defaultAnswer:someValue |
|
280 |
"define the queries defaultAnswer to be someValue. |
|
281 |
This is the same as defining an appropriate handlerBlock." |
|
282 |
||
283 |
|handler| |
|
284 |
||
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
285 |
defaultAnswer := someValue. |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
286 |
hasDefault := true. |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
287 |
|
2037 | 288 |
"/ avoid creating a fullBlock, in the most common cases |
289 |
someValue == true ifTrue:[ |
|
290 |
handler := [:ex | ex proceedWith:true] |
|
291 |
] ifFalse:[ |
|
292 |
someValue == false ifTrue:[ |
|
293 |
handler := [:ex | ex proceedWith:false] |
|
294 |
] ifFalse:[ |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
295 |
someValue isNil ifTrue:[ |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
296 |
handler := [:ex | ex proceedWith:nil] |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
297 |
] ifFalse:[ |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
298 |
handler := [:ex | ex proceedWith:someValue]. |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
299 |
] |
2037 | 300 |
] |
301 |
]. |
|
302 |
self handlerBlock:handler. |
|
303 |
||
304 |
" |
|
305 |
QuerySignal new raise |
|
306 |
(QuerySignal new defaultAnswer:true) raise |
|
307 |
(QuerySignal new defaultAnswer:false) raise |
|
308 |
" |
|
309 |
||
310 |
" |
|
311 |
|sig rslt| |
|
312 |
||
313 |
sig := QuerySignal new. |
|
314 |
sig defaultAnswer:false. |
|
315 |
rslt := sig raise. |
|
316 |
Transcript showCR:rslt. |
|
317 |
" |
|
318 |
||
319 |
" |
|
320 |
|sig rslt| |
|
321 |
||
322 |
sig := QuerySignal new. |
|
323 |
sig defaultAnswer:false. |
|
324 |
sig answer:true |
|
325 |
do:[ |
|
326 |
rslt := sig raise |
|
327 |
]. |
|
328 |
Transcript showCR:rslt. |
|
329 |
" |
|
330 |
||
331 |
"Modified: 3.1.1997 / 15:14:40 / cg" |
|
332 |
! ! |
|
333 |
||
420 | 334 |
!QuerySignal methodsFor:'queries'! |
335 |
||
336 |
accepts:aSignal |
|
337 |
"return true, if the receiver accepts the argument, aSignal. |
|
338 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
339 |
||
340 |
|s| |
|
341 |
||
421 | 342 |
aSignal isQuerySignal ifFalse:[^ false]. |
420 | 343 |
|
344 |
s := aSignal. |
|
345 |
[s notNil] whileTrue:[ |
|
346 |
self == s ifTrue:[^ true]. |
|
347 |
s := s parent |
|
348 |
]. |
|
349 |
^ false |
|
691 | 350 |
! |
351 |
||
352 |
isQuerySignal |
|
1249 | 353 |
"return true, if this is a querySignal - always return true here" |
354 |
||
691 | 355 |
^ true |
1249 | 356 |
|
357 |
"Modified: 22.4.1996 / 13:45:10 / cg" |
|
2893 | 358 |
! |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
359 |
|
2893 | 360 |
raise |
361 |
"raise the query - return the handlers value, or the default |
|
362 |
value, if there is no handler. |
|
363 |
This is exactly the functionality of my inherited method, |
|
364 |
but we can do it faster here." |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
365 |
|
2893 | 366 |
|con| |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
367 |
|
2893 | 368 |
con := thisContext sender. |
369 |
[con notNil] whileTrue:[ |
|
370 |
con := con findNextContextWithSelector:#doRaise |
|
371 |
or:#handle:do: |
|
372 |
or:#handle:from:do:. |
|
373 |
con notNil ifTrue:[ |
|
374 |
(con selector == #handle:do:) ifFalse:[ |
|
375 |
^ super raise |
|
376 |
]. |
|
377 |
(con receiver == self) ifTrue:[ |
|
378 |
"/ found a non-busy handler ... |
|
379 |
"/ if its sender is a #answer context, |
|
380 |
"/ fetch its value quickly from it. |
|
381 |
con := con sender. |
|
382 |
con selector == #answer:do: ifFalse:[ |
|
383 |
con receiver == self ifFalse:[ |
|
384 |
^ super raise |
|
385 |
] |
|
386 |
]. |
|
387 |
^ con argAt:1 |
|
388 |
] |
|
389 |
] |
|
390 |
]. |
|
391 |
"/ no handler found - return the default value |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
392 |
hasDefault == true ifTrue:[ |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
393 |
^ defaultAnswer |
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
394 |
]. |
2893 | 395 |
handlerBlock isNil ifTrue:[ |
396 |
^ nil |
|
397 |
]. |
|
398 |
^ super raise |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
399 |
|
2893 | 400 |
"Modified: 31.8.1997 / 08:05:32 / cg" |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
401 |
! ! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
402 |
|
1954 | 403 |
!QuerySignal class methodsFor:'documentation'! |
691 | 404 |
|
405 |
version |
|
2900
2153bcddc299
faster return of defaultValue
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
406 |
^ '$Header: /cvs/stx/stx/libbasic/QuerySignal.st,v 1.21 1997-09-06 17:22:50 cg Exp $' |
691 | 407 |
! ! |