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