1 | // file : xsd/cxx/tree/stream-insertion-map.txx |
---|
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 | #include <xsd/cxx/tree/types.hxx> |
---|
7 | #include <xsd/cxx/tree/stream-insertion.hxx> |
---|
8 | #include <xsd/cxx/tree/bits/literals.hxx> |
---|
9 | |
---|
10 | namespace xsd |
---|
11 | { |
---|
12 | namespace cxx |
---|
13 | { |
---|
14 | namespace tree |
---|
15 | { |
---|
16 | // stream_insertion_map |
---|
17 | // |
---|
18 | template <typename S, typename C> |
---|
19 | stream_insertion_map<S, C>:: |
---|
20 | stream_insertion_map () |
---|
21 | { |
---|
22 | // Register inserters for built-in non-fundamental types. |
---|
23 | // |
---|
24 | std::basic_string<C> xsd (bits::xml_schema<C> ()); |
---|
25 | |
---|
26 | |
---|
27 | // anyType and anySimpleType. |
---|
28 | // |
---|
29 | register_type ( |
---|
30 | typeid (type), |
---|
31 | qualified_name (bits::any_type<C> (), xsd), |
---|
32 | &inserter_impl<S, type>, |
---|
33 | false); |
---|
34 | |
---|
35 | typedef simple_type<type> simple_type; |
---|
36 | register_type ( |
---|
37 | typeid (simple_type), |
---|
38 | qualified_name (bits::any_simple_type<C> (), xsd), |
---|
39 | &inserter_impl<S, simple_type>, |
---|
40 | false); |
---|
41 | |
---|
42 | |
---|
43 | // Strings |
---|
44 | // |
---|
45 | typedef string<C, simple_type> string; |
---|
46 | register_type ( |
---|
47 | typeid (string), |
---|
48 | qualified_name (bits::string<C> (), xsd), |
---|
49 | &inserter_impl<S, string>, |
---|
50 | false); |
---|
51 | |
---|
52 | typedef normalized_string<C, string> normalized_string; |
---|
53 | register_type ( |
---|
54 | typeid (normalized_string), |
---|
55 | qualified_name (bits::normalized_string<C> (), xsd), |
---|
56 | &inserter_impl<S, normalized_string>, |
---|
57 | false); |
---|
58 | |
---|
59 | typedef token<C, normalized_string> token; |
---|
60 | register_type ( |
---|
61 | typeid (token), |
---|
62 | qualified_name (bits::token<C> (), xsd), |
---|
63 | &inserter_impl<S, token>, |
---|
64 | false); |
---|
65 | |
---|
66 | typedef name<C, token> name; |
---|
67 | register_type ( |
---|
68 | typeid (name), |
---|
69 | qualified_name (bits::name<C> (), xsd), |
---|
70 | &inserter_impl<S, name>, |
---|
71 | false); |
---|
72 | |
---|
73 | typedef nmtoken<C, token> nmtoken; |
---|
74 | register_type ( |
---|
75 | typeid (nmtoken), |
---|
76 | qualified_name (bits::nmtoken<C> (), xsd), |
---|
77 | &inserter_impl<S, nmtoken>, |
---|
78 | false); |
---|
79 | |
---|
80 | typedef nmtokens<C, simple_type, nmtoken> nmtokens; |
---|
81 | register_type ( |
---|
82 | typeid (nmtokens), |
---|
83 | qualified_name (bits::nmtokens<C> (), xsd), |
---|
84 | &inserter_impl<S, nmtokens>, |
---|
85 | false); |
---|
86 | |
---|
87 | typedef ncname<C, name> ncname; |
---|
88 | register_type ( |
---|
89 | typeid (ncname), |
---|
90 | qualified_name (bits::ncname<C> (), xsd), |
---|
91 | &inserter_impl<S, ncname>, |
---|
92 | false); |
---|
93 | |
---|
94 | typedef language<C, token> language; |
---|
95 | register_type ( |
---|
96 | typeid (language), |
---|
97 | qualified_name (bits::language<C> (), xsd), |
---|
98 | &inserter_impl<S, language>, |
---|
99 | false); |
---|
100 | |
---|
101 | |
---|
102 | // ID/IDREF. |
---|
103 | // |
---|
104 | typedef id<C, ncname> id; |
---|
105 | register_type ( |
---|
106 | typeid (id), |
---|
107 | qualified_name (bits::id<C> (), xsd), |
---|
108 | &inserter_impl<S, id>, |
---|
109 | false); |
---|
110 | |
---|
111 | typedef idref<type, C, ncname> idref; |
---|
112 | register_type ( |
---|
113 | typeid (idref), |
---|
114 | qualified_name (bits::idref<C> (), xsd), |
---|
115 | &inserter_impl<S, idref>, |
---|
116 | false); |
---|
117 | |
---|
118 | typedef idrefs<C, simple_type, idref> idrefs; |
---|
119 | register_type ( |
---|
120 | typeid (idrefs), |
---|
121 | qualified_name (bits::idrefs<C> (), xsd), |
---|
122 | &inserter_impl<S, idrefs>, |
---|
123 | false); |
---|
124 | |
---|
125 | |
---|
126 | // URI. |
---|
127 | // |
---|
128 | typedef uri<C, simple_type> uri; |
---|
129 | register_type ( |
---|
130 | typeid (uri), |
---|
131 | qualified_name (bits::any_uri<C> (), xsd), |
---|
132 | &inserter_impl<S, uri>, |
---|
133 | false); |
---|
134 | |
---|
135 | |
---|
136 | // Qualified name. |
---|
137 | // |
---|
138 | typedef qname<C, simple_type, uri, ncname> qname; |
---|
139 | register_type ( |
---|
140 | typeid (qname), |
---|
141 | qualified_name (bits::qname<C> (), xsd), |
---|
142 | &inserter_impl<S, qname>, |
---|
143 | false); |
---|
144 | |
---|
145 | |
---|
146 | // Binary. |
---|
147 | // |
---|
148 | typedef base64_binary<C, simple_type> base64_binary; |
---|
149 | register_type ( |
---|
150 | typeid (base64_binary), |
---|
151 | qualified_name (bits::base64_binary<C> (), xsd), |
---|
152 | &inserter_impl<S, base64_binary>, |
---|
153 | false); |
---|
154 | |
---|
155 | typedef hex_binary<C, simple_type> hex_binary; |
---|
156 | register_type ( |
---|
157 | typeid (hex_binary), |
---|
158 | qualified_name (bits::hex_binary<C> (), xsd), |
---|
159 | &inserter_impl<S, hex_binary>, |
---|
160 | false); |
---|
161 | |
---|
162 | |
---|
163 | // Date/time. |
---|
164 | // |
---|
165 | typedef gday<C, simple_type> gday; |
---|
166 | register_type ( |
---|
167 | typeid (gday), |
---|
168 | qualified_name (bits::gday<C> (), xsd), |
---|
169 | &inserter_impl<S, gday>, |
---|
170 | false); |
---|
171 | |
---|
172 | typedef gmonth<C, simple_type> gmonth; |
---|
173 | register_type ( |
---|
174 | typeid (gmonth), |
---|
175 | qualified_name (bits::gmonth<C> (), xsd), |
---|
176 | &inserter_impl<S, gmonth>, |
---|
177 | false); |
---|
178 | |
---|
179 | typedef gyear<C, simple_type> gyear; |
---|
180 | register_type ( |
---|
181 | typeid (gyear), |
---|
182 | qualified_name (bits::gyear<C> (), xsd), |
---|
183 | &inserter_impl<S, gyear>, |
---|
184 | false); |
---|
185 | |
---|
186 | typedef gmonth_day<C, simple_type> gmonth_day; |
---|
187 | register_type ( |
---|
188 | typeid (gmonth_day), |
---|
189 | qualified_name (bits::gmonth_day<C> (), xsd), |
---|
190 | &inserter_impl<S, gmonth_day>, |
---|
191 | false); |
---|
192 | |
---|
193 | typedef gyear_month<C, simple_type> gyear_month; |
---|
194 | register_type ( |
---|
195 | typeid (gyear_month), |
---|
196 | qualified_name (bits::gyear_month<C> (), xsd), |
---|
197 | &inserter_impl<S, gyear_month>, |
---|
198 | false); |
---|
199 | |
---|
200 | typedef date<C, simple_type> date; |
---|
201 | register_type ( |
---|
202 | typeid (date), |
---|
203 | qualified_name (bits::date<C> (), xsd), |
---|
204 | &inserter_impl<S, date>, |
---|
205 | false); |
---|
206 | |
---|
207 | typedef time<C, simple_type> time; |
---|
208 | register_type ( |
---|
209 | typeid (time), |
---|
210 | qualified_name (bits::time<C> (), xsd), |
---|
211 | &inserter_impl<S, time>, |
---|
212 | false); |
---|
213 | |
---|
214 | typedef date_time<C, simple_type> date_time; |
---|
215 | register_type ( |
---|
216 | typeid (date_time), |
---|
217 | qualified_name (bits::date_time<C> (), xsd), |
---|
218 | &inserter_impl<S, date_time>, |
---|
219 | false); |
---|
220 | |
---|
221 | typedef duration<C, simple_type> duration; |
---|
222 | register_type ( |
---|
223 | typeid (duration), |
---|
224 | qualified_name (bits::duration<C> (), xsd), |
---|
225 | &inserter_impl<S, duration>, |
---|
226 | false); |
---|
227 | |
---|
228 | |
---|
229 | // Entity. |
---|
230 | // |
---|
231 | typedef entity<C, ncname> entity; |
---|
232 | register_type ( |
---|
233 | typeid (entity), |
---|
234 | qualified_name (bits::entity<C> (), xsd), |
---|
235 | &inserter_impl<S, entity>, |
---|
236 | false); |
---|
237 | |
---|
238 | typedef entities<C, simple_type, entity> entities; |
---|
239 | register_type ( |
---|
240 | typeid (entities), |
---|
241 | qualified_name (bits::entities<C> (), xsd), |
---|
242 | &inserter_impl<S, entities>, |
---|
243 | false); |
---|
244 | } |
---|
245 | |
---|
246 | template <typename S, typename C> |
---|
247 | void stream_insertion_map<S, C>:: |
---|
248 | register_type (const type_id& tid, |
---|
249 | const qualified_name& name, |
---|
250 | inserter i, |
---|
251 | bool override) |
---|
252 | { |
---|
253 | if (override || type_map_.find (&tid) == type_map_.end ()) |
---|
254 | type_map_[&tid] = type_info (name, i); |
---|
255 | } |
---|
256 | |
---|
257 | template <typename S, typename C> |
---|
258 | void stream_insertion_map<S, C>:: |
---|
259 | insert (ostream<S>& s, const type& x) |
---|
260 | { |
---|
261 | if (const type_info* ti = find (typeid (x))) |
---|
262 | { |
---|
263 | const qualified_name& qn (ti->name ()); |
---|
264 | |
---|
265 | s << qn.namespace_ () << qn.name (); |
---|
266 | ti->inserter () (s, x); |
---|
267 | } |
---|
268 | else |
---|
269 | throw no_type_info<C> (std::basic_string<C> (), |
---|
270 | std::basic_string<C> ()); // @@ TODO |
---|
271 | } |
---|
272 | |
---|
273 | template <typename S, typename C> |
---|
274 | const typename stream_insertion_map<S, C>::type_info* |
---|
275 | stream_insertion_map<S, C>:: |
---|
276 | find (const type_id& tid) const |
---|
277 | { |
---|
278 | typename type_map::const_iterator i (type_map_.find (&tid)); |
---|
279 | return i == type_map_.end () ? 0 : &i->second; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | // stream_insertion_plate |
---|
284 | // |
---|
285 | template<unsigned long id, typename S, typename C> |
---|
286 | stream_insertion_plate<id, S, C>:: |
---|
287 | stream_insertion_plate () |
---|
288 | { |
---|
289 | if (count == 0) |
---|
290 | map = new stream_insertion_map<S, C>; |
---|
291 | |
---|
292 | ++count; |
---|
293 | } |
---|
294 | |
---|
295 | template<unsigned long id, typename S, typename C> |
---|
296 | stream_insertion_plate<id, S, C>:: |
---|
297 | ~stream_insertion_plate () |
---|
298 | { |
---|
299 | if (--count == 0) |
---|
300 | delete map; |
---|
301 | } |
---|
302 | |
---|
303 | // |
---|
304 | // |
---|
305 | template<typename S, typename X> |
---|
306 | void |
---|
307 | inserter_impl (ostream<S>& s, const type& x) |
---|
308 | { |
---|
309 | s << static_cast<const X&> (x); |
---|
310 | } |
---|
311 | |
---|
312 | // stream_insertion_initializer |
---|
313 | // |
---|
314 | template<unsigned long id, typename S, typename C, typename X> |
---|
315 | stream_insertion_initializer<id, S, C, X>:: |
---|
316 | stream_insertion_initializer (const C* name, const C* ns) |
---|
317 | { |
---|
318 | stream_insertion_map_instance<id, S, C> ().register_type ( |
---|
319 | typeid (X), |
---|
320 | xml::qualified_name<C> (name, ns), |
---|
321 | &inserter_impl<S, X>); |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|