[111] | 1 | // file : xsd/cxx/tree/xdr-stream-extraction.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_XDR_STREAM_EXTRACTION_HXX |
---|
| 7 | #define XSD_CXX_TREE_XDR_STREAM_EXTRACTION_HXX |
---|
| 8 | |
---|
| 9 | #include <rpc/xdr.h> |
---|
| 10 | |
---|
| 11 | #include <string> |
---|
| 12 | |
---|
| 13 | #include <xsd/cxx/tree/buffer.hxx> |
---|
| 14 | #include <xsd/cxx/tree/istream.hxx> |
---|
| 15 | #include <xsd/cxx/tree/xdr-stream-common.hxx> |
---|
| 16 | |
---|
| 17 | namespace xsd |
---|
| 18 | { |
---|
| 19 | namespace cxx |
---|
| 20 | { |
---|
| 21 | namespace tree |
---|
| 22 | { |
---|
| 23 | struct xdr_stream_extraction: xdr_stream_operation |
---|
| 24 | { |
---|
| 25 | virtual const char* |
---|
| 26 | what () const throw () |
---|
| 27 | { |
---|
| 28 | return "XDR stream extraction operation failed"; |
---|
| 29 | } |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | // as_size |
---|
| 34 | // |
---|
| 35 | #ifdef XSD_CXX_TREE_USE_64_BIT_SIZE |
---|
| 36 | template <typename X> |
---|
| 37 | inline istream<XDR>& |
---|
| 38 | operator>> (istream<XDR>& s, istream<XDR>::as_size<X>& x) |
---|
| 39 | { |
---|
| 40 | uint64_t v; |
---|
| 41 | |
---|
| 42 | if (!xdr_uint64_t (&s.impl (), &v) || v > ~(X (0))) |
---|
| 43 | throw xdr_stream_extraction (); |
---|
| 44 | |
---|
| 45 | x.x_ = static_cast<X> (v); |
---|
| 46 | |
---|
| 47 | return s; |
---|
| 48 | } |
---|
| 49 | #else |
---|
| 50 | template <typename X> |
---|
| 51 | inline istream<XDR>& |
---|
| 52 | operator>> (istream<XDR>& s, istream<XDR>::as_size<X>& x) |
---|
| 53 | { |
---|
| 54 | uint32_t v; |
---|
| 55 | |
---|
| 56 | if (!xdr_uint32_t (&s.impl (), &v)) |
---|
| 57 | throw xdr_stream_extraction (); |
---|
| 58 | |
---|
| 59 | x.x_ = static_cast<X> (v); |
---|
| 60 | |
---|
| 61 | return s; |
---|
| 62 | } |
---|
| 63 | #endif |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | // 8-bit |
---|
| 67 | // |
---|
| 68 | template <typename X> |
---|
| 69 | inline istream<XDR>& |
---|
| 70 | operator>> (istream<XDR>& s, istream<XDR>::as_int8<X>& x) |
---|
| 71 | { |
---|
| 72 | int8_t v; |
---|
| 73 | |
---|
| 74 | if (!xdr_int8_t (&s.impl (), &v)) |
---|
| 75 | throw xdr_stream_extraction (); |
---|
| 76 | |
---|
| 77 | x.x_ = static_cast<X> (v); |
---|
| 78 | |
---|
| 79 | return s; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | template <typename X> |
---|
| 83 | inline istream<XDR>& |
---|
| 84 | operator>> (istream<XDR>& s, istream<XDR>::as_uint8<X>& x) |
---|
| 85 | { |
---|
| 86 | uint8_t v; |
---|
| 87 | |
---|
| 88 | if (!xdr_uint8_t (&s.impl (), &v)) |
---|
| 89 | throw xdr_stream_extraction (); |
---|
| 90 | |
---|
| 91 | x.x_ = static_cast<X> (v); |
---|
| 92 | |
---|
| 93 | return s; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | // 16-bit |
---|
| 98 | // |
---|
| 99 | template <typename X> |
---|
| 100 | inline istream<XDR>& |
---|
| 101 | operator>> (istream<XDR>& s, istream<XDR>::as_int16<X>& x) |
---|
| 102 | { |
---|
| 103 | int16_t v; |
---|
| 104 | |
---|
| 105 | if (!xdr_int16_t (&s.impl (), &v)) |
---|
| 106 | throw xdr_stream_extraction (); |
---|
| 107 | |
---|
| 108 | x.x_ = static_cast<X> (v); |
---|
| 109 | |
---|
| 110 | return s; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | template <typename X> |
---|
| 114 | inline istream<XDR>& |
---|
| 115 | operator>> (istream<XDR>& s, istream<XDR>::as_uint16<X>& x) |
---|
| 116 | { |
---|
| 117 | uint16_t v; |
---|
| 118 | |
---|
| 119 | if (!xdr_uint16_t (&s.impl (), &v)) |
---|
| 120 | throw xdr_stream_extraction (); |
---|
| 121 | |
---|
| 122 | x.x_ = static_cast<X> (v); |
---|
| 123 | |
---|
| 124 | return s; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | // 32-bit |
---|
| 129 | // |
---|
| 130 | template <typename X> |
---|
| 131 | inline istream<XDR>& |
---|
| 132 | operator>> (istream<XDR>& s, istream<XDR>::as_int32<X>& x) |
---|
| 133 | { |
---|
| 134 | int32_t v; |
---|
| 135 | |
---|
| 136 | if (!xdr_int32_t (&s.impl (), &v)) |
---|
| 137 | throw xdr_stream_extraction (); |
---|
| 138 | |
---|
| 139 | x.x_ = static_cast<X> (v); |
---|
| 140 | |
---|
| 141 | return s; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template <typename X> |
---|
| 145 | inline istream<XDR>& |
---|
| 146 | operator>> (istream<XDR>& s, istream<XDR>::as_uint32<X>& x) |
---|
| 147 | { |
---|
| 148 | uint32_t v; |
---|
| 149 | |
---|
| 150 | if (!xdr_uint32_t (&s.impl (), &v)) |
---|
| 151 | throw xdr_stream_extraction (); |
---|
| 152 | |
---|
| 153 | x.x_ = static_cast<X> (v); |
---|
| 154 | |
---|
| 155 | return s; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | // 64-bit |
---|
| 160 | // |
---|
| 161 | template <typename X> |
---|
| 162 | inline istream<XDR>& |
---|
| 163 | operator>> (istream<XDR>& s, istream<XDR>::as_int64<X>& x) |
---|
| 164 | { |
---|
| 165 | int64_t v; |
---|
| 166 | |
---|
| 167 | if (!xdr_int64_t (&s.impl (), &v)) |
---|
| 168 | throw xdr_stream_extraction (); |
---|
| 169 | |
---|
| 170 | x.x_ = static_cast<X> (v); |
---|
| 171 | |
---|
| 172 | return s; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | template <typename X> |
---|
| 176 | inline istream<XDR>& |
---|
| 177 | operator>> (istream<XDR>& s, istream<XDR>::as_uint64<X>& x) |
---|
| 178 | { |
---|
| 179 | uint64_t v; |
---|
| 180 | |
---|
| 181 | if (!xdr_uint64_t (&s.impl (), &v)) |
---|
| 182 | throw xdr_stream_extraction (); |
---|
| 183 | |
---|
| 184 | x.x_ = static_cast<X> (v); |
---|
| 185 | |
---|
| 186 | return s; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | // Boolean |
---|
| 191 | // |
---|
| 192 | template <typename X> |
---|
| 193 | inline istream<XDR>& |
---|
| 194 | operator>> (istream<XDR>& s, istream<XDR>::as_bool<X>& x) |
---|
| 195 | { |
---|
| 196 | bool_t v; |
---|
| 197 | |
---|
| 198 | if (!xdr_bool (&s.impl (), &v)) |
---|
| 199 | throw xdr_stream_extraction (); |
---|
| 200 | |
---|
| 201 | x.x_ = static_cast<X> (v); |
---|
| 202 | |
---|
| 203 | return s; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | // Floating-point |
---|
| 208 | // |
---|
| 209 | template <typename X> |
---|
| 210 | inline istream<XDR>& |
---|
| 211 | operator>> (istream<XDR>& s, istream<XDR>::as_float32<X>& x) |
---|
| 212 | { |
---|
| 213 | float v; |
---|
| 214 | |
---|
| 215 | if (!xdr_float (&s.impl (), &v)) |
---|
| 216 | throw xdr_stream_extraction (); |
---|
| 217 | |
---|
| 218 | x.x_ = static_cast<X> (v); |
---|
| 219 | |
---|
| 220 | return s; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | template <typename X> |
---|
| 224 | inline istream<XDR>& |
---|
| 225 | operator>> (istream<XDR>& s, istream<XDR>::as_float64<X>& x) |
---|
| 226 | { |
---|
| 227 | double v; |
---|
| 228 | |
---|
| 229 | if (!xdr_double (&s.impl (), &v)) |
---|
| 230 | throw xdr_stream_extraction (); |
---|
| 231 | |
---|
| 232 | x.x_ = static_cast<X> (v); |
---|
| 233 | |
---|
| 234 | return s; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | // Extraction of std::basic_string. |
---|
| 238 | // |
---|
| 239 | |
---|
| 240 | inline istream<XDR>& |
---|
| 241 | operator>> (istream<XDR>& s, std::basic_string<char>& x) |
---|
| 242 | { |
---|
| 243 | unsigned int n; |
---|
| 244 | |
---|
| 245 | if (!xdr_u_int (&s.impl (), &n)) |
---|
| 246 | throw xdr_stream_extraction (); |
---|
| 247 | |
---|
| 248 | // Dangerous but fast. |
---|
| 249 | // |
---|
| 250 | x.resize (n); |
---|
| 251 | char* p (const_cast<char*> (x.c_str ())); |
---|
| 252 | |
---|
| 253 | if (!xdr_opaque (&s.impl (), p, n)) |
---|
| 254 | throw xdr_stream_extraction (); |
---|
| 255 | |
---|
| 256 | return s; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | // Wide strings are not supported by XDR. |
---|
| 260 | // |
---|
| 261 | // inline istream<XDR>& |
---|
| 262 | // operator>> (istream<XDR>& s, std::basic_string<wchar_t>& x) |
---|
| 263 | // { |
---|
| 264 | // } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | // Extraction of a binary buffer. |
---|
| 268 | // |
---|
| 269 | template <typename C> |
---|
| 270 | istream<XDR>& |
---|
| 271 | operator>> (istream<XDR>& s, buffer<C>& x) |
---|
| 272 | { |
---|
| 273 | unsigned int n; |
---|
| 274 | |
---|
| 275 | if (!xdr_u_int (&s.impl (), &n)) |
---|
| 276 | throw xdr_stream_extraction (); |
---|
| 277 | |
---|
| 278 | x.size (n); |
---|
| 279 | |
---|
| 280 | if (!xdr_opaque (&s.impl (), x.data (), n)) |
---|
| 281 | throw xdr_stream_extraction (); |
---|
| 282 | |
---|
| 283 | return s; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | #endif // XSD_CXX_TREE_XDR_STREAM_EXTRACTION_HXX |
---|