summaryrefslogtreecommitdiff
path: root/ACE/ace/XML_Utils/XSCRT/Traversal.hpp
blob: a40cf5f59071f4fc1055d4804a5510aca923f40a (plain)
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
// file      : XSCRT/Traversal.hpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
#ifndef XSCRT_TRAVERSAL_HPP
#define XSCRT_TRAVERSAL_HPP

#include <map>
#include <set>
#include <vector>

#include "ace/XML_Utils/XSCRT/ExtendedTypeInfo.hpp"

namespace XSCRT
{
  namespace Traversal
  {
    template<typename B>
    class TraverserBase
    {
    protected:
      virtual ~TraverserBase () = default;

      //@@ VC6
    public:
      virtual void trampoline (B& n) = 0;

      virtual void trampoline (B const& n) = 0;
    };

    template <typename B>
    class DispatcherBase
    {
    public:
      virtual ~DispatcherBase () = default;

      virtual void dispatch (B& n);

      virtual void dispatch (B const& n);

      void
      map (TypeId id, TraverserBase<B>& t)
      {
        //@@ 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:
      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 ()
      {
        DispatcherBase<B>::map (typeid (Type), *this);
      }

      virtual void
      traverse (Type&)
      {
        abort ();
      }

      virtual void
      traverse (Type const&)
      {
        abort ();
      }

    protected:
      virtual void
      trampoline (B& n)
      {
        traverse (dynamic_cast<Type&> (n));
      }

      virtual void
      trampoline (B const& n)
      {
        traverse (dynamic_cast<Type const&> (n));
      }
    };
  }
}

#include <ace/XML_Utils/XSCRT/Traversal.tpp>

#endif  // XSCRT_TRAVERSAL_HPP