author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jan 2020 17:27:00 +0100 | |
changeset 25177 | 1c618f3d51f5 |
parent 24791 | b74d57425436 |
permissions | -rw-r--r-- |
22808 | 1 |
"{ Encoding: utf8 }" |
2 |
||
8391 | 3 |
" |
4 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
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 |
" |
|
14 |
"{ Package: 'stx:libbasic' }" |
|
15 |
||
21397 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8391 | 18 |
Object subclass:#Visitor |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
8392 | 22 |
category:'System-Visiting' |
8391 | 23 |
! |
24 |
||
25 |
!Visitor class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
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 |
" |
|
43 |
This class is abstract and implements the visitor pattern. |
|
44 |
It defines #visitXXX:with: messages for various kinds of objects. |
|
21790 | 45 |
Any of these messages is eventually mapped to #visitObject:with:. |
8391 | 46 |
|
47 |
When a class redefines #acceptVisitor:with:, an method has to be defined here, too. |
|
48 |
||
49 |
Subclasses have to define at least #visitObject:with: |
|
50 |
||
51 |
[author:] |
|
52 |
Stefan Vogel (stefan@zwerg) |
|
53 |
||
54 |
[see also:] |
|
55 |
Object>>acceptVisitor:with: |
|
56 |
" |
|
57 |
! ! |
|
58 |
||
21397 | 59 |
!Visitor class methodsFor:'queries'! |
60 |
||
61 |
isAbstract |
|
62 |
"Return if this class is an abstract class. |
|
63 |
True is returned here for myself only; false for subclasses. |
|
64 |
Abstract subclasses must redefine this again." |
|
65 |
||
66 |
^ self == Visitor. |
|
67 |
! ! |
|
68 |
||
8391 | 69 |
!Visitor methodsFor:'helpers'! |
70 |
||
71 |
doesNotUnderstand:aMessage |
|
72 |
"catch unimplemented #visitXXX:with: messages and redirect them to #visitObject:with:. |
|
73 |
maybe someone implemented an #acceptVisitor:with: in the XXX class and didn't |
|
74 |
implement the corresponding method here. |
|
75 |
||
76 |
This should not happen, and a error message is printed, because this can also |
|
77 |
occur due to a typo." |
|
78 |
||
79 |
|selector args| |
|
80 |
||
81 |
selector := aMessage selector. |
|
82 |
((selector startsWith:'visit') and:[selector endsWith:'with:']) ifTrue:[ |
|
83 |
('Visitor: unimplemented #', selector, ' in class ', self className, '. Please define as #visitObject:with:') errorPrintCR. |
|
84 |
args := aMessage arguments. |
|
85 |
^ self visitObject:(args at:1) with:(args at:2). |
|
86 |
]. |
|
87 |
||
88 |
^ super doesNotUnderstand:aMessage |
|
89 |
||
90 |
" |
|
91 |
Visitor new visitFoo:#foo with:'arg' |
|
92 |
" |
|
93 |
! |
|
94 |
||
95 |
visitChildrenOf:anObject |
|
96 |
"recursively enumerate anObjects refs, visiting each of the child objects. |
|
22338 | 97 |
nil children are not visited" |
8391 | 98 |
|
22338 | 99 |
|instVarNames theClass |
8391 | 100 |
sz "{ Class: SmallInteger }"| |
101 |
||
102 |
theClass := anObject class. |
|
103 |
sz := theClass instSize. |
|
104 |
sz ~~ 0 ifTrue:[ |
|
105 |
instVarNames := theClass allInstVarNames. |
|
106 |
1 to:sz do:[:i | |ref| |
|
107 |
ref := anObject instVarAt:i. |
|
108 |
ref notNil ifTrue:[ |
|
109 |
ref acceptVisitor:self with:(instVarNames at:i). |
|
110 |
self nextObject. |
|
111 |
]. |
|
112 |
]. |
|
113 |
]. |
|
114 |
||
115 |
theClass isVariable ifTrue:[ |
|
116 |
sz := anObject basicSize. |
|
117 |
sz ~~ 0 ifTrue:[ |
|
118 |
1 to:sz do:[:i | |
|
22338 | 119 |
(anObject basicAt:i) acceptVisitor:self with:i. |
8391 | 120 |
self nextObject. |
121 |
] |
|
122 |
]. |
|
123 |
]. |
|
22338 | 124 |
|
125 |
"Modified (comment): / 09-11-2017 / 10:46:55 / mawalch" |
|
8391 | 126 |
! ! |
127 |
||
128 |
!Visitor methodsFor:'subclass responsibility'! |
|
129 |
||
130 |
nextObject |
|
131 |
"this is sent, when a new Object is going to be visited. |
|
132 |
Subclasses may redefine this to perform specific actions" |
|
133 |
||
134 |
^ self |
|
135 |
! |
|
136 |
||
137 |
visitObject:anObject with:aParameter |
|
138 |
"visit an object. Subclasses must at least implement this method" |
|
139 |
||
140 |
self subclassResponsibility |
|
23192 | 141 |
! |
142 |
||
143 |
visitProtoObject:anObject with:aParameter |
|
144 |
"visit a proto object." |
|
145 |
||
23193 | 146 |
"/ ^ self visitObject:anObject with:aParameter |
23192 | 147 |
self subclassResponsibility |
148 |
||
149 |
"Created: / 04-07-2018 / 00:43:49 / Claus Gittinger" |
|
8391 | 150 |
! ! |
151 |
||
152 |
!Visitor methodsFor:'visiting smalltalk types'! |
|
153 |
||
154 |
visitBehavior:aSymbol with:aParameter |
|
22808 | 155 |
"visit a class - fallback here is to call visitObject" |
8391 | 156 |
|
157 |
self visitObject:aSymbol with:aParameter |
|
158 |
! |
|
159 |
||
160 |
visitBitArray:aByteArray with:aParameter |
|
161 |
"visit an absolute time value" |
|
162 |
||
163 |
self visitSequenceableCollection:aByteArray with:aParameter |
|
164 |
! |
|
165 |
||
166 |
visitBlock:aBlock with:aParameter |
|
167 |
||
168 |
self visitObject:aBlock with:aParameter |
|
169 |
! |
|
170 |
||
171 |
visitBoolean:aBoolean with:aParameter |
|
22808 | 172 |
"visit a Boolean - fallback here is to call visitObject" |
8391 | 173 |
|
174 |
self visitObject:aBoolean with:aParameter |
|
175 |
! |
|
176 |
||
177 |
visitByteArray:aByteArray with:aParameter |
|
11037
ab98e48e189e
changed #visitByteArray:with: - comment
Stefan Vogel <sv@exept.de>
parents:
11036
diff
changeset
|
178 |
"visit a ByteArray value" |
8391 | 179 |
|
180 |
self visitSequenceableCollection:aByteArray with:aParameter |
|
181 |
! |
|
182 |
||
24791 | 183 |
visitCDatum:aCDatum with:aParameter |
184 |
"visit a CDatum - fallback here is to call visitObject" |
|
185 |
||
186 |
self visitObject:aCDatum with:aParameter |
|
187 |
! |
|
188 |
||
8391 | 189 |
visitCharacter:aCharacter with:aParameter |
22808 | 190 |
"visit a Character - fallback here is to call visitObject" |
8391 | 191 |
|
192 |
self visitObject:aCharacter with:aParameter |
|
193 |
! |
|
194 |
||
195 |
visitCollection:aCollection with:aParameter |
|
22808 | 196 |
"visit a Collection - fallback here is to call visitObject" |
8391 | 197 |
|
198 |
self visitObject:aCollection with:aParameter |
|
199 |
! |
|
200 |
||
11175 | 201 |
visitDate:aDate with:aParameter |
22808 | 202 |
"visit a date value - fallback here is to call visitObject" |
11175 | 203 |
|
204 |
self visitObject:aDate with:aParameter |
|
205 |
! |
|
206 |
||
8391 | 207 |
visitDictionary:aDictionary with:aParameter |
208 |
"visit a Dictionary" |
|
209 |
||
210 |
self visitCollection:aDictionary with:aParameter |
|
211 |
! |
|
212 |
||
213 |
visitFloat:aFloat with:aParameter |
|
214 |
"visit a Float" |
|
215 |
||
216 |
self visitNumber:aFloat with:aParameter |
|
217 |
! |
|
218 |
||
219 |
visitFraction:aFraction with:aParameter |
|
220 |
"visit a Fraction" |
|
221 |
||
222 |
self visitNumber:aFraction with:aParameter |
|
223 |
! |
|
224 |
||
225 |
visitInteger:anInteger with:aParameter |
|
226 |
"visit an Integer" |
|
227 |
||
228 |
self visitNumber:anInteger with:aParameter |
|
229 |
! |
|
230 |
||
231 |
visitInterval:anInterval with:aParameter |
|
232 |
"intervals are a special flavor of sequenceable collection (saving memory) |
|
233 |
We treat it as SequenceableCollection by default. |
|
234 |
Encoders that do a dense representation, redefine this to #visitObject" |
|
235 |
||
236 |
^ self visitSequenceableCollection:anInterval with:aParameter |
|
237 |
! |
|
238 |
||
239 |
visitNilWith:aParameter |
|
22808 | 240 |
"visit nil - fallback here is to call visitObject" |
8391 | 241 |
|
242 |
self visitObject:nil with:aParameter |
|
243 |
! |
|
244 |
||
245 |
visitNumber:aNumber with:aParameter |
|
22808 | 246 |
"visit any number - fallback here is to call visitObject" |
8391 | 247 |
|
248 |
self visitObject:aNumber with:aParameter |
|
249 |
! |
|
250 |
||
251 |
visitSequenceableCollection:aCollection with:aParameter |
|
252 |
"visit a SequenceableCollection" |
|
253 |
||
254 |
self visitCollection:aCollection with:aParameter |
|
255 |
! |
|
256 |
||
257 |
visitSet:aCollection with:aParameter |
|
258 |
"visit a Set" |
|
259 |
||
260 |
self visitCollection:aCollection with:aParameter |
|
261 |
! |
|
262 |
||
11749 | 263 |
visitStream:aStream with:aParameter |
22808 | 264 |
"visit a Stream - fallback here is to call visitObject" |
11749 | 265 |
|
266 |
self visitObject:aStream with:aParameter |
|
267 |
! |
|
268 |
||
8391 | 269 |
visitString:aString with:aParameter |
22808 | 270 |
"visit a String - fallback here is to call visitObject" |
8391 | 271 |
|
272 |
self visitObject:aString with:aParameter |
|
273 |
! |
|
274 |
||
275 |
visitSymbol:aSymbol with:aParameter |
|
276 |
"visit a Symbol" |
|
277 |
||
11036 | 278 |
^ self visitString:aSymbol with:aParameter |
8391 | 279 |
! |
280 |
||
22808 | 281 |
visitTime:aTime with:aParameter |
282 |
"visit a time value - fallback here is to call visitObject" |
|
283 |
||
284 |
self visitObject:aTime with:aParameter |
|
285 |
! |
|
286 |
||
8391 | 287 |
visitTimestamp:aTimestamp with:aParameter |
22808 | 288 |
"visit an timestamp value - fallback here is to call visitObject" |
8391 | 289 |
|
290 |
self visitObject:aTimestamp with:aParameter |
|
13230
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
291 |
! |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
292 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
293 |
visitUtcTimestamp:aTimestamp with:aParameter |
22808 | 294 |
"visit an UTC timestamp value - fallback here is to call visitObject" |
13230
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
295 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
296 |
self visitTimestamp:aTimestamp with:aParameter |
8391 | 297 |
! ! |
298 |
||
299 |
!Visitor class methodsFor:'documentation'! |
|
300 |
||
301 |
version |
|
21397 | 302 |
^ '$Header$' |
13230
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
303 |
! |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
304 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
305 |
version_CVS |
21397 | 306 |
^ '$Header$' |
8391 | 307 |
! ! |
21397 | 308 |