author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 17 Jun 2015 06:22:00 +0100 | |
branch | jv |
changeset 18487 | 8735bd9eee2f |
parent 18011 | deb0c3355881 |
child 19227 | 5e949760a4e8 |
permissions | -rw-r--r-- |
6432 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
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 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
14 |
Notification subclass:#Query |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Kernel-Exceptions' |
|
19 |
! |
|
20 |
||
21 |
!Query class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
Query is an abstract superclass for queries. |
|
40 |
||
41 |
A query is an exception which by default proceeds if unhandled and returns |
|
42 |
a default value. |
|
43 |
As a class based reimplementation, it replaces and obsoletes the old |
|
44 |
QuerySignal instance based mechanism. |
|
45 |
||
46 |
Note: |
|
47 |
Query does not add/refine any functionality from its superclass. |
|
48 |
It exists for the more descriptive class name only. |
|
49 |
||
11201 | 50 |
Queries are like exceptions, except that they are not accepted |
51 |
by handlers for ordinary exceptions. |
|
52 |
I.e. a handler for a normal exception will not handle a query. |
|
53 |
Thus, these bypass all normal exception handlers. |
|
54 |
||
55 |
However, if unhandled, no error is raised, instead it is simply ignored |
|
56 |
and nil is returned from the raise |
|
57 |
(as opposed to normal exceptions, which raise an unhandled exception error). |
|
58 |
Queries are also ignored, if a handler exists, but rejects. |
|
59 |
||
60 |
The main use of Queries is to implement an upQuery, which works even |
|
61 |
if intermediate errorSignal handlers are present. |
|
62 |
||
63 |
Code deep down in the calling hierarchy can post such an up-Query to ask |
|
64 |
for some information or to pass some information upward. |
|
65 |
||
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 Queries for this (instead of regular Signals) helps in documenting |
|
72 |
the intended usage of those signals. |
|
73 |
||
74 |
Another use of queries 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 |
||
80 |
A highly elegant solution to this problem is to provide a handler somewhere |
|
81 |
at the top of the calling hierarchy, and raise an upQuery from whereever |
|
82 |
that value is required. |
|
83 |
A concrete application can be found in the windowGroup-lastEvent |
|
84 |
queries. If anyone is interested in the windowEvent which was responible for |
|
85 |
being invoked, all he needs to do is to raise the lastEventQuerySignal, |
|
86 |
which returns that event. |
|
87 |
No intermediate methods are required to know anything about that. |
|
88 |
Another example is found in the way Metaclass asks for the nameSpace |
|
89 |
when new classes are to be installed. A Browser may simply answer such |
|
90 |
a query and provide a namespace (no need to pass that information down |
|
91 |
the calling chain). |
|
92 |
||
93 |
A final note (to C++ and Java fans): |
|
94 |
such upQueries are only possible, if the exception handling mechanism |
|
95 |
does not automatically unwind the stack for the handler invokation. |
|
96 |
Since the handler must be able to proceed the execution and return |
|
97 |
a value to the raiser .... |
|
98 |
... another demonstration of why ST's exception mechanisms are superior. |
|
99 |
||
6432 | 100 |
[author:] |
101 |
Claus Gittinger |
|
102 |
||
103 |
[see also:] |
|
11201 | 104 |
Notification Warning Signal QuerySignal |
105 |
(``Exception handling and signals'': programming/exceptions.html) |
|
6432 | 106 |
" |
107 |
! |
|
108 |
||
109 |
examples |
|
110 |
" |
|
111 |
[exBegin] |
|
112 |
Query answer:'hello' |
|
113 |
do:[ |
|
114 |
Transcript showCR:(Query query) |
|
115 |
] |
|
116 |
[exEnd] |
|
117 |
||
118 |
||
119 |
an up-query from a deeply nested operation to a higher level: |
|
120 |
the example below demonstrates that a Query is not an Error |
|
121 |
(i.e. the Error-handler does not interfere with Queries) |
|
122 |
[exBegin] |
|
123 |
|zero| |
|
124 |
||
125 |
zero := 0. |
|
126 |
Query handle:[:ex | |
|
127 |
Transcript showCR:'query'. |
|
128 |
ex proceedWith:true |
|
129 |
] do:[ |
|
130 |
'nesting'. |
|
131 |
[ |
|
132 |
[ |
|
133 |
Error handle:[:ex | |
|
134 |
Transcript showCR:'some error: ' , ex errorString. |
|
135 |
ex proceed |
|
136 |
] do:[ |
|
137 |
[ |
|
138 |
1 // zero. 'an error which is caught in the handler'. |
|
139 |
(Query query) == true ifTrue:[ |
|
140 |
Transcript showCR:'query says: ok'. |
|
141 |
] ifFalse:[ |
|
142 |
Transcript showCR:'query says: no' |
|
143 |
] |
|
144 |
] value |
|
145 |
] |
|
146 |
] value |
|
147 |
] value |
|
148 |
] |
|
149 |
[exEnd] |
|
150 |
for lazy typists, a more compact interface for query-answerign |
|
151 |
is also provided (which is also easier to read): |
|
152 |
[exBegin] |
|
153 |
Query answer:true do:[ |
|
154 |
'nesting'. |
|
155 |
[ |
|
156 |
[ |
|
157 |
(Query query) == true ifTrue:[ |
|
158 |
Transcript showCR:'query says: ok'. |
|
159 |
] ifFalse:[ |
|
160 |
Transcript showCR:'query says: no' |
|
161 |
] |
|
162 |
] value |
|
163 |
] value |
|
164 |
] |
|
165 |
[exEnd] |
|
166 |
an up-query from a deeply nested operation, for which there |
|
167 |
is no handler: |
|
168 |
(notice, this would not work with normal signals, which would raise |
|
169 |
another unhandled exception-exception; |
|
170 |
also notice the == check #raise's return value being true, |
|
171 |
instead of a simple ifTrue; this handles a nil-value from |
|
172 |
the unhandled query) |
|
173 |
[exBegin] |
|
174 |
|zero| |
|
175 |
||
176 |
zero := 0. |
|
177 |
[ |
|
178 |
'nesting'. |
|
179 |
[ |
|
180 |
[ |
|
181 |
Error handle:[:ex | |
|
182 |
Transcript showCR:'some error: ' , ex errorString. |
|
183 |
ex proceed |
|
184 |
] do:[ |
|
185 |
[ |
|
186 |
1 // zero. 'an error which is caught in the handler'. |
|
187 |
(Query raise) == true ifTrue:[ |
|
188 |
Transcript showCR:'query says: ok'. |
|
189 |
] ifFalse:[ |
|
190 |
Transcript showCR:'query says: no' |
|
191 |
] |
|
192 |
] value |
|
193 |
] |
|
194 |
] value |
|
195 |
] value |
|
196 |
] value |
|
197 |
[exEnd] |
|
198 |
counter-example, just to show that things would not work this way |
|
199 |
with regular signals: |
|
200 |
[exBegin] |
|
201 |
|signal| |
|
202 |
||
203 |
signal := Signal new. |
|
204 |
'nesting deeply'. |
|
205 |
[ |
|
206 |
[ |
|
207 |
[ |
|
208 |
[ |
|
209 |
[ |
|
210 |
(signal raise) == true ifTrue:[ |
|
211 |
Transcript showCR:'query says: ok'. |
|
212 |
] ifFalse:[ |
|
213 |
Transcript showCR:'query says: no' |
|
214 |
] |
|
215 |
] value |
|
216 |
] value |
|
217 |
] value |
|
218 |
] value |
|
219 |
] value |
|
220 |
[exEnd] |
|
221 |
||
222 |
except, by handling the unhandled exception |
|
223 |
(but we think, that querySignals are easier to use and |
|
224 |
better document the intent): |
|
225 |
[exBegin] |
|
226 |
|signal| |
|
227 |
||
228 |
signal := Signal new. |
|
229 |
'nesting deeply'. |
|
230 |
[ |
|
231 |
[ |
|
232 |
[ |
|
233 |
[ |
|
234 |
[ |
|
235 |
Signal noHandlerSignal handle:[:ex | |
|
236 |
ex proceedWith:nil |
|
237 |
] do:[ |
|
238 |
(signal raise) == true ifTrue:[ |
|
239 |
Transcript showCR:'query says: ok'. |
|
240 |
] ifFalse:[ |
|
241 |
Transcript showCR:'query says: no' |
|
242 |
] |
|
243 |
] |
|
244 |
] value |
|
245 |
] value |
|
246 |
] value |
|
247 |
] value |
|
248 |
] value |
|
249 |
[exEnd] |
|
250 |
" |
|
251 |
! ! |
|
252 |
||
253 |
!Query class methodsFor:'initialization'! |
|
254 |
||
255 |
initialize |
|
256 |
||
257 |
NotifierString := 'Query'. |
|
258 |
||
259 |
" |
|
260 |
self initialize |
|
261 |
" |
|
262 |
! ! |
|
263 |
||
12088 | 264 |
!Query class methodsFor:'misc ui support'! |
265 |
||
266 |
iconInBrowserSymbol |
|
267 |
<resource: #programImage> |
|
268 |
||
269 |
^ #queryClassBrowserIcon |
|
270 |
! ! |
|
271 |
||
6433 | 272 |
!Query class methodsFor:'documentation'! |
273 |
||
274 |
version |
|
12088 | 275 |
^ '$Header: /cvs/stx/stx/libbasic/Query.st,v 1.13 2009-10-01 13:44:05 cg Exp $' |
276 |
! |
|
277 |
||
278 |
version_CVS |
|
279 |
^ '$Header: /cvs/stx/stx/libbasic/Query.st,v 1.13 2009-10-01 13:44:05 cg Exp $' |
|
6433 | 280 |
! ! |
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
6433
diff
changeset
|
281 |
|
6432 | 282 |
Query initialize! |