[111] | 1 | // file : xsd/cxx/tree/ace-cdr-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_ACE_CDR_STREAM_EXTRACTION_HXX |
---|
| 7 | #define XSD_CXX_TREE_ACE_CDR_STREAM_EXTRACTION_HXX |
---|
| 8 | |
---|
| 9 | #include <cstddef> // std::size_t |
---|
| 10 | #include <string> |
---|
| 11 | |
---|
| 12 | #include <ace/ACE.h> // ACE::strdelete |
---|
| 13 | #include <ace/CDR_Stream.h> |
---|
| 14 | |
---|
| 15 | #include <xsd/cxx/auto-array.hxx> |
---|
| 16 | |
---|
| 17 | #include <xsd/cxx/tree/buffer.hxx> |
---|
| 18 | #include <xsd/cxx/tree/istream.hxx> |
---|
| 19 | #include <xsd/cxx/tree/ace-cdr-stream-common.hxx> |
---|
| 20 | |
---|
| 21 | namespace xsd |
---|
| 22 | { |
---|
| 23 | namespace cxx |
---|
| 24 | { |
---|
| 25 | namespace tree |
---|
| 26 | { |
---|
| 27 | struct ace_cdr_stream_extraction: ace_cdr_stream_operation |
---|
| 28 | { |
---|
| 29 | virtual const char* |
---|
| 30 | what () const throw () |
---|
| 31 | { |
---|
| 32 | return "ACE CDR stream extraction operation failed"; |
---|
| 33 | } |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | // as_size |
---|
| 38 | // |
---|
| 39 | |
---|
| 40 | #ifdef XSD_CXX_TREE_USE_64_BIT_SIZE |
---|
| 41 | template <typename X> |
---|
| 42 | inline istream<ACE_InputCDR>& |
---|
| 43 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 44 | istream<ACE_InputCDR>::as_size<X>& x) |
---|
| 45 | { |
---|
| 46 | ACE_CDR::ULongLong r; |
---|
| 47 | |
---|
| 48 | if (!s.impl ().read_ulonglong (r) || |
---|
| 49 | r > ~(X (0))) |
---|
| 50 | throw ace_cdr_stream_extraction (); |
---|
| 51 | |
---|
| 52 | x.x_ = static_cast<X> (r); |
---|
| 53 | |
---|
| 54 | return s; |
---|
| 55 | } |
---|
| 56 | #else |
---|
| 57 | template <typename X> |
---|
| 58 | inline istream<ACE_InputCDR>& |
---|
| 59 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 60 | istream<ACE_InputCDR>::as_size<X>& x) |
---|
| 61 | { |
---|
| 62 | ACE_CDR::ULong r; |
---|
| 63 | |
---|
| 64 | if (!s.impl ().read_ulong (r)) |
---|
| 65 | throw ace_cdr_stream_extraction (); |
---|
| 66 | |
---|
| 67 | x.x_ = static_cast<X> (r); |
---|
| 68 | |
---|
| 69 | return s; |
---|
| 70 | } |
---|
| 71 | #endif |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | // 8-bit |
---|
| 75 | // |
---|
| 76 | template <typename X> |
---|
| 77 | inline istream<ACE_InputCDR>& |
---|
| 78 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 79 | istream<ACE_InputCDR>::as_int8<X>& x) |
---|
| 80 | { |
---|
| 81 | ACE_CDR::Octet r; |
---|
| 82 | |
---|
| 83 | if (!s.impl ().read_octet (r)) |
---|
| 84 | throw ace_cdr_stream_extraction (); |
---|
| 85 | |
---|
| 86 | x.x_ = static_cast<X> (r); |
---|
| 87 | |
---|
| 88 | return s; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | template <typename X> |
---|
| 92 | inline istream<ACE_InputCDR>& |
---|
| 93 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 94 | istream<ACE_InputCDR>::as_uint8<X>& x) |
---|
| 95 | { |
---|
| 96 | ACE_CDR::Octet r; |
---|
| 97 | |
---|
| 98 | if (!s.impl ().read_octet (r)) |
---|
| 99 | throw ace_cdr_stream_extraction (); |
---|
| 100 | |
---|
| 101 | x.x_ = static_cast<X> (r); |
---|
| 102 | |
---|
| 103 | return s; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | // 16-bit |
---|
| 108 | // |
---|
| 109 | template <typename X> |
---|
| 110 | inline istream<ACE_InputCDR>& |
---|
| 111 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 112 | istream<ACE_InputCDR>::as_int16<X>& x) |
---|
| 113 | { |
---|
| 114 | ACE_CDR::Short r; |
---|
| 115 | |
---|
| 116 | if (!s.impl ().read_short (r)) |
---|
| 117 | throw ace_cdr_stream_extraction (); |
---|
| 118 | |
---|
| 119 | x.x_ = static_cast<X> (r); |
---|
| 120 | |
---|
| 121 | return s; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | template <typename X> |
---|
| 125 | inline istream<ACE_InputCDR>& |
---|
| 126 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 127 | istream<ACE_InputCDR>::as_uint16<X>& x) |
---|
| 128 | { |
---|
| 129 | ACE_CDR::UShort r; |
---|
| 130 | |
---|
| 131 | if (!s.impl ().read_ushort (r)) |
---|
| 132 | throw ace_cdr_stream_extraction (); |
---|
| 133 | |
---|
| 134 | x.x_ = static_cast<X> (r); |
---|
| 135 | |
---|
| 136 | return s; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | // 32-bit |
---|
| 141 | // |
---|
| 142 | template <typename X> |
---|
| 143 | inline istream<ACE_InputCDR>& |
---|
| 144 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 145 | istream<ACE_InputCDR>::as_int32<X>& x) |
---|
| 146 | { |
---|
| 147 | ACE_CDR::Long r; |
---|
| 148 | |
---|
| 149 | if (!s.impl ().read_long (r)) |
---|
| 150 | throw ace_cdr_stream_extraction (); |
---|
| 151 | |
---|
| 152 | x.x_ = static_cast<X> (r); |
---|
| 153 | |
---|
| 154 | return s; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | template <typename X> |
---|
| 158 | inline istream<ACE_InputCDR>& |
---|
| 159 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 160 | istream<ACE_InputCDR>::as_uint32<X>& x) |
---|
| 161 | { |
---|
| 162 | ACE_CDR::ULong r; |
---|
| 163 | |
---|
| 164 | if (!s.impl ().read_ulong (r)) |
---|
| 165 | throw ace_cdr_stream_extraction (); |
---|
| 166 | |
---|
| 167 | x.x_ = static_cast<X> (r); |
---|
| 168 | |
---|
| 169 | return s; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | // 64-bit |
---|
| 174 | // |
---|
| 175 | template <typename X> |
---|
| 176 | inline istream<ACE_InputCDR>& |
---|
| 177 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 178 | istream<ACE_InputCDR>::as_int64<X>& x) |
---|
| 179 | { |
---|
| 180 | ACE_CDR::LongLong r; |
---|
| 181 | |
---|
| 182 | if (!s.impl ().read_longlong (r)) |
---|
| 183 | throw ace_cdr_stream_extraction (); |
---|
| 184 | |
---|
| 185 | x.x_ = static_cast<X> (r); |
---|
| 186 | |
---|
| 187 | return s; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | template <typename X> |
---|
| 191 | inline istream<ACE_InputCDR>& |
---|
| 192 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 193 | istream<ACE_InputCDR>::as_uint64<X>& x) |
---|
| 194 | { |
---|
| 195 | ACE_CDR::ULongLong r; |
---|
| 196 | |
---|
| 197 | if (!s.impl ().read_ulonglong (r)) |
---|
| 198 | throw ace_cdr_stream_extraction (); |
---|
| 199 | |
---|
| 200 | x.x_ = static_cast<X> (r); |
---|
| 201 | |
---|
| 202 | return s; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | // Boolean |
---|
| 207 | // |
---|
| 208 | template <typename X> |
---|
| 209 | inline istream<ACE_InputCDR>& |
---|
| 210 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 211 | istream<ACE_InputCDR>::as_bool<X>& x) |
---|
| 212 | { |
---|
| 213 | ACE_CDR::Boolean r; |
---|
| 214 | |
---|
| 215 | if (!s.impl ().read_boolean (r)) |
---|
| 216 | throw ace_cdr_stream_extraction (); |
---|
| 217 | |
---|
| 218 | x.x_ = static_cast<X> (r); |
---|
| 219 | |
---|
| 220 | return s; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | // Floating-point |
---|
| 225 | // |
---|
| 226 | template <typename X> |
---|
| 227 | inline istream<ACE_InputCDR>& |
---|
| 228 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 229 | istream<ACE_InputCDR>::as_float32<X>& x) |
---|
| 230 | { |
---|
| 231 | ACE_CDR::Float r; |
---|
| 232 | |
---|
| 233 | if (!s.impl ().read_float (r)) |
---|
| 234 | throw ace_cdr_stream_extraction (); |
---|
| 235 | |
---|
| 236 | x.x_ = static_cast<X> (r); |
---|
| 237 | |
---|
| 238 | return s; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | template <typename X> |
---|
| 242 | inline istream<ACE_InputCDR>& |
---|
| 243 | operator>> (istream<ACE_InputCDR>& s, |
---|
| 244 | istream<ACE_InputCDR>::as_float64<X>& x) |
---|
| 245 | { |
---|
| 246 | ACE_CDR::Double r; |
---|
| 247 | |
---|
| 248 | if (!s.impl ().read_double (r)) |
---|
| 249 | throw ace_cdr_stream_extraction (); |
---|
| 250 | |
---|
| 251 | x.x_ = static_cast<X> (r); |
---|
| 252 | |
---|
| 253 | return s; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | // Extraction of std::basic_string. |
---|
| 257 | // |
---|
| 258 | |
---|
| 259 | namespace bits |
---|
| 260 | { |
---|
| 261 | template<typename C> |
---|
| 262 | struct ace_str_deallocator |
---|
| 263 | { |
---|
| 264 | void |
---|
| 265 | deallocate (C* s) |
---|
| 266 | { |
---|
| 267 | ACE::strdelete (s); |
---|
| 268 | } |
---|
| 269 | }; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | inline istream<ACE_InputCDR>& |
---|
| 273 | operator>> (istream<ACE_InputCDR>& s, std::basic_string<char>& x) |
---|
| 274 | { |
---|
| 275 | typedef bits::ace_str_deallocator<char> deallocator; |
---|
| 276 | |
---|
| 277 | deallocator d; |
---|
| 278 | char* r; |
---|
| 279 | |
---|
| 280 | if (!s.impl ().read_string (r)) |
---|
| 281 | throw ace_cdr_stream_extraction (); |
---|
| 282 | |
---|
| 283 | auto_array<char, deallocator> ar (r, d); |
---|
| 284 | |
---|
| 285 | x = r; |
---|
| 286 | |
---|
| 287 | return s; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | #ifdef ACE_HAS_WCHAR |
---|
| 291 | inline istream<ACE_InputCDR>& |
---|
| 292 | operator>> (istream<ACE_InputCDR>& s, std::basic_string<wchar_t>& x) |
---|
| 293 | { |
---|
| 294 | typedef bits::ace_str_deallocator<wchar_t> deallocator; |
---|
| 295 | |
---|
| 296 | deallocator d; |
---|
| 297 | wchar_t* r; |
---|
| 298 | |
---|
| 299 | if (!s.impl ().read_wstring (r)) |
---|
| 300 | throw ace_cdr_stream_extraction (); |
---|
| 301 | |
---|
| 302 | auto_array<wchar_t, deallocator> ar (r, d); |
---|
| 303 | |
---|
| 304 | x = r; |
---|
| 305 | |
---|
| 306 | return s; |
---|
| 307 | } |
---|
| 308 | #endif |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | // Extraction of a binary buffer. |
---|
| 312 | // |
---|
| 313 | template <typename C> |
---|
| 314 | istream<ACE_InputCDR>& |
---|
| 315 | operator>> (istream<ACE_InputCDR>& s, buffer<C>& x) |
---|
| 316 | { |
---|
| 317 | ACE_CDR::ULong size; |
---|
| 318 | |
---|
| 319 | if (!s.impl ().read_ulong (size)) |
---|
| 320 | throw ace_cdr_stream_extraction (); |
---|
| 321 | |
---|
| 322 | x.size (size); |
---|
| 323 | |
---|
| 324 | if (!s.impl ().read_octet_array ( |
---|
| 325 | reinterpret_cast<ACE_CDR::Octet*> (x.data ()), size)) |
---|
| 326 | throw ace_cdr_stream_extraction (); |
---|
| 327 | |
---|
| 328 | return s; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | #endif // XSD_CXX_TREE_ACE_CDR_STREAM_EXTRACTION_HXX |
---|