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