1 // file : XSCRT/Traversal.hpp
2 // author : Boris Kolpackov <boris@dre.vanderbilt.edu>
3 #ifndef XSCRT_TRAVERSAL_HPP
4 #define XSCRT_TRAVERSAL_HPP
10 #include "ace/XML_Utils/XSCRT/ExtendedTypeInfo.hpp"
20 virtual ~TraverserBase () = default;
24 virtual void trampoline (B
& n
) = 0;
26 virtual void trampoline (B
const& n
) = 0;
33 virtual ~DispatcherBase () = default;
35 virtual void dispatch (B
& n
);
37 virtual void dispatch (B
const& n
);
40 map (TypeId id
, TraverserBase
<B
>& t
)
43 Traversers
& traversers
= traversal_map_
[id
];
44 traversers
.push_back (&t
);
48 typedef std::vector
<TraverserBase
<B
>*> Traversers
;
49 typedef std::map
<TypeId
, Traversers
> TraversalMap
;
50 typedef typename
TraversalMap::const_iterator Iterator
;
55 return traversal_map_
.begin ();
61 return traversal_map_
.end ();
65 struct TypeInfoComparator
68 operator () (ExtendedTypeInfo
const& x
,
69 ExtendedTypeInfo
const& y
) const
71 return x
.type_id () < y
.type_id ();
75 typedef std::map
<ExtendedTypeInfo
, unsigned long, TypeInfoComparator
> LevelMap
;
76 typedef std::set
<ExtendedTypeInfo
, TypeInfoComparator
> TypeInfoSet
;
79 compute_levels (ExtendedTypeInfo
const& ti
,
84 flatten_tree (ExtendedTypeInfo
const& ti
, TypeInfoSet
& set
);
87 TraversalMap traversal_map_
;
91 class Dispatcher
: public virtual DispatcherBase
<B
>
100 traverser (DispatcherBase
<B
>& d
)
102 for (typename DispatcherBase
<B
>::Iterator
103 i (d
.begin ()), end (d
.end ());
106 for (typename DispatcherBase
<B
>::Traversers::const_iterator
107 t (i
->second
.begin ()), end (i
->second
.end ());
110 dispatcher_
.map (i
->first
, **t
);
120 dispatcher_
.dispatch (n
);
124 dispatch (B
const& n
)
127 dispatcher_
.dispatch (n
);
130 using DispatcherBase
<B
>::begin
;
131 using DispatcherBase
<B
>::end
;
139 for (typename DispatcherBase
<B
>::Iterator
140 i (begin ()), e (end ()); i
!= e
; ++i
)
142 for (typename DispatcherBase
<B
>::Traversers::const_iterator
143 t (i
->second
.begin ()), e (i
->second
.end ()); t
!= e
; ++t
)
145 dispatcher_
.map (i
->first
, **t
);
154 template <typename X
, typename A
, typename I
>
156 iterate_and_dispatch (I begin
, I end
, X
& x
, void (X::*next
)(A
&), A
& a
)
158 for (; begin
!= end
;)
162 if (++begin
!= end
) (x
.*next
) (a
);
168 DispatcherBase
<B
> dispatcher_
;
171 template <typename T
, typename B
>
172 struct Traverser
: TraverserBase
<B
>, virtual Dispatcher
<B
>
178 DispatcherBase
<B
>::map (typeid (Type
), *this);
188 traverse (Type
const&)
197 traverse (dynamic_cast<Type
&> (n
));
201 trampoline (B
const& n
)
203 traverse (dynamic_cast<Type
const&> (n
));
209 #include <ace/XML_Utils/XSCRT/Traversal.tpp>
211 #endif // XSCRT_TRAVERSAL_HPP