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