summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/tree/type-factory-map.hxx
blob: 767969d70f13f12fd902a4a596263989fb018345 (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
// file      : xsd/cxx/tree/type-factory-map.hxx
// copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_TREE_TYPE_FACTORY_MAP_HXX
#define XSD_CXX_TREE_TYPE_FACTORY_MAP_HXX

#include <map>
#include <string>
#include <memory>  // std::auto_ptr/unique_ptr
#include <cstddef> // std::size_t

#include <xercesc/dom/DOMElement.hpp>

#include <xsd/cxx/config.hxx> // XSD_AUTO_PTR

#include <xsd/cxx/tree/elements.hxx>
#include <xsd/cxx/xml/qualified-name.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      template <typename C>
      struct type_factory_map
      {
        typedef xml::qualified_name<C> qualified_name;
        typedef XSD_AUTO_PTR<type> (*factory) (const xercesc::DOMElement&,
                                               flags,
                                               container*);
      public:
        type_factory_map ();

        void
        register_type (const qualified_name& name,
                       factory,
                       bool replace = true);

        void
        unregister_type (const qualified_name& name);

        void
        register_element (const qualified_name& root,
                          const qualified_name& subst,
                          factory);

        void
        unregister_element (const qualified_name& root,
                            const qualified_name& subst);

        XSD_AUTO_PTR<type>
        create (const C* name, // element name
                const C* ns,   // element namespace
                factory static_type,
                bool global,
                bool qualified,
                const xercesc::DOMElement&,
                const qualified_name&,
                flags,
                container*) const;

      public:
        factory
        find (const qualified_name& name) const;

      private:
        template <typename T>
        static XSD_AUTO_PTR<type>
        traits_adapter (const xercesc::DOMElement&, flags, container*);

      private:
        typedef
        std::map<qualified_name, factory>
        type_map;

        // Map of (root-element to map of (subst-element to factory)).
        //
        typedef
        std::map<qualified_name, factory>
        subst_map;

        typedef
        std::map<qualified_name, subst_map>
        element_map;

        type_map type_map_;
        element_map element_map_;

      private:
        factory
        find_substitution (const subst_map& start,
                           const qualified_name& name) const;

        // The name argument is as specified in xsi:type.
        //
        factory
        find_type (const std::basic_string<C>& name,
                   const xercesc::DOMElement&) const;
      };


      //
      //
      template<unsigned long id, typename C>
      struct type_factory_plate
      {
        static type_factory_map<C>* map;
        static std::size_t count;

        type_factory_plate ();
        ~type_factory_plate ();
      };

      template<unsigned long id, typename C>
      type_factory_map<C>* type_factory_plate<id, C>::map = 0;

      template<unsigned long id, typename C>
      std::size_t type_factory_plate<id, C>::count = 0;


      //
      //
      template<unsigned long id, typename C>
      inline type_factory_map<C>&
      type_factory_map_instance ()
      {
        return *type_factory_plate<id, C>::map;
      }


      //
      //
      template<typename T>
      XSD_AUTO_PTR<type>
      factory_impl (const xercesc::DOMElement&, flags, container*);

      //
      //
      template<unsigned long id, typename C, typename T>
      struct type_factory_initializer
      {
        type_factory_initializer (const C* name, const C* ns);
        ~type_factory_initializer ();

      private:
        const C* name_;
        const C* ns_;
      };

      //
      //
      template<unsigned long id, typename C, typename T>
      struct element_factory_initializer
      {
        element_factory_initializer (const C* root_name, const C* root_ns,
                                     const C* subst_name, const C* subst_ns);

        ~element_factory_initializer ();

      private:
        const C* root_name_;
        const C* root_ns_;
        const C* subst_name_;
        const C* subst_ns_;
      };
    }
  }
}

#include <xsd/cxx/tree/type-factory-map.txx>

#endif // XSD_CXX_TREE_TYPE_FACTORY_MAP_HXX