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