[111] | 1 | // file : xsd/cxx/tree/containers.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 <ostream> |
---|
| 7 | #include <xsd/cxx/tree/bits/literals.hxx> |
---|
| 8 | |
---|
| 9 | namespace xsd |
---|
| 10 | { |
---|
| 11 | namespace cxx |
---|
| 12 | { |
---|
| 13 | namespace tree |
---|
| 14 | { |
---|
| 15 | // one |
---|
| 16 | // |
---|
| 17 | template<typename X> |
---|
| 18 | one<X, false>:: |
---|
| 19 | ~one () |
---|
| 20 | { |
---|
| 21 | delete x_; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | template<typename X> |
---|
| 25 | one<X, false>:: |
---|
| 26 | one (flags f, container* c) |
---|
| 27 | : x_ (0), flags_ (f), container_ (c) |
---|
| 28 | { |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | template<typename X> |
---|
| 32 | one<X, false>:: |
---|
| 33 | one (const X& x, flags f, container* c) |
---|
| 34 | : x_ (0), flags_ (f), container_ (c) |
---|
| 35 | { |
---|
| 36 | set (x); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | template<typename X> |
---|
| 40 | one<X, false>:: |
---|
| 41 | one (std::auto_ptr<X> x, flags f, container* c) |
---|
| 42 | : x_ (0), flags_ (f), container_ (c) |
---|
| 43 | { |
---|
| 44 | set (x); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | template<typename X> |
---|
| 48 | one<X, false>:: |
---|
| 49 | one (const one<X, false>& x, flags f, container* c) |
---|
| 50 | : x_ (0), flags_ (f), container_ (c) |
---|
| 51 | { |
---|
| 52 | if (x.present ()) |
---|
| 53 | set (x.get ()); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | template<typename X> |
---|
| 57 | one<X, false>& one<X, false>:: |
---|
| 58 | operator= (const one<X, false>& x) |
---|
| 59 | { |
---|
| 60 | if (this == &x) |
---|
| 61 | return *this; |
---|
| 62 | |
---|
| 63 | if (x.present ()) |
---|
| 64 | set (x.get ()); |
---|
| 65 | else |
---|
| 66 | { |
---|
| 67 | delete x_; |
---|
| 68 | x_ = 0; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return *this; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | template<typename X> |
---|
| 75 | void one<X, false>:: |
---|
| 76 | set (const X& x) |
---|
| 77 | { |
---|
| 78 | // We always do a fresh copy because X may not be x's |
---|
| 79 | // dynamic type. |
---|
| 80 | // |
---|
| 81 | X* r (x._clone (flags_, container_)); |
---|
| 82 | |
---|
| 83 | delete x_; |
---|
| 84 | x_ = r; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | template<typename X> |
---|
| 88 | void one<X, false>:: |
---|
| 89 | set (std::auto_ptr<X> x) |
---|
| 90 | { |
---|
| 91 | X* r (0); |
---|
| 92 | |
---|
| 93 | if (x.get () != 0) |
---|
| 94 | { |
---|
| 95 | if (x->_container () != container_) |
---|
| 96 | x->_container (container_); |
---|
| 97 | |
---|
| 98 | r = x.release (); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | delete x_; |
---|
| 102 | x_ = r; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // optional |
---|
| 106 | // |
---|
| 107 | template <typename X> |
---|
| 108 | optional<X, false>:: |
---|
| 109 | ~optional () |
---|
| 110 | { |
---|
| 111 | delete x_; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | template <typename X> |
---|
| 115 | optional<X, false>:: |
---|
| 116 | optional (flags f, container* c) |
---|
| 117 | : x_ (0), flags_ (f), container_ (c) |
---|
| 118 | { |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | template <typename X> |
---|
| 122 | optional<X, false>:: |
---|
| 123 | optional (const X& x, flags f, container* c) |
---|
| 124 | : x_ (0), flags_ (f), container_ (c) |
---|
| 125 | { |
---|
| 126 | set (x); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | template <typename X> |
---|
| 130 | optional<X, false>:: |
---|
| 131 | optional (std::auto_ptr<X> x, flags f, container* c) |
---|
| 132 | : x_ (0), flags_ (f), container_ (c) |
---|
| 133 | { |
---|
| 134 | set (x); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | template <typename X> |
---|
| 138 | optional<X, false>:: |
---|
| 139 | optional (const optional<X, false>& x, flags f, container* c) |
---|
| 140 | : x_ (0), flags_ (f), container_ (c) |
---|
| 141 | { |
---|
| 142 | if (x) |
---|
| 143 | set (*x); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | template <typename X> |
---|
| 147 | optional<X, false>& optional<X, false>:: |
---|
| 148 | operator= (const X& x) |
---|
| 149 | { |
---|
| 150 | if (x_ == &x) |
---|
| 151 | return *this; |
---|
| 152 | |
---|
| 153 | set (x); |
---|
| 154 | |
---|
| 155 | return *this; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | template <typename X> |
---|
| 159 | optional<X, false>& optional<X, false>:: |
---|
| 160 | operator= (const optional<X, false>& x) |
---|
| 161 | { |
---|
| 162 | if (this == &x) |
---|
| 163 | return *this; |
---|
| 164 | |
---|
| 165 | if (x) |
---|
| 166 | set (*x); |
---|
| 167 | else |
---|
| 168 | reset (); |
---|
| 169 | |
---|
| 170 | return *this; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | template <typename X> |
---|
| 174 | void optional<X, false>:: |
---|
| 175 | set (const X& x) |
---|
| 176 | { |
---|
| 177 | // We always do a fresh copy because X may not be x's |
---|
| 178 | // dynamic type. |
---|
| 179 | // |
---|
| 180 | X* r (x._clone (flags_, container_)); |
---|
| 181 | |
---|
| 182 | delete x_; |
---|
| 183 | x_ = r; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | template <typename X> |
---|
| 187 | void optional<X, false>:: |
---|
| 188 | set (std::auto_ptr<X> x) |
---|
| 189 | { |
---|
| 190 | X* r (0); |
---|
| 191 | |
---|
| 192 | if (x.get () != 0) |
---|
| 193 | { |
---|
| 194 | if (x->_container () != container_) |
---|
| 195 | x->_container (container_); |
---|
| 196 | |
---|
| 197 | r = x.release (); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | delete x_; |
---|
| 201 | x_ = r; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | template <typename X> |
---|
| 205 | void optional<X, false>:: |
---|
| 206 | reset () |
---|
| 207 | { |
---|
| 208 | delete x_; |
---|
| 209 | x_ = 0; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | template <typename X> |
---|
| 213 | void optional<X, false>:: |
---|
| 214 | true_ () |
---|
| 215 | { |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | // optional |
---|
| 220 | // |
---|
| 221 | template <typename X> |
---|
| 222 | optional<X, true>:: |
---|
| 223 | optional (const X& y, flags, container*) |
---|
| 224 | : present_ (false) |
---|
| 225 | { |
---|
| 226 | set (y); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | template <typename X> |
---|
| 230 | optional<X, true>:: |
---|
| 231 | optional (const optional<X, true>& y, flags, container*) |
---|
| 232 | : present_ (false) |
---|
| 233 | { |
---|
| 234 | if (y) |
---|
| 235 | set (*y); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | template <typename X> |
---|
| 239 | optional<X, true>& optional<X, true>:: |
---|
| 240 | operator= (const X& y) |
---|
| 241 | { |
---|
| 242 | if (&x_ == &y) |
---|
| 243 | return *this; |
---|
| 244 | |
---|
| 245 | set (y); |
---|
| 246 | |
---|
| 247 | return *this; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | template <typename X> |
---|
| 251 | optional<X, true>& optional<X, true>:: |
---|
| 252 | operator= (const optional<X, true>& y) |
---|
| 253 | { |
---|
| 254 | if (this == &y) |
---|
| 255 | return *this; |
---|
| 256 | |
---|
| 257 | if (y) |
---|
| 258 | set (*y); |
---|
| 259 | else |
---|
| 260 | reset (); |
---|
| 261 | |
---|
| 262 | return *this; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | template <typename X> |
---|
| 266 | void optional<X, true>:: |
---|
| 267 | true_ () |
---|
| 268 | { |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | template <typename C, typename X, bool fund> |
---|
| 272 | std::basic_ostream<C>& |
---|
| 273 | operator<< (std::basic_ostream<C>& os, const optional<X, fund>& x) |
---|
| 274 | { |
---|
| 275 | if (x) |
---|
| 276 | os << *x; |
---|
| 277 | else |
---|
| 278 | os << bits::not_present<C> (); |
---|
| 279 | |
---|
| 280 | return os; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|