author | Claus Gittinger <cg@exept.de> |
Sat, 28 Jun 1997 20:24:11 +0200 | |
changeset 2729 | 184013fb5dfd |
parent 2689 | ada9b102abcf |
child 2893 | 8ba406da6b22 |
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 |
||
13 |
Signal subclass:#QuerySignal |
|
1248 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Kernel-Exceptions' |
|
420 | 18 |
! |
19 |
||
1954 | 20 |
!QuerySignal class methodsFor:'documentation'! |
420 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
QuerySignals are like signals, except that they are not accepted |
|
39 |
by handlers for ordinary signals. |
|
40 |
I.e. a signal handler for a normal signal will not handle a query |
|
580 | 41 |
signal. Thus, these bypass anySignal handlers. |
1248 | 42 |
If unhandled, no error is raised, instead they are simply ignored |
1954 | 43 |
and nil is returned from the raise |
1248 | 44 |
(as opposed to normal signals, which raise an unhandled signal exception). |
1363 | 45 |
QuerySignals are also ignored, if a handler exists, but rejects. |
1248 | 46 |
|
1954 | 47 |
Their main use is to implement upQueries via signals, that work even |
1363 | 48 |
if intermediate errorSignal handlers are present |
49 |
(which is not possible with ordinary signals, since errorSignal handlers |
|
50 |
would catch those signals). |
|
51 |
||
1248 | 52 |
Code deep down in the calling hierarchy can post such an up-Query to ask |
53 |
for some information in return or to pass some information upward. |
|
580 | 54 |
|
1248 | 55 |
For example, the activityNotification mechanism is built on top of this: |
56 |
everyone can send such a notification which is either handled by someone |
|
57 |
up in the hierarchy (to show it in the windows info area) or simply |
|
58 |
ignored. |
|
59 |
||
60 |
Using QuerySignals for this (instead of regular Signals) helps in documenting |
|
61 |
the intended usage of those signals. |
|
1275 | 62 |
|
1954 | 63 |
Another use of querySignals is to provide additional information to |
64 |
deeply nested methods, which is only required in the uncommon case; |
|
65 |
or if another parameter is required by some method, which was not planned |
|
66 |
for in the beginning, and you do not want to hand this value (via an |
|
67 |
additional argument) through all intermediate levels. |
|
68 |
A highly elegant solution to this problem is to provide a handler somewhere |
|
69 |
at the top of the calling hierarchy, and raise an upQuery from whereever |
|
70 |
that value is required. |
|
71 |
A concrete application can be found in the windowGroup- and lastEvent- |
|
2729 | 72 |
queries, provided by the windowGroup object. If anyone is interested |
1954 | 73 |
in the event which was responible for the anyone to be called, all he needs |
74 |
to do is to raise the lastEventQuerySignal, which returns that event. |
|
75 |
No intermediate methods are required to know anything about that. |
|
76 |
||
77 |
A final note (to C++ and Java fans): |
|
78 |
such upQueries are only possible, if the exception handling mechanism |
|
79 |
does not automatically unwind the stack for the handler invokation. |
|
80 |
Since the handler must be able to proceed the execution and return |
|
81 |
a value to the raiser .... |
|
82 |
||
83 |
||
1275 | 84 |
[see also:] |
85 |
Signal SignalSet Exception |
|
86 |
Object |
|
1363 | 87 |
(``Exception handling and signals'': programming/exceptions.html) |
1292 | 88 |
|
89 |
[author:] |
|
90 |
Claus Gittinger |
|
580 | 91 |
" |
92 |
! |
|
93 |
||
94 |
examples |
|
95 |
" |
|
1379 | 96 |
an up-query from a deeply nested operation to a higher level: |
1316 | 97 |
[exBegin] |
1379 | 98 |
|querySignal zero| |
580 | 99 |
|
1379 | 100 |
zero := 0. |
101 |
querySignal := QuerySignal new. |
|
102 |
querySignal handle:[:ex | |
|
1422 | 103 |
Transcript showCR:'query'. |
1379 | 104 |
ex proceedWith:true |
105 |
] do:[ |
|
106 |
'nesting'. |
|
107 |
[ |
|
108 |
[ |
|
109 |
Object errorSignal handle:[:ex | |
|
1422 | 110 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 111 |
ex proceed |
112 |
] do:[ |
|
113 |
[ |
|
2689 | 114 |
1 // zero. 'an error which is caught in the handler'. |
1379 | 115 |
(querySignal raise) == true ifTrue:[ |
1422 | 116 |
Transcript showCR:'query says: ok'. |
1379 | 117 |
] ifFalse:[ |
1422 | 118 |
Transcript showCR:'query says: no' |
1379 | 119 |
] |
120 |
] value |
|
121 |
] |
|
122 |
] value |
|
123 |
] value |
|
124 |
] |
|
125 |
[exEnd] |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
126 |
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
|
127 |
(which is also easier to read): |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
128 |
[exBegin] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
129 |
|querySignal| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
130 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
131 |
querySignal := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
132 |
querySignal answer:true do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
133 |
'nesting'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
134 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
135 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
136 |
(querySignal raise) == true ifTrue:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
137 |
Transcript showCR:'query says: ok'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
138 |
] ifFalse:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
139 |
Transcript showCR:'query says: no' |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
140 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
141 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
142 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
143 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
144 |
[exEnd] |
1379 | 145 |
an up-query from a deeply nested operation, for which there |
146 |
is no handler: |
|
147 |
(notice, this would not work with normal signals, which would raise |
|
148 |
another unhandled exception-exception; |
|
149 |
also notice the == check #raise's return value being true, |
|
150 |
instead of a simple ifTrue; this handles a nil-value from |
|
151 |
the unhandled query) |
|
152 |
[exBegin] |
|
153 |
|querySignal zero| |
|
154 |
||
155 |
zero := 0. |
|
156 |
querySignal := QuerySignal new. |
|
157 |
||
158 |
[ |
|
159 |
'nesting'. |
|
160 |
[ |
|
161 |
[ |
|
162 |
Object errorSignal handle:[:ex | |
|
1422 | 163 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 164 |
ex proceed |
165 |
] do:[ |
|
166 |
[ |
|
2689 | 167 |
1 // zero. 'an error which is caught in the handler'. |
1379 | 168 |
(querySignal raise) == true ifTrue:[ |
1422 | 169 |
Transcript showCR:'query says: ok'. |
1379 | 170 |
] ifFalse:[ |
1422 | 171 |
Transcript showCR:'query says: no' |
1379 | 172 |
] |
173 |
] value |
|
174 |
] |
|
175 |
] value |
|
176 |
] value |
|
177 |
] value |
|
178 |
[exEnd] |
|
179 |
counter-example, just to show that things would not work this way |
|
180 |
with regular signals: |
|
181 |
[exBegin] |
|
182 |
|signal| |
|
183 |
||
184 |
signal := Signal new. |
|
185 |
'nesting deeply'. |
|
186 |
[ |
|
187 |
[ |
|
1316 | 188 |
[ |
189 |
[ |
|
1379 | 190 |
[ |
191 |
(signal raise) == true ifTrue:[ |
|
1422 | 192 |
Transcript showCR:'query says: ok'. |
1379 | 193 |
] ifFalse:[ |
1422 | 194 |
Transcript showCR:'query says: no' |
1379 | 195 |
] |
196 |
] value |
|
1316 | 197 |
] value |
198 |
] value |
|
1379 | 199 |
] value |
200 |
] value |
|
201 |
[exEnd] |
|
202 |
||
203 |
except, by handling the unhandled exception |
|
204 |
(but we think, that querySignals are easier to use and |
|
205 |
better document the intent): |
|
206 |
[exBegin] |
|
207 |
|signal| |
|
208 |
||
209 |
signal := Signal new. |
|
210 |
'nesting deeply'. |
|
211 |
[ |
|
212 |
[ |
|
213 |
[ |
|
214 |
[ |
|
215 |
[ |
|
216 |
Signal noHandlerSignal handle:[:ex | |
|
217 |
ex proceedWith:nil |
|
218 |
] do:[ |
|
219 |
(signal raise) == true ifTrue:[ |
|
1422 | 220 |
Transcript showCR:'query says: ok'. |
1379 | 221 |
] ifFalse:[ |
1422 | 222 |
Transcript showCR:'query says: no' |
1379 | 223 |
] |
224 |
] |
|
225 |
] value |
|
226 |
] value |
|
227 |
] value |
|
228 |
] value |
|
229 |
] value |
|
230 |
[exEnd] |
|
420 | 231 |
" |
232 |
! ! |
|
233 |
||
2037 | 234 |
!QuerySignal methodsFor:'initialization'! |
235 |
||
236 |
defaultAnswer:someValue |
|
237 |
"define the queries defaultAnswer to be someValue. |
|
238 |
This is the same as defining an appropriate handlerBlock." |
|
239 |
||
240 |
|handler| |
|
241 |
||
242 |
"/ avoid creating a fullBlock, in the most common cases |
|
243 |
someValue == true ifTrue:[ |
|
244 |
handler := [:ex | ex proceedWith:true] |
|
245 |
] ifFalse:[ |
|
246 |
someValue == false ifTrue:[ |
|
247 |
handler := [:ex | ex proceedWith:false] |
|
248 |
] ifFalse:[ |
|
249 |
handler := [:ex | ex proceedWith:someValue]. |
|
250 |
] |
|
251 |
]. |
|
252 |
self handlerBlock:handler. |
|
253 |
||
254 |
" |
|
255 |
QuerySignal new raise |
|
256 |
(QuerySignal new defaultAnswer:true) raise |
|
257 |
(QuerySignal new defaultAnswer:false) raise |
|
258 |
" |
|
259 |
||
260 |
" |
|
261 |
|sig rslt| |
|
262 |
||
263 |
sig := QuerySignal new. |
|
264 |
sig defaultAnswer:false. |
|
265 |
rslt := sig raise. |
|
266 |
Transcript showCR:rslt. |
|
267 |
" |
|
268 |
||
269 |
" |
|
270 |
|sig rslt| |
|
271 |
||
272 |
sig := QuerySignal new. |
|
273 |
sig defaultAnswer:false. |
|
274 |
sig answer:true |
|
275 |
do:[ |
|
276 |
rslt := sig raise |
|
277 |
]. |
|
278 |
Transcript showCR:rslt. |
|
279 |
" |
|
280 |
||
281 |
"Modified: 3.1.1997 / 15:14:40 / cg" |
|
282 |
! ! |
|
283 |
||
420 | 284 |
!QuerySignal methodsFor:'queries'! |
285 |
||
286 |
accepts:aSignal |
|
287 |
"return true, if the receiver accepts the argument, aSignal. |
|
288 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
289 |
||
290 |
|s| |
|
291 |
||
421 | 292 |
aSignal isQuerySignal ifFalse:[^ false]. |
420 | 293 |
|
294 |
s := aSignal. |
|
295 |
[s notNil] whileTrue:[ |
|
296 |
self == s ifTrue:[^ true]. |
|
297 |
s := s parent |
|
298 |
]. |
|
299 |
^ false |
|
691 | 300 |
! |
301 |
||
302 |
isQuerySignal |
|
1249 | 303 |
"return true, if this is a querySignal - always return true here" |
304 |
||
691 | 305 |
^ true |
1249 | 306 |
|
307 |
"Modified: 22.4.1996 / 13:45:10 / cg" |
|
420 | 308 |
! ! |
691 | 309 |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
310 |
!QuerySignal methodsFor:'save evaluation'! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
311 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
312 |
answer:someAnswer do:aBlock |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
313 |
"evaluate the argument, aBlock. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
314 |
If the receiver is queried during evaluation, answer with someAnswer. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
315 |
This is a wrapper for #handle:do: for lazy typists; no new functionality." |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
316 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
317 |
^ self handle:[:ex | ex proceedWith:someAnswer] do:aBlock. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
318 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
319 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
320 |
|q| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
321 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
322 |
q := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
323 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
324 |
q answer:true do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
325 |
Transcript showCR:'query answers: ' , (q raise printString). |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
326 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
327 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
328 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
329 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
330 |
|q| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
331 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
332 |
q := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
333 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
334 |
q answer:false do:[ |
1734 | 335 |
Transcript showCR:'first query answers: ' , (q raise printString). |
336 |
q answer:true do:[ |
|
337 |
Transcript showCR:'second query answers: ' , (q raise printString). |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
338 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
339 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
340 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
341 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
342 |
"Created: 10.7.1996 / 15:08:20 / cg" |
1734 | 343 |
"Modified: 14.10.1996 / 16:59:18 / cg" |
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
344 |
! ! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
345 |
|
1954 | 346 |
!QuerySignal class methodsFor:'documentation'! |
691 | 347 |
|
348 |
version |
|
2729 | 349 |
^ '$Header: /cvs/stx/stx/libbasic/QuerySignal.st,v 1.19 1997-06-28 18:24:11 cg Exp $' |
691 | 350 |
! ! |