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 |
|
|
13 |
"{ Package: 'stx:libbasic' }"
|
|
14 |
|
|
15 |
Object subclass:#Visitor
|
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
8392
|
19 |
category:'System-Visiting'
|
8391
|
20 |
!
|
|
21 |
|
|
22 |
!Visitor class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
This class is abstract and implements the visitor pattern.
|
|
41 |
It defines #visitXXX:with: messages for various kinds of objects.
|
|
42 |
Any of these messsages is eventually mapped to #visitObject:with:.
|
|
43 |
|
|
44 |
When a class redefines #acceptVisitor:with:, an method has to be defined here, too.
|
|
45 |
|
|
46 |
Subclasses have to define at least #visitObject:with:
|
|
47 |
|
|
48 |
[author:]
|
|
49 |
Stefan Vogel (stefan@zwerg)
|
|
50 |
|
|
51 |
[see also:]
|
|
52 |
Object>>acceptVisitor:with:
|
|
53 |
"
|
|
54 |
! !
|
|
55 |
|
|
56 |
!Visitor methodsFor:'helpers'!
|
|
57 |
|
|
58 |
doesNotUnderstand:aMessage
|
|
59 |
"catch unimplemented #visitXXX:with: messages and redirect them to #visitObject:with:.
|
|
60 |
maybe someone implemented an #acceptVisitor:with: in the XXX class and didn't
|
|
61 |
implement the corresponding method here.
|
|
62 |
|
|
63 |
This should not happen, and a error message is printed, because this can also
|
|
64 |
occur due to a typo."
|
|
65 |
|
|
66 |
|selector args|
|
|
67 |
|
|
68 |
selector := aMessage selector.
|
|
69 |
((selector startsWith:'visit') and:[selector endsWith:'with:']) ifTrue:[
|
|
70 |
('Visitor: unimplemented #', selector, ' in class ', self className, '. Please define as #visitObject:with:') errorPrintCR.
|
|
71 |
args := aMessage arguments.
|
|
72 |
^ self visitObject:(args at:1) with:(args at:2).
|
|
73 |
].
|
|
74 |
|
|
75 |
^ super doesNotUnderstand:aMessage
|
|
76 |
|
|
77 |
"
|
|
78 |
Visitor new visitFoo:#foo with:'arg'
|
|
79 |
"
|
|
80 |
!
|
|
81 |
|
|
82 |
visitChildrenOf:anObject
|
|
83 |
"recursively enumerate anObjects refs, visiting each of the child objects.
|
|
84 |
nil childs are not visited"
|
|
85 |
|
|
86 |
|instVarNames theClass
|
|
87 |
sz "{ Class: SmallInteger }"|
|
|
88 |
|
|
89 |
theClass := anObject class.
|
|
90 |
sz := theClass instSize.
|
|
91 |
sz ~~ 0 ifTrue:[
|
|
92 |
instVarNames := theClass allInstVarNames.
|
|
93 |
1 to:sz do:[:i | |ref|
|
|
94 |
ref := anObject instVarAt:i.
|
|
95 |
ref notNil ifTrue:[
|
|
96 |
ref acceptVisitor:self with:(instVarNames at:i).
|
|
97 |
self nextObject.
|
|
98 |
].
|
|
99 |
].
|
|
100 |
].
|
|
101 |
|
|
102 |
theClass isVariable ifTrue:[
|
|
103 |
sz := anObject basicSize.
|
|
104 |
sz ~~ 0 ifTrue:[
|
|
105 |
1 to:sz do:[:i |
|
|
106 |
(anObject basicAt:i) acceptVisitor:self with:i.
|
|
107 |
self nextObject.
|
|
108 |
]
|
|
109 |
].
|
|
110 |
].
|
|
111 |
! !
|
|
112 |
|
|
113 |
!Visitor methodsFor:'subclass responsibility'!
|
|
114 |
|
|
115 |
nextObject
|
|
116 |
"this is sent, when a new Object is going to be visited.
|
|
117 |
Subclasses may redefine this to perform specific actions"
|
|
118 |
|
|
119 |
^ self
|
|
120 |
!
|
|
121 |
|
|
122 |
visitObject:anObject with:aParameter
|
|
123 |
"visit an object. Subclasses must at least implement this method"
|
|
124 |
|
|
125 |
self subclassResponsibility
|
|
126 |
! !
|
|
127 |
|
|
128 |
!Visitor methodsFor:'visiting smalltalk types'!
|
|
129 |
|
|
130 |
visitBehavior:aSymbol with:aParameter
|
|
131 |
"visit a class"
|
|
132 |
|
|
133 |
self visitObject:aSymbol with:aParameter
|
|
134 |
!
|
|
135 |
|
|
136 |
visitBitArray:aByteArray with:aParameter
|
|
137 |
"visit an absolute time value"
|
|
138 |
|
|
139 |
self visitSequenceableCollection:aByteArray with:aParameter
|
|
140 |
!
|
|
141 |
|
|
142 |
visitBlock:aBlock with:aParameter
|
|
143 |
|
|
144 |
self visitObject:aBlock with:aParameter
|
|
145 |
!
|
|
146 |
|
|
147 |
visitBoolean:aBoolean with:aParameter
|
|
148 |
"visit a Boolean"
|
|
149 |
|
|
150 |
self visitObject:aBoolean with:aParameter
|
|
151 |
!
|
|
152 |
|
|
153 |
visitByteArray:aByteArray with:aParameter
|
|
154 |
"visit an absolute time value"
|
|
155 |
|
|
156 |
self visitSequenceableCollection:aByteArray with:aParameter
|
|
157 |
!
|
|
158 |
|
|
159 |
visitCharacter:aCharacter with:aParameter
|
|
160 |
"visit a Character"
|
|
161 |
|
|
162 |
self visitObject:aCharacter with:aParameter
|
|
163 |
!
|
|
164 |
|
|
165 |
visitCollection:aCollection with:aParameter
|
|
166 |
"visit a Collection"
|
|
167 |
|
|
168 |
self visitObject:aCollection with:aParameter
|
|
169 |
!
|
|
170 |
|
|
171 |
visitDictionary:aDictionary with:aParameter
|
|
172 |
"visit a Dictionary"
|
|
173 |
|
|
174 |
self visitCollection:aDictionary with:aParameter
|
|
175 |
!
|
|
176 |
|
|
177 |
visitFloat:aFloat with:aParameter
|
|
178 |
"visit a Float"
|
|
179 |
|
|
180 |
self visitNumber:aFloat with:aParameter
|
|
181 |
!
|
|
182 |
|
|
183 |
visitFraction:aFraction with:aParameter
|
|
184 |
"visit a Fraction"
|
|
185 |
|
|
186 |
self visitNumber:aFraction with:aParameter
|
|
187 |
!
|
|
188 |
|
|
189 |
visitInteger:anInteger with:aParameter
|
|
190 |
"visit an Integer"
|
|
191 |
|
|
192 |
self visitNumber:anInteger with:aParameter
|
|
193 |
!
|
|
194 |
|
|
195 |
visitInterval:anInterval with:aParameter
|
|
196 |
"intervals are a special flavor of sequenceable collection (saving memory)
|
|
197 |
We treat it as SequenceableCollection by default.
|
|
198 |
Encoders that do a dense representation, redefine this to #visitObject"
|
|
199 |
|
|
200 |
^ self visitSequenceableCollection:anInterval with:aParameter
|
|
201 |
!
|
|
202 |
|
|
203 |
visitNilWith:aParameter
|
|
204 |
"visit a nil"
|
|
205 |
|
|
206 |
self visitObject:nil with:aParameter
|
|
207 |
!
|
|
208 |
|
|
209 |
visitNumber:aNumber with:aParameter
|
|
210 |
"visit an Integer"
|
|
211 |
|
|
212 |
self visitObject:aNumber with:aParameter
|
|
213 |
!
|
|
214 |
|
|
215 |
visitSequenceableCollection:aCollection with:aParameter
|
|
216 |
"visit a SequenceableCollection"
|
|
217 |
|
|
218 |
self visitCollection:aCollection with:aParameter
|
|
219 |
!
|
|
220 |
|
|
221 |
visitSet:aCollection with:aParameter
|
|
222 |
"visit a Set"
|
|
223 |
|
|
224 |
self visitCollection:aCollection with:aParameter
|
|
225 |
!
|
|
226 |
|
|
227 |
visitString:aString with:aParameter
|
|
228 |
"visit a String"
|
|
229 |
|
|
230 |
self visitObject:aString with:aParameter
|
|
231 |
!
|
|
232 |
|
|
233 |
visitSymbol:aSymbol with:aParameter
|
|
234 |
"visit a Symbol"
|
|
235 |
|
|
236 |
self visitObject:aSymbol with:aParameter
|
|
237 |
!
|
|
238 |
|
|
239 |
visitTimestamp:aTimestamp with:aParameter
|
|
240 |
"visit an absolute time value"
|
|
241 |
|
|
242 |
self visitObject:aTimestamp with:aParameter
|
|
243 |
! !
|
|
244 |
|
|
245 |
!Visitor class methodsFor:'documentation'!
|
|
246 |
|
|
247 |
version
|
8392
|
248 |
^ '$Header: /cvs/stx/stx/libbasic/Visitor.st,v 1.2 2004-06-11 17:54:29 stefan Exp $'
|
8391
|
249 |
! !
|