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