| 1 | // file : xsd/cxx/parser/substitution-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_PARSER_SUBSTITUTION_MAP_HXX |
|---|
| 7 | #define XSD_CXX_PARSER_SUBSTITUTION_MAP_HXX |
|---|
| 8 | |
|---|
| 9 | #include <map> |
|---|
| 10 | #include <cstddef> // std::size_t |
|---|
| 11 | |
|---|
| 12 | #include <xsd/cxx/ro-string.hxx> |
|---|
| 13 | |
|---|
| 14 | namespace xsd |
|---|
| 15 | { |
|---|
| 16 | namespace cxx |
|---|
| 17 | { |
|---|
| 18 | namespace parser |
|---|
| 19 | { |
|---|
| 20 | template <typename C> |
|---|
| 21 | struct substitution_map_key |
|---|
| 22 | { |
|---|
| 23 | substitution_map_key (const C* ns, const C* name) |
|---|
| 24 | : ns_ (ns), name_ (name) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | substitution_map_key (const ro_string<C>& ns, |
|---|
| 29 | const ro_string<C>& name) |
|---|
| 30 | : ns_ (ns.data (), ns.size ()), |
|---|
| 31 | name_ (name.data (), name.size ()) |
|---|
| 32 | { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | substitution_map_key (const substitution_map_key& x) |
|---|
| 36 | : ns_ (x.ns_.data (), x.ns_.size ()), |
|---|
| 37 | name_ (x.name_.data (), x.name_.size ()) |
|---|
| 38 | { |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | private: |
|---|
| 42 | substitution_map_key& |
|---|
| 43 | operator= (const substitution_map_key&); |
|---|
| 44 | |
|---|
| 45 | public: |
|---|
| 46 | const ro_string<C>& |
|---|
| 47 | ns () const |
|---|
| 48 | { |
|---|
| 49 | return ns_; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | const ro_string<C>& |
|---|
| 53 | name () const |
|---|
| 54 | { |
|---|
| 55 | return name_; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | private: |
|---|
| 59 | const ro_string<C> ns_; |
|---|
| 60 | const ro_string<C> name_; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | template <typename C> |
|---|
| 64 | inline bool |
|---|
| 65 | operator< (const substitution_map_key<C>& x, |
|---|
| 66 | const substitution_map_key<C>& y) |
|---|
| 67 | { |
|---|
| 68 | int r (x.name ().compare (y.name ())); |
|---|
| 69 | return r < 0 || (r == 0 && x.ns () < y.ns ()); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | template <typename C> |
|---|
| 73 | struct substitution_map_value |
|---|
| 74 | { |
|---|
| 75 | substitution_map_value (const C* ns, const C* name, const C* type) |
|---|
| 76 | : ns_ (ns), name_ (name), type_ (type) |
|---|
| 77 | { |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | substitution_map_value (const substitution_map_value& x) |
|---|
| 81 | : ns_ (x.ns_.data (), x.ns_.size ()), |
|---|
| 82 | name_ (x.name_.data (), x.name_.size ()), |
|---|
| 83 | type_ (x.type_.data (), x.type_.size ()) |
|---|
| 84 | { |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | substitution_map_value& |
|---|
| 88 | operator= (const substitution_map_value& x) |
|---|
| 89 | { |
|---|
| 90 | if (this != &x) |
|---|
| 91 | { |
|---|
| 92 | ns_.assign (x.ns_.data (), x.ns_.size ()); |
|---|
| 93 | name_.assign (x.name_.data (), x.name_.size ()); |
|---|
| 94 | type_.assign (x.type_.data (), x.type_.size ()); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return *this; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public: |
|---|
| 101 | const ro_string<C>& |
|---|
| 102 | ns () const |
|---|
| 103 | { |
|---|
| 104 | return ns_; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | const ro_string<C>& |
|---|
| 108 | name () const |
|---|
| 109 | { |
|---|
| 110 | return name_; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | const ro_string<C>& |
|---|
| 114 | type () const |
|---|
| 115 | { |
|---|
| 116 | return type_; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | private: |
|---|
| 120 | ro_string<C> ns_; |
|---|
| 121 | ro_string<C> name_; |
|---|
| 122 | ro_string<C> type_; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | template <typename C> |
|---|
| 126 | struct substitution_map |
|---|
| 127 | { |
|---|
| 128 | void |
|---|
| 129 | insert (const C* member_ns, |
|---|
| 130 | const C* member_name, |
|---|
| 131 | const C* root_ns, |
|---|
| 132 | const C* root_name, |
|---|
| 133 | const C* member_type) |
|---|
| 134 | { |
|---|
| 135 | key k (member_ns, member_name); |
|---|
| 136 | value v (root_ns, root_name, member_type); |
|---|
| 137 | map_.insert (std::pair<key, value> (k, v)); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // Check and get the type set if found. |
|---|
| 141 | // |
|---|
| 142 | bool |
|---|
| 143 | check (const ro_string<C>& ns, |
|---|
| 144 | const ro_string<C>& name, |
|---|
| 145 | const C* root_ns, |
|---|
| 146 | const C* root_name, |
|---|
| 147 | const ro_string<C>*& type) const |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | return map_.empty () |
|---|
| 151 | ? false |
|---|
| 152 | : check_ (ns, name, root_ns, root_name, &type); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | // Check but don't care about the type. |
|---|
| 156 | // |
|---|
| 157 | bool |
|---|
| 158 | check (const ro_string<C>& ns, |
|---|
| 159 | const ro_string<C>& name, |
|---|
| 160 | const C* root_ns, |
|---|
| 161 | const C* root_name) const |
|---|
| 162 | { |
|---|
| 163 | |
|---|
| 164 | return map_.empty () |
|---|
| 165 | ? false |
|---|
| 166 | : check_ (ns, name, root_ns, root_name, 0); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | private: |
|---|
| 170 | bool |
|---|
| 171 | check_ (const ro_string<C>& ns, |
|---|
| 172 | const ro_string<C>& name, |
|---|
| 173 | const C* root_ns, |
|---|
| 174 | const C* root_name, |
|---|
| 175 | const ro_string<C>** type) const; |
|---|
| 176 | |
|---|
| 177 | private: |
|---|
| 178 | typedef substitution_map_key<C> key; |
|---|
| 179 | typedef substitution_map_value<C> value; |
|---|
| 180 | typedef std::map<key, value> map; |
|---|
| 181 | |
|---|
| 182 | map map_; |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | // Translation unit initializer. |
|---|
| 187 | // |
|---|
| 188 | template<typename C> |
|---|
| 189 | struct substitution_map_init |
|---|
| 190 | { |
|---|
| 191 | static substitution_map<C>* map; |
|---|
| 192 | static std::size_t count; |
|---|
| 193 | |
|---|
| 194 | substitution_map_init (); |
|---|
| 195 | ~substitution_map_init (); |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | template<typename C> |
|---|
| 199 | substitution_map<C>* substitution_map_init<C>::map = 0; |
|---|
| 200 | |
|---|
| 201 | template<typename C> |
|---|
| 202 | std::size_t substitution_map_init<C>::count = 0; |
|---|
| 203 | |
|---|
| 204 | template<typename C> |
|---|
| 205 | inline substitution_map<C>& |
|---|
| 206 | substitution_map_instance () |
|---|
| 207 | { |
|---|
| 208 | return *substitution_map_init<C>::map; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | // Map entry initializer. |
|---|
| 213 | // |
|---|
| 214 | template<typename C> |
|---|
| 215 | struct substitution_map_entry |
|---|
| 216 | { |
|---|
| 217 | substitution_map_entry (const C* member_ns, |
|---|
| 218 | const C* member_name, |
|---|
| 219 | const C* root_ns, |
|---|
| 220 | const C* root_name, |
|---|
| 221 | const C* member_type); |
|---|
| 222 | }; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | #include <xsd/cxx/parser/substitution-map.txx> |
|---|
| 228 | |
|---|
| 229 | #endif // XSD_CXX_PARSER_SUBSTITUTION_MAP_HXX |
|---|