| 1 | // file : xsd/cxx/tree/istream.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_ISTREAM_HXX |
|---|
| 7 | #define XSD_CXX_TREE_ISTREAM_HXX |
|---|
| 8 | |
|---|
| 9 | #include <cstddef> // std::size_t |
|---|
| 10 | |
|---|
| 11 | #include <xsd/cxx/tree/istream-fwd.hxx> |
|---|
| 12 | |
|---|
| 13 | namespace xsd |
|---|
| 14 | { |
|---|
| 15 | namespace cxx |
|---|
| 16 | { |
|---|
| 17 | namespace tree |
|---|
| 18 | { |
|---|
| 19 | class istream_common |
|---|
| 20 | { |
|---|
| 21 | public: |
|---|
| 22 | template <typename X> |
|---|
| 23 | struct as_size |
|---|
| 24 | { |
|---|
| 25 | explicit as_size (X& x) : x_ (x) {} |
|---|
| 26 | X& x_; |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | // 8-bit |
|---|
| 31 | // |
|---|
| 32 | template <typename X> |
|---|
| 33 | struct as_int8 |
|---|
| 34 | { |
|---|
| 35 | explicit as_int8 (X& x) : x_ (x) {} |
|---|
| 36 | X& x_; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | template <typename X> |
|---|
| 40 | struct as_uint8 |
|---|
| 41 | { |
|---|
| 42 | explicit as_uint8 (X& x) : x_ (x) {} |
|---|
| 43 | X& x_; |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | // 16-bit |
|---|
| 48 | // |
|---|
| 49 | template <typename X> |
|---|
| 50 | struct as_int16 |
|---|
| 51 | { |
|---|
| 52 | explicit as_int16 (X& x) : x_ (x) {} |
|---|
| 53 | X& x_; |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | template <typename X> |
|---|
| 57 | struct as_uint16 |
|---|
| 58 | { |
|---|
| 59 | explicit as_uint16 (X& x) : x_ (x) {} |
|---|
| 60 | X& x_; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | // 32-bit |
|---|
| 65 | // |
|---|
| 66 | template <typename X> |
|---|
| 67 | struct as_int32 |
|---|
| 68 | { |
|---|
| 69 | explicit as_int32 (X& x) : x_ (x) {} |
|---|
| 70 | X& x_; |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | template <typename X> |
|---|
| 74 | struct as_uint32 |
|---|
| 75 | { |
|---|
| 76 | explicit as_uint32 (X& x) : x_ (x) {} |
|---|
| 77 | X& x_; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | // 64-bit |
|---|
| 82 | // |
|---|
| 83 | template <typename X> |
|---|
| 84 | struct as_int64 |
|---|
| 85 | { |
|---|
| 86 | explicit as_int64 (X& x) : x_ (x) {} |
|---|
| 87 | X& x_; |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | template <typename X> |
|---|
| 91 | struct as_uint64 |
|---|
| 92 | { |
|---|
| 93 | explicit as_uint64 (X& x) : x_ (x) {} |
|---|
| 94 | X& x_; |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | // Boolean |
|---|
| 99 | // |
|---|
| 100 | template <typename X> |
|---|
| 101 | struct as_bool |
|---|
| 102 | { |
|---|
| 103 | explicit as_bool (X& x) : x_ (x) {} |
|---|
| 104 | X& x_; |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | // Floating-point |
|---|
| 109 | // |
|---|
| 110 | template <typename X> |
|---|
| 111 | struct as_float32 |
|---|
| 112 | { |
|---|
| 113 | explicit as_float32 (X& x) : x_ (x) {} |
|---|
| 114 | X& x_; |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | template <typename X> |
|---|
| 118 | struct as_float64 |
|---|
| 119 | { |
|---|
| 120 | explicit as_float64 (X& x) : x_ (x) {} |
|---|
| 121 | X& x_; |
|---|
| 122 | }; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | template<typename S> |
|---|
| 126 | class istream: public istream_common |
|---|
| 127 | { |
|---|
| 128 | public: |
|---|
| 129 | explicit |
|---|
| 130 | istream (S& s) |
|---|
| 131 | : s_ (s) |
|---|
| 132 | { |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | S& |
|---|
| 136 | impl () |
|---|
| 137 | { |
|---|
| 138 | return s_; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | private: |
|---|
| 142 | istream (const istream&); |
|---|
| 143 | istream& |
|---|
| 144 | operator= (const istream&); |
|---|
| 145 | |
|---|
| 146 | private: |
|---|
| 147 | S& s_; |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | // 8-bit |
|---|
| 152 | // |
|---|
| 153 | template <typename S> |
|---|
| 154 | inline istream<S>& |
|---|
| 155 | operator>> (istream<S>& s, signed char& x) |
|---|
| 156 | { |
|---|
| 157 | istream_common::as_int8<signed char> as_int8 (x); |
|---|
| 158 | return s >> as_int8; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | template <typename S> |
|---|
| 162 | inline istream<S>& |
|---|
| 163 | operator>> (istream<S>& s, unsigned char& x) |
|---|
| 164 | { |
|---|
| 165 | istream_common::as_uint8<unsigned char> as_uint8 (x); |
|---|
| 166 | return s >> as_uint8; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | // 16-bit |
|---|
| 171 | // |
|---|
| 172 | template <typename S> |
|---|
| 173 | inline istream<S>& |
|---|
| 174 | operator>> (istream<S>& s, short& x) |
|---|
| 175 | { |
|---|
| 176 | istream_common::as_int16<short> as_int16 (x); |
|---|
| 177 | return s >> as_int16; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | template <typename S> |
|---|
| 181 | inline istream<S>& |
|---|
| 182 | operator>> (istream<S>& s, unsigned short& x) |
|---|
| 183 | { |
|---|
| 184 | istream_common::as_uint16<unsigned short> as_uint16 (x); |
|---|
| 185 | return s >> as_uint16; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | // 32-bit |
|---|
| 190 | // |
|---|
| 191 | template <typename S> |
|---|
| 192 | inline istream<S>& |
|---|
| 193 | operator>> (istream<S>& s, int& x) |
|---|
| 194 | { |
|---|
| 195 | istream_common::as_int32<int> as_int32 (x); |
|---|
| 196 | return s >> as_int32; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | template <typename S> |
|---|
| 200 | inline istream<S>& |
|---|
| 201 | operator>> (istream<S>& s, unsigned int& x) |
|---|
| 202 | { |
|---|
| 203 | istream_common::as_uint32<unsigned int> as_uint32 (x); |
|---|
| 204 | return s >> as_uint32; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | // 64-bit |
|---|
| 209 | // |
|---|
| 210 | template <typename S> |
|---|
| 211 | inline istream<S>& |
|---|
| 212 | operator>> (istream<S>& s, long long& x) |
|---|
| 213 | { |
|---|
| 214 | istream_common::as_int64<long long> as_int64 (x); |
|---|
| 215 | return s >> as_int64; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | template <typename S> |
|---|
| 219 | inline istream<S>& |
|---|
| 220 | operator>> (istream<S>& s, unsigned long long& x) |
|---|
| 221 | { |
|---|
| 222 | istream_common::as_uint64<unsigned long long> as_uint64 (x); |
|---|
| 223 | return s >> as_uint64; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | // Boolean |
|---|
| 227 | // |
|---|
| 228 | template <typename S> |
|---|
| 229 | inline istream<S>& |
|---|
| 230 | operator>> (istream<S>& s, bool& x) |
|---|
| 231 | { |
|---|
| 232 | istream_common::as_bool<bool> as_bool (x); |
|---|
| 233 | return s >> as_bool; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | // Floating-point |
|---|
| 238 | // |
|---|
| 239 | template <typename S> |
|---|
| 240 | inline istream<S>& |
|---|
| 241 | operator>> (istream<S>& s, float& x) |
|---|
| 242 | { |
|---|
| 243 | istream_common::as_float32<float> as_float32 (x); |
|---|
| 244 | return s >> as_float32; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | template <typename S> |
|---|
| 248 | inline istream<S>& |
|---|
| 249 | operator>> (istream<S>& s, double& x) |
|---|
| 250 | { |
|---|
| 251 | istream_common::as_float64<double> as_float64 (x); |
|---|
| 252 | return s >> as_float64; |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | #endif // XSD_CXX_TREE_ISTREAM_HXX |
|---|