author | Stefan Vogel <sv@exept.de> |
Thu, 16 Feb 2017 20:48:27 +0100 | |
changeset 21475 | 6409725581b1 |
parent 21397 | 2b6bb4b440bb |
child 21790 | e2c2bf98f841 |
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. |
|
43 |
Any of these messsages is eventually mapped to #visitObject:with:. |
|
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. |
|
95 |
nil childs are not visited" |
|
96 |
||
97 |
|instVarNames theClass |
|
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 | |
|
117 |
(anObject basicAt:i) acceptVisitor:self with:i. |
|
118 |
self nextObject. |
|
119 |
] |
|
120 |
]. |
|
121 |
]. |
|
122 |
! ! |
|
123 |
||
124 |
!Visitor methodsFor:'subclass responsibility'! |
|
125 |
||
126 |
nextObject |
|
127 |
"this is sent, when a new Object is going to be visited. |
|
128 |
Subclasses may redefine this to perform specific actions" |
|
129 |
||
130 |
^ self |
|
131 |
! |
|
132 |
||
133 |
visitObject:anObject with:aParameter |
|
134 |
"visit an object. Subclasses must at least implement this method" |
|
135 |
||
136 |
self subclassResponsibility |
|
137 |
! ! |
|
138 |
||
139 |
!Visitor methodsFor:'visiting smalltalk types'! |
|
140 |
||
141 |
visitBehavior:aSymbol with:aParameter |
|
142 |
"visit a class" |
|
143 |
||
144 |
self visitObject:aSymbol with:aParameter |
|
145 |
! |
|
146 |
||
147 |
visitBitArray:aByteArray with:aParameter |
|
148 |
"visit an absolute time value" |
|
149 |
||
150 |
self visitSequenceableCollection:aByteArray with:aParameter |
|
151 |
! |
|
152 |
||
153 |
visitBlock:aBlock with:aParameter |
|
154 |
||
155 |
self visitObject:aBlock with:aParameter |
|
156 |
! |
|
157 |
||
158 |
visitBoolean:aBoolean with:aParameter |
|
159 |
"visit a Boolean" |
|
160 |
||
161 |
self visitObject:aBoolean with:aParameter |
|
162 |
! |
|
163 |
||
164 |
visitByteArray:aByteArray with:aParameter |
|
11037
ab98e48e189e
changed #visitByteArray:with: - comment
Stefan Vogel <sv@exept.de>
parents:
11036
diff
changeset
|
165 |
"visit a ByteArray value" |
8391 | 166 |
|
167 |
self visitSequenceableCollection:aByteArray with:aParameter |
|
168 |
! |
|
169 |
||
170 |
visitCharacter:aCharacter with:aParameter |
|
171 |
"visit a Character" |
|
172 |
||
173 |
self visitObject:aCharacter with:aParameter |
|
174 |
! |
|
175 |
||
176 |
visitCollection:aCollection with:aParameter |
|
177 |
"visit a Collection" |
|
178 |
||
179 |
self visitObject:aCollection with:aParameter |
|
180 |
! |
|
181 |
||
11175 | 182 |
visitDate:aDate with:aParameter |
183 |
"visit a date value" |
|
184 |
||
185 |
self visitObject:aDate with:aParameter |
|
186 |
! |
|
187 |
||
8391 | 188 |
visitDictionary:aDictionary with:aParameter |
189 |
"visit a Dictionary" |
|
190 |
||
191 |
self visitCollection:aDictionary with:aParameter |
|
192 |
! |
|
193 |
||
194 |
visitFloat:aFloat with:aParameter |
|
195 |
"visit a Float" |
|
196 |
||
197 |
self visitNumber:aFloat with:aParameter |
|
198 |
! |
|
199 |
||
200 |
visitFraction:aFraction with:aParameter |
|
201 |
"visit a Fraction" |
|
202 |
||
203 |
self visitNumber:aFraction with:aParameter |
|
204 |
! |
|
205 |
||
206 |
visitInteger:anInteger with:aParameter |
|
207 |
"visit an Integer" |
|
208 |
||
209 |
self visitNumber:anInteger with:aParameter |
|
210 |
! |
|
211 |
||
212 |
visitInterval:anInterval with:aParameter |
|
213 |
"intervals are a special flavor of sequenceable collection (saving memory) |
|
214 |
We treat it as SequenceableCollection by default. |
|
215 |
Encoders that do a dense representation, redefine this to #visitObject" |
|
216 |
||
217 |
^ self visitSequenceableCollection:anInterval with:aParameter |
|
218 |
! |
|
219 |
||
220 |
visitNilWith:aParameter |
|
221 |
"visit a nil" |
|
222 |
||
223 |
self visitObject:nil with:aParameter |
|
224 |
! |
|
225 |
||
226 |
visitNumber:aNumber with:aParameter |
|
227 |
"visit an Integer" |
|
228 |
||
229 |
self visitObject:aNumber with:aParameter |
|
230 |
! |
|
231 |
||
232 |
visitSequenceableCollection:aCollection with:aParameter |
|
233 |
"visit a SequenceableCollection" |
|
234 |
||
235 |
self visitCollection:aCollection with:aParameter |
|
236 |
! |
|
237 |
||
238 |
visitSet:aCollection with:aParameter |
|
239 |
"visit a Set" |
|
240 |
||
241 |
self visitCollection:aCollection with:aParameter |
|
242 |
! |
|
243 |
||
11749 | 244 |
visitStream:aStream with:aParameter |
245 |
"visit a Stream" |
|
246 |
||
247 |
self visitObject:aStream with:aParameter |
|
248 |
! |
|
249 |
||
8391 | 250 |
visitString:aString with:aParameter |
251 |
"visit a String" |
|
252 |
||
253 |
self visitObject:aString with:aParameter |
|
254 |
! |
|
255 |
||
256 |
visitSymbol:aSymbol with:aParameter |
|
257 |
"visit a Symbol" |
|
258 |
||
11036 | 259 |
^ self visitString:aSymbol with:aParameter |
8391 | 260 |
! |
261 |
||
262 |
visitTimestamp:aTimestamp with:aParameter |
|
11175 | 263 |
"visit an timestamp value" |
8391 | 264 |
|
265 |
self visitObject:aTimestamp with:aParameter |
|
13230
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
266 |
! |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
267 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
268 |
visitUtcTimestamp:aTimestamp with:aParameter |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
269 |
"visit an UTC timestamp value" |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
270 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
271 |
self visitTimestamp:aTimestamp with:aParameter |
8391 | 272 |
! ! |
273 |
||
274 |
!Visitor class methodsFor:'documentation'! |
|
275 |
||
276 |
version |
|
21397 | 277 |
^ '$Header$' |
13230
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
278 |
! |
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
279 |
|
cc6bc2d6c296
added: #visitUtcTimestamp:with:
Stefan Vogel <sv@exept.de>
parents:
11749
diff
changeset
|
280 |
version_CVS |
21397 | 281 |
^ '$Header$' |
8391 | 282 |
! ! |
21397 | 283 |