author | Claus Gittinger <cg@exept.de> |
Wed, 10 Jul 1996 15:08:44 +0200 | |
changeset 1534 | 3aac2f9ef96c |
parent 1422 | 9a0b792f2953 |
child 1734 | c05da5185472 |
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 |
||
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
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 |
43 |
(as opposed to normal signals, which raise an unhandled signal exception). |
|
1363 | 44 |
QuerySignals are also ignored, if a handler exists, but rejects. |
1248 | 45 |
|
1363 | 46 |
Their main use is to implement up-Queries via signals, that work even |
47 |
if intermediate errorSignal handlers are present |
|
48 |
(which is not possible with ordinary signals, since errorSignal handlers |
|
49 |
would catch those signals). |
|
50 |
||
1248 | 51 |
Code deep down in the calling hierarchy can post such an up-Query to ask |
52 |
for some information in return or to pass some information upward. |
|
580 | 53 |
|
1248 | 54 |
For example, the activityNotification mechanism is built on top of this: |
55 |
everyone can send such a notification which is either handled by someone |
|
56 |
up in the hierarchy (to show it in the windows info area) or simply |
|
57 |
ignored. |
|
58 |
||
59 |
Using QuerySignals for this (instead of regular Signals) helps in documenting |
|
60 |
the intended usage of those signals. |
|
1275 | 61 |
|
62 |
[see also:] |
|
63 |
Signal SignalSet Exception |
|
64 |
Object |
|
1363 | 65 |
(``Exception handling and signals'': programming/exceptions.html) |
1292 | 66 |
|
67 |
[author:] |
|
68 |
Claus Gittinger |
|
580 | 69 |
" |
70 |
! |
|
71 |
||
72 |
examples |
|
73 |
" |
|
1379 | 74 |
an up-query from a deeply nested operation to a higher level: |
1316 | 75 |
[exBegin] |
1379 | 76 |
|querySignal zero| |
580 | 77 |
|
1379 | 78 |
zero := 0. |
79 |
querySignal := QuerySignal new. |
|
80 |
querySignal handle:[:ex | |
|
1422 | 81 |
Transcript showCR:'query'. |
1379 | 82 |
ex proceedWith:true |
83 |
] do:[ |
|
84 |
'nesting'. |
|
85 |
[ |
|
86 |
[ |
|
87 |
Object errorSignal handle:[:ex | |
|
1422 | 88 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 89 |
ex proceed |
90 |
] do:[ |
|
91 |
[ |
|
92 |
1 // zero. 'an error which is cought in the handler'. |
|
93 |
(querySignal raise) == true ifTrue:[ |
|
1422 | 94 |
Transcript showCR:'query says: ok'. |
1379 | 95 |
] ifFalse:[ |
1422 | 96 |
Transcript showCR:'query says: no' |
1379 | 97 |
] |
98 |
] value |
|
99 |
] |
|
100 |
] value |
|
101 |
] value |
|
102 |
] |
|
103 |
[exEnd] |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
104 |
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
|
105 |
(which is also easier to read): |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
106 |
[exBegin] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
107 |
|querySignal| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
108 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
109 |
querySignal := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
110 |
querySignal answer:true do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
111 |
'nesting'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
112 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
113 |
[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
114 |
(querySignal raise) == true ifTrue:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
115 |
Transcript showCR:'query says: ok'. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
116 |
] ifFalse:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
117 |
Transcript showCR:'query says: no' |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
118 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
119 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
120 |
] value |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
121 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
122 |
[exEnd] |
1379 | 123 |
an up-query from a deeply nested operation, for which there |
124 |
is no handler: |
|
125 |
(notice, this would not work with normal signals, which would raise |
|
126 |
another unhandled exception-exception; |
|
127 |
also notice the == check #raise's return value being true, |
|
128 |
instead of a simple ifTrue; this handles a nil-value from |
|
129 |
the unhandled query) |
|
130 |
[exBegin] |
|
131 |
|querySignal zero| |
|
132 |
||
133 |
zero := 0. |
|
134 |
querySignal := QuerySignal new. |
|
135 |
||
136 |
[ |
|
137 |
'nesting'. |
|
138 |
[ |
|
139 |
[ |
|
140 |
Object errorSignal handle:[:ex | |
|
1422 | 141 |
Transcript showCR:'some error: ' , ex errorString. |
1379 | 142 |
ex proceed |
143 |
] do:[ |
|
144 |
[ |
|
145 |
1 // zero. 'an error which is cought in the handler'. |
|
146 |
(querySignal raise) == true ifTrue:[ |
|
1422 | 147 |
Transcript showCR:'query says: ok'. |
1379 | 148 |
] ifFalse:[ |
1422 | 149 |
Transcript showCR:'query says: no' |
1379 | 150 |
] |
151 |
] value |
|
152 |
] |
|
153 |
] value |
|
154 |
] value |
|
155 |
] value |
|
156 |
[exEnd] |
|
157 |
counter-example, just to show that things would not work this way |
|
158 |
with regular signals: |
|
159 |
[exBegin] |
|
160 |
|signal| |
|
161 |
||
162 |
signal := Signal new. |
|
163 |
'nesting deeply'. |
|
164 |
[ |
|
165 |
[ |
|
1316 | 166 |
[ |
167 |
[ |
|
1379 | 168 |
[ |
169 |
(signal raise) == true ifTrue:[ |
|
1422 | 170 |
Transcript showCR:'query says: ok'. |
1379 | 171 |
] ifFalse:[ |
1422 | 172 |
Transcript showCR:'query says: no' |
1379 | 173 |
] |
174 |
] value |
|
1316 | 175 |
] value |
176 |
] value |
|
1379 | 177 |
] value |
178 |
] value |
|
179 |
[exEnd] |
|
180 |
||
181 |
except, by handling the unhandled exception |
|
182 |
(but we think, that querySignals are easier to use and |
|
183 |
better document the intent): |
|
184 |
[exBegin] |
|
185 |
|signal| |
|
186 |
||
187 |
signal := Signal new. |
|
188 |
'nesting deeply'. |
|
189 |
[ |
|
190 |
[ |
|
191 |
[ |
|
192 |
[ |
|
193 |
[ |
|
194 |
Signal noHandlerSignal handle:[:ex | |
|
195 |
ex proceedWith:nil |
|
196 |
] do:[ |
|
197 |
(signal raise) == true ifTrue:[ |
|
1422 | 198 |
Transcript showCR:'query says: ok'. |
1379 | 199 |
] ifFalse:[ |
1422 | 200 |
Transcript showCR:'query says: no' |
1379 | 201 |
] |
202 |
] |
|
203 |
] value |
|
204 |
] value |
|
205 |
] value |
|
206 |
] value |
|
207 |
] value |
|
208 |
[exEnd] |
|
420 | 209 |
" |
210 |
! ! |
|
211 |
||
212 |
!QuerySignal methodsFor:'queries'! |
|
213 |
||
214 |
accepts:aSignal |
|
215 |
"return true, if the receiver accepts the argument, aSignal. |
|
216 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
217 |
||
218 |
|s| |
|
219 |
||
421 | 220 |
aSignal isQuerySignal ifFalse:[^ false]. |
420 | 221 |
|
222 |
s := aSignal. |
|
223 |
[s notNil] whileTrue:[ |
|
224 |
self == s ifTrue:[^ true]. |
|
225 |
s := s parent |
|
226 |
]. |
|
227 |
^ false |
|
691 | 228 |
! |
229 |
||
230 |
isQuerySignal |
|
1249 | 231 |
"return true, if this is a querySignal - always return true here" |
232 |
||
691 | 233 |
^ true |
1249 | 234 |
|
235 |
"Modified: 22.4.1996 / 13:45:10 / cg" |
|
420 | 236 |
! ! |
691 | 237 |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
238 |
!QuerySignal methodsFor:'save evaluation'! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
239 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
240 |
answer:someAnswer do:aBlock |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
241 |
"evaluate the argument, aBlock. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
242 |
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
|
243 |
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
|
244 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
245 |
^ self handle:[:ex | ex proceedWith:someAnswer] do:aBlock. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
246 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
247 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
248 |
|q| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
249 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
250 |
q := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
251 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
252 |
q answer:true do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
253 |
Transcript showCR:'query answers: ' , (q raise printString). |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
254 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
255 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
256 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
257 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
258 |
|q| |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
259 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
260 |
q := QuerySignal new. |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
261 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
262 |
q answer:false do:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
263 |
Transcript showCR:'query answers: ' , (q raise printString). |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
264 |
q answer:true in:[ |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
265 |
Transcript showCR:'query answers: ' , (q raise printString). |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
266 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
267 |
] |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
268 |
" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
269 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
270 |
"Modified: 10.7.1996 / 15:05:56 / cg" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
271 |
"Created: 10.7.1996 / 15:08:20 / cg" |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
272 |
! ! |
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
273 |
|
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
274 |
!QuerySignal class methodsFor:'documentation'! |
691 | 275 |
|
276 |
version |
|
1534
3aac2f9ef96c
added #answer:do: - easier to read
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
277 |
^ '$Header: /cvs/stx/stx/libbasic/QuerySignal.st,v 1.14 1996-07-10 13:08:44 cg Exp $' |
691 | 278 |
! ! |