1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// file : XSCRT/Traversal.hpp
// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id : $Id$
#ifndef XSCRT_TRAVERSAL_HPP
#define XSCRT_TRAVERSAL_HPP
#include <map>
#include <set>
#include <vector>
// #include <iostream>
// using std::wcerr;
// using std::endl;
#include "XSCRT/ExtendedTypeInfo.hpp"
namespace XSCRT
{
namespace Traversal
{
//
//
//
template<typename B>
class TraverserBase
{
protected:
virtual
~TraverserBase ();
//@@ VC6
public:
virtual void
trampoline (B& n) = 0;
virtual void
trampoline (B const& n) = 0;
/*@@ VC6
template <typename X>
friend class DispatcherBase;
*/
};
//
//
//
template <typename B>
class DispatcherBase
{
public:
virtual
~DispatcherBase ();
virtual void
dispatch (B& n);
virtual void
dispatch (B const& n);
void
map (TypeId id, TraverserBase<B>& t)
{
//wcerr << "map for " << id.name () << " to " << &t
// << " in " << &traversal_map_ << endl;
//@@ VC6
Traversers& traversers = traversal_map_[id];
traversers.push_back (&t);
}
public:
typedef
std::vector<TraverserBase<B>*>
Traversers;
typedef
std::map<TypeId, Traversers>
TraversalMap;
typedef
typename TraversalMap::const_iterator
Iterator;
Iterator
begin () const
{
return traversal_map_.begin ();
}
Iterator
end () const
{
return traversal_map_.end ();
}
private:
struct TypeInfoComparator
{
bool
operator () (ExtendedTypeInfo const& x,
ExtendedTypeInfo const& y) const
{
return x.type_id () < y.type_id ();
}
};
typedef
std::map<ExtendedTypeInfo, unsigned long, TypeInfoComparator>
LevelMap;
typedef
std::set<ExtendedTypeInfo, TypeInfoComparator>
TypeInfoSet;
static unsigned long
compute_levels (ExtendedTypeInfo const& ti,
unsigned long cur,
LevelMap& map);
static void
flatten_tree (ExtendedTypeInfo const& ti, TypeInfoSet& set);
private:
TraversalMap traversal_map_;
};
//
//
//
template <typename B>
class Dispatcher : public virtual DispatcherBase<B>
{
public:
Dispatcher ()
: merge_ (true)
{
}
void
traverser (DispatcherBase<B>& d)
{
for (typename DispatcherBase<B>::Iterator
i (d.begin ()), end (d.end ());
i != end; ++i)
{
for (typename DispatcherBase<B>::Traversers::const_iterator
t (i->second.begin ()), end (i->second.end ());
t != end; ++t)
{
dispatcher_.map (i->first, **t);
}
}
}
public:
virtual void
dispatch (B& n)
{
merge ();
dispatcher_.dispatch (n);
}
virtual void
dispatch (B const& n)
{
merge ();
dispatcher_.dispatch (n);
}
using DispatcherBase<B>::begin;
using DispatcherBase<B>::end;
private:
void
merge ()
{
if (merge_)
{
for (typename DispatcherBase<B>::Iterator
i (begin ()), e (end ()); i != e; ++i)
{
for (typename DispatcherBase<B>::Traversers::const_iterator
t (i->second.begin ()), e (i->second.end ()); t != e; ++t)
{
dispatcher_.map (i->first, **t);
}
}
merge_ = false;
}
}
protected:
// DispatcherBase<B>&
// traverser ()
// {
// return dispatcher_;
// }
template <typename X, typename A, typename I>
void
iterate_and_dispatch (I begin, I end, X& x, void (X::*next)(A&), A& a)
{
for (; begin != end;)
{
dispatch (*begin);
if (++begin != end) (x.*next) (a);
}
}
private:
bool merge_;
DispatcherBase<B> dispatcher_;
};
//
//
//
template <typename T, typename B>
struct Traverser : TraverserBase<B>, virtual Dispatcher<B>
{
typedef
T
Type;
Traverser ()
{
map (typeid (Type), *this);
}
virtual void
traverse (Type&)
{
abort ();
}
virtual void
traverse (Type const&)
{
abort ();
}
protected:
virtual void
trampoline (B& n)
{
//wcerr << "trampoline for " << &n << " to type "
// << typeid (Type).name () << endl;
traverse (dynamic_cast<Type&> (n));
}
virtual void
trampoline (B const& n)
{
//wcerr << "trampoline for " << &n << " to type "
// << typeid (Type).name () << endl;
traverse (dynamic_cast<Type const&> (n));
}
};
}
}
#include <XSCRT/Traversal.ipp>
#include <XSCRT/Traversal.tpp>
#endif // XSCRT_TRAVERSAL_HPP
|