1 | // file : xsd/cxx/tree/type-serializer-map.hxx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #ifndef XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX |
---|
7 | #define XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX |
---|
8 | |
---|
9 | #include <map> |
---|
10 | #include <string> |
---|
11 | #include <typeinfo> |
---|
12 | |
---|
13 | #include <xercesc/dom/DOMElement.hpp> |
---|
14 | |
---|
15 | #include <xsd/cxx/tree/elements.hxx> |
---|
16 | |
---|
17 | #include <xsd/cxx/xml/qualified-name.hxx> |
---|
18 | #include <xsd/cxx/xml/dom/serialization-header.hxx> // namespace_infomap |
---|
19 | |
---|
20 | namespace xsd |
---|
21 | { |
---|
22 | namespace cxx |
---|
23 | { |
---|
24 | namespace tree |
---|
25 | { |
---|
26 | template <typename C> |
---|
27 | struct type_serializer_map |
---|
28 | { |
---|
29 | typedef std::type_info type_id; |
---|
30 | typedef xml::qualified_name<C> qualified_name; |
---|
31 | typedef void (*serializer) (xercesc::DOMElement&, const type&); |
---|
32 | |
---|
33 | void |
---|
34 | register_type (const type_id&, |
---|
35 | const qualified_name& name, |
---|
36 | serializer, |
---|
37 | bool override = true); |
---|
38 | |
---|
39 | void |
---|
40 | register_element (const qualified_name& root, |
---|
41 | const qualified_name& subst, |
---|
42 | const type_id&, |
---|
43 | serializer); |
---|
44 | |
---|
45 | public: |
---|
46 | void |
---|
47 | serialize (const C* name, // element name |
---|
48 | const C* ns, // element namespace |
---|
49 | bool global, |
---|
50 | bool qualified, |
---|
51 | xercesc::DOMElement& parent, |
---|
52 | const type&) const; |
---|
53 | |
---|
54 | // Serialize into existing element. |
---|
55 | // |
---|
56 | void |
---|
57 | serialize (const C* static_name, |
---|
58 | const C* static_ns, |
---|
59 | xercesc::DOMElement&, |
---|
60 | const qualified_name&, |
---|
61 | const type&) const; |
---|
62 | |
---|
63 | // Create DOMDocument with root element suitable for serializing |
---|
64 | // X into it. |
---|
65 | // |
---|
66 | xml::dom::auto_ptr<xercesc::DOMDocument> |
---|
67 | serialize (const C* name, // element name |
---|
68 | const C* ns, // element namespace |
---|
69 | const xml::dom::namespace_infomap<C>&, |
---|
70 | const type&, |
---|
71 | unsigned long flags) const; |
---|
72 | |
---|
73 | public: |
---|
74 | type_serializer_map (); |
---|
75 | |
---|
76 | public: |
---|
77 | struct type_info |
---|
78 | { |
---|
79 | type_info (const qualified_name& name, |
---|
80 | typename type_serializer_map::serializer serializer) |
---|
81 | : name_ (name), serializer_ (serializer) |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | const qualified_name& |
---|
86 | name () const |
---|
87 | { |
---|
88 | return name_; |
---|
89 | } |
---|
90 | |
---|
91 | typename type_serializer_map::serializer |
---|
92 | serializer () const |
---|
93 | { |
---|
94 | return serializer_; |
---|
95 | } |
---|
96 | |
---|
97 | // For std::map. |
---|
98 | // |
---|
99 | type_info () |
---|
100 | : name_ (std::basic_string<C> (), std::basic_string<C> ()), |
---|
101 | serializer_ (0) |
---|
102 | { |
---|
103 | } |
---|
104 | |
---|
105 | private: |
---|
106 | qualified_name name_; |
---|
107 | typename type_serializer_map::serializer serializer_; |
---|
108 | }; |
---|
109 | |
---|
110 | public: |
---|
111 | const type_info* |
---|
112 | find (const type_id&) const; |
---|
113 | |
---|
114 | private: |
---|
115 | struct type_id_comparator |
---|
116 | { |
---|
117 | bool |
---|
118 | operator() (const type_id* x, const type_id* y) const |
---|
119 | { |
---|
120 | return x->before (*y); |
---|
121 | } |
---|
122 | }; |
---|
123 | |
---|
124 | typedef |
---|
125 | std::map<const type_id*, type_info, type_id_comparator> |
---|
126 | type_map; |
---|
127 | |
---|
128 | // Map of (root-element to map of (type_id to type_info)). |
---|
129 | // Note that here type_info::name is element name. |
---|
130 | // |
---|
131 | typedef |
---|
132 | std::map<const type_id*, type_info, type_id_comparator> |
---|
133 | subst_map; |
---|
134 | |
---|
135 | typedef |
---|
136 | std::map<qualified_name, subst_map> |
---|
137 | element_map; |
---|
138 | |
---|
139 | type_map type_map_; |
---|
140 | element_map element_map_; |
---|
141 | |
---|
142 | private: |
---|
143 | const type_info* |
---|
144 | find_substitution (const subst_map& start, const type_id&) const; |
---|
145 | |
---|
146 | // Sets an xsi:type attribute corresponding to the type_info. |
---|
147 | // |
---|
148 | void |
---|
149 | set_xsi_type (xercesc::DOMElement&, const type_info&) const; |
---|
150 | }; |
---|
151 | |
---|
152 | |
---|
153 | // |
---|
154 | // |
---|
155 | template<unsigned long id, typename C> |
---|
156 | struct type_serializer_plate |
---|
157 | { |
---|
158 | static type_serializer_map<C>* map; |
---|
159 | static unsigned long count; |
---|
160 | |
---|
161 | type_serializer_plate (); |
---|
162 | ~type_serializer_plate (); |
---|
163 | }; |
---|
164 | |
---|
165 | template<unsigned long id, typename C> |
---|
166 | type_serializer_map<C>* type_serializer_plate<id, C>::map = 0; |
---|
167 | |
---|
168 | template<unsigned long id, typename C> |
---|
169 | unsigned long type_serializer_plate<id, C>::count = 0; |
---|
170 | |
---|
171 | |
---|
172 | // |
---|
173 | // |
---|
174 | template<unsigned long id, typename C> |
---|
175 | inline type_serializer_map<C>& |
---|
176 | type_serializer_map_instance () |
---|
177 | { |
---|
178 | return *type_serializer_plate<id, C>::map; |
---|
179 | } |
---|
180 | |
---|
181 | // |
---|
182 | // |
---|
183 | template<typename X> |
---|
184 | void |
---|
185 | serializer_impl (xercesc::DOMElement&, const type&); |
---|
186 | |
---|
187 | |
---|
188 | template<unsigned long id, typename C, typename X> |
---|
189 | struct type_serializer_initializer |
---|
190 | { |
---|
191 | // Register type. |
---|
192 | // |
---|
193 | type_serializer_initializer (const C* name, const C* ns); |
---|
194 | |
---|
195 | // Register element. |
---|
196 | // |
---|
197 | type_serializer_initializer (const C* root_name, const C* root_ns, |
---|
198 | const C* subst_name, const C* subst_ns); |
---|
199 | }; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | #include <xsd/cxx/tree/type-serializer-map.txx> |
---|
205 | |
---|
206 | #endif // XSD_CXX_TREE_TYPE_SERIALIZER_MAP_HXX |
---|