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