aboutsummaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/tree/type-serializer-map.hxx
blob: ca52129e17d58858bd29394150541fdbad4e64f3 (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
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
// file      : xsd/cxx/tree/type-serializer-map.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX
#define XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX

#include <map>
#include <string>
#include <cstddef>  // std::size_t
#include <typeinfo>

#include <xercesc/dom/DOMElement.hpp>

#include <xsd/cxx/tree/elements.hxx>

#include <xsd/cxx/xml/qualified-name.hxx>
#include <xsd/cxx/xml/dom/auto-ptr.hxx>
#include <xsd/cxx/xml/dom/serialization-header.hxx> // namespace_infomap

namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      template <typename C>
      struct type_serializer_map
      {
        typedef std::type_info type_id;
        typedef xml::qualified_name<C> qualified_name;
        typedef void (*serializer) (xercesc::DOMElement&, const type&);

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

        void
        unregister_type (const type_id&);

        void
        register_element (const qualified_name& root,
                          const qualified_name& subst,
                          const type_id&,
                          serializer);

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

      public:
        void
        serialize (const C* name, // element name
                   const C* ns,   // element namespace
                   bool global,
                   bool qualified,
                   xercesc::DOMElement& parent,
                   const type&) const;

        // Serialize into existing element.
        //
        void
        serialize (const C* static_name,
                   const C* static_ns,
                   xercesc::DOMElement&,
                   const qualified_name&,
                   const type&) const;

        // Create DOMDocument with root element suitable for serializing
        // x into it.
        //
        XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
        serialize (const C* name, // element name
                   const C* ns,   // element namespace
                   const xml::dom::namespace_infomap<C>&,
                   const type& x,
                   unsigned long flags) const;

      public:
        type_serializer_map ();

      public:
        struct type_info
        {
          type_info (const qualified_name& name,
                     typename type_serializer_map::serializer serializer)
              : name_ (name), serializer_ (serializer)
          {
          }

          const qualified_name&
          name () const
          {
            return name_;
          }

          typename type_serializer_map::serializer
          serializer () const
          {
            return serializer_;
          }

          // For std::map.
          //
          type_info ()
              : name_ (std::basic_string<C> (), std::basic_string<C> ()),
                serializer_ (0)
          {
          }

        private:
          qualified_name name_;
          typename type_serializer_map::serializer serializer_;
        };

      public:
        const type_info*
        find (const type_id&) const;

      private:
        struct type_id_comparator
        {
          bool
          operator() (const type_id* x, const type_id* y) const
          {
            // XL C++ on AIX has buggy type_info::before() in that
            // it returns true for two different type_info objects
            // that happened to be for the same type.
            //
#if defined(__xlC__) && defined(_AIX)
            return *x != *y && x->before (*y);
#else
            return x->before (*y);
#endif
          }
        };

        typedef
        std::map<const type_id*, type_info, type_id_comparator>
        type_map;

        // Map of (root-element to map of (type_id to type_info)).
        // Note that here type_info::name is element name.
        //
        typedef
        std::map<const type_id*, type_info, type_id_comparator>
        subst_map;

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

        type_map type_map_;
        element_map element_map_;

      private:
        const type_info*
        find_substitution (const subst_map& start, const type_id&) const;

        // Sets an xsi:type attribute corresponding to the type_info.
        //
        void
        set_xsi_type (xercesc::DOMElement& parent,
                      xercesc::DOMElement&,
                      const type_info&) const;
      };


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

        type_serializer_plate ();
        ~type_serializer_plate ();
      };

      template<unsigned long id, typename C>
      type_serializer_map<C>* type_serializer_plate<id, C>::map = 0;

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


      //
      //
      template<unsigned long id, typename C>
      inline type_serializer_map<C>&
      type_serializer_map_instance ()
      {
        return *type_serializer_plate<id, C>::map;
      }

      //
      //
      template<typename T>
      void
      serializer_impl (xercesc::DOMElement&, const type&);


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


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

        ~element_serializer_initializer ();

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

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

#endif // XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX