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