David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_PROTOCOL_TPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 23 | #include <transport/TTransport.h> |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 24 | #include <protocol/TProtocolException.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | |
| 26 | #include <boost/shared_ptr.hpp> |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 27 | #include <boost/static_assert.hpp> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 28 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 29 | #include <netinet/in.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <string> |
| 32 | #include <map> |
| 33 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 34 | |
| 35 | // Use this to get around strict aliasing rules. |
| 36 | // For example, uint64_t i = bitwise_cast<uint64_t>(returns_double()); |
| 37 | // The most obvious implementation is to just cast a pointer, |
| 38 | // but that doesn't work. |
| 39 | // For a pretty in-depth explanation of the problem, see |
| 40 | // http://www.cellperformance.com/mike_acton/2006/06/ (...) |
| 41 | // understanding_strict_aliasing.html |
| 42 | template <typename To, typename From> |
| 43 | static inline To bitwise_cast(From from) { |
| 44 | BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To)); |
| 45 | |
| 46 | // BAD!!! These are all broken with -O2. |
| 47 | //return *reinterpret_cast<To*>(&from); // BAD!!! |
| 48 | //return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!! |
| 49 | //return *(To*)(void*)&from; // BAD!!! |
| 50 | |
| 51 | // Super clean and paritally blessed by section 3.9 of the standard. |
| 52 | //unsigned char c[sizeof(from)]; |
| 53 | //memcpy(c, &from, sizeof(from)); |
| 54 | //To to; |
| 55 | //memcpy(&to, c, sizeof(c)); |
| 56 | //return to; |
| 57 | |
| 58 | // Slightly more questionable. |
| 59 | // Same code emitted by GCC. |
| 60 | //To to; |
| 61 | //memcpy(&to, &from, sizeof(from)); |
| 62 | //return to; |
| 63 | |
| 64 | // Technically undefined, but almost universally supported, |
| 65 | // and the most efficient implementation. |
| 66 | union { |
| 67 | From f; |
| 68 | To t; |
| 69 | } u; |
| 70 | u.f = from; |
| 71 | return u.t; |
| 72 | } |
| 73 | |
| 74 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 75 | namespace apache { namespace thrift { namespace protocol { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 76 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 77 | using apache::thrift::transport::TTransport; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 78 | |
Bryan Duxbury | 184d262 | 2010-08-17 17:43:58 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SYS_PARAM_H_ |
| 80 | #include <sys/param.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Mark Slee | 1d2ead3 | 2007-06-09 01:23:04 +0000 | [diff] [blame] | 83 | #ifndef __BYTE_ORDER |
| 84 | # if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) |
| 85 | # define __BYTE_ORDER BYTE_ORDER |
| 86 | # define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 87 | # define __BIG_ENDIAN BIG_ENDIAN |
| 88 | # else |
David Reiss | 9b90344 | 2009-10-21 05:51:28 +0000 | [diff] [blame] | 89 | # include <boost/config.hpp> |
| 90 | # define __BYTE_ORDER BOOST_BYTE_ORDER |
| 91 | # define __LITTLE_ENDIAN BOOST_LITTLE_ENDIAN |
| 92 | # define __BIG_ENDIAN BOOST_BIG_ENDIAN |
Mark Slee | 1d2ead3 | 2007-06-09 01:23:04 +0000 | [diff] [blame] | 93 | # endif |
| 94 | #endif |
| 95 | |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 96 | #if __BYTE_ORDER == __BIG_ENDIAN |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 97 | # define ntohll(n) (n) |
| 98 | # define htonll(n) (n) |
| 99 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 100 | # include <byteswap.h> |
| 101 | # define htolell(n) bswap_64(n) |
| 102 | # define letohll(n) bswap_64(n) |
| 103 | # else /* GNUC & GLIBC */ |
| 104 | # define bswap_64(n) \ |
| 105 | ( (((n) & 0xff00000000000000ull) >> 56) \ |
| 106 | | (((n) & 0x00ff000000000000ull) >> 40) \ |
| 107 | | (((n) & 0x0000ff0000000000ull) >> 24) \ |
| 108 | | (((n) & 0x000000ff00000000ull) >> 8) \ |
| 109 | | (((n) & 0x00000000ff000000ull) << 8) \ |
| 110 | | (((n) & 0x0000000000ff0000ull) << 24) \ |
| 111 | | (((n) & 0x000000000000ff00ull) << 40) \ |
| 112 | | (((n) & 0x00000000000000ffull) << 56) ) |
David Reiss | 3029786 | 2009-08-05 16:42:59 +0000 | [diff] [blame] | 113 | # define htolell(n) bswap_64(n) |
| 114 | # define letohll(n) bswap_64(n) |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 115 | # endif /* GNUC & GLIBC */ |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 116 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 117 | # define htolell(n) (n) |
| 118 | # define letohll(n) (n) |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 119 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 120 | # include <byteswap.h> |
| 121 | # define ntohll(n) bswap_64(n) |
| 122 | # define htonll(n) bswap_64(n) |
| 123 | # else /* GNUC & GLIBC */ |
| 124 | # define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) ) |
| 125 | # define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) ) |
| 126 | # endif /* GNUC & GLIBC */ |
| 127 | #else /* __BYTE_ORDER */ |
| 128 | # error "Can't define htonll or ntohll!" |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 129 | #endif |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 130 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 131 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 132 | * Enumerated definition of the types that the Thrift protocol supports. |
| 133 | * Take special note of the T_END type which is used specifically to mark |
| 134 | * the end of a sequence of fields. |
| 135 | */ |
| 136 | enum TType { |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 137 | T_STOP = 0, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 138 | T_VOID = 1, |
| 139 | T_BOOL = 2, |
| 140 | T_BYTE = 3, |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 141 | T_I08 = 3, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 142 | T_I16 = 6, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 143 | T_I32 = 8, |
| 144 | T_U64 = 9, |
| 145 | T_I64 = 10, |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 146 | T_DOUBLE = 4, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 147 | T_STRING = 11, |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 148 | T_UTF7 = 11, |
| 149 | T_STRUCT = 12, |
| 150 | T_MAP = 13, |
| 151 | T_SET = 14, |
| 152 | T_LIST = 15, |
| 153 | T_UTF8 = 16, |
| 154 | T_UTF16 = 17 |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 158 | * Enumerated definition of the message types that the Thrift protocol |
| 159 | * supports. |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 160 | */ |
| 161 | enum TMessageType { |
| 162 | T_CALL = 1, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 163 | T_REPLY = 2, |
David Reiss | deda141 | 2009-04-02 19:22:31 +0000 | [diff] [blame] | 164 | T_EXCEPTION = 3, |
| 165 | T_ONEWAY = 4 |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 168 | |
| 169 | /** |
| 170 | * Helper template for implementing TProtocol::skip(). |
| 171 | * |
| 172 | * Templatized to avoid having to make virtual function calls. |
| 173 | */ |
| 174 | template <class Protocol_> |
| 175 | uint32_t skip(Protocol_& prot, TType type) { |
| 176 | switch (type) { |
| 177 | case T_BOOL: |
| 178 | { |
| 179 | bool boolv; |
| 180 | return prot.readBool(boolv); |
| 181 | } |
| 182 | case T_BYTE: |
| 183 | { |
| 184 | int8_t bytev; |
| 185 | return prot.readByte(bytev); |
| 186 | } |
| 187 | case T_I16: |
| 188 | { |
| 189 | int16_t i16; |
| 190 | return prot.readI16(i16); |
| 191 | } |
| 192 | case T_I32: |
| 193 | { |
| 194 | int32_t i32; |
| 195 | return prot.readI32(i32); |
| 196 | } |
| 197 | case T_I64: |
| 198 | { |
| 199 | int64_t i64; |
| 200 | return prot.readI64(i64); |
| 201 | } |
| 202 | case T_DOUBLE: |
| 203 | { |
| 204 | double dub; |
| 205 | return prot.readDouble(dub); |
| 206 | } |
| 207 | case T_STRING: |
| 208 | { |
| 209 | std::string str; |
| 210 | return prot.readBinary(str); |
| 211 | } |
| 212 | case T_STRUCT: |
| 213 | { |
| 214 | uint32_t result = 0; |
| 215 | std::string name; |
| 216 | int16_t fid; |
| 217 | TType ftype; |
| 218 | result += prot.readStructBegin(name); |
| 219 | while (true) { |
| 220 | result += prot.readFieldBegin(name, ftype, fid); |
| 221 | if (ftype == T_STOP) { |
| 222 | break; |
| 223 | } |
| 224 | result += skip(prot, ftype); |
| 225 | result += prot.readFieldEnd(); |
| 226 | } |
| 227 | result += prot.readStructEnd(); |
| 228 | return result; |
| 229 | } |
| 230 | case T_MAP: |
| 231 | { |
| 232 | uint32_t result = 0; |
| 233 | TType keyType; |
| 234 | TType valType; |
| 235 | uint32_t i, size; |
| 236 | result += prot.readMapBegin(keyType, valType, size); |
| 237 | for (i = 0; i < size; i++) { |
| 238 | result += skip(prot, keyType); |
| 239 | result += skip(prot, valType); |
| 240 | } |
| 241 | result += prot.readMapEnd(); |
| 242 | return result; |
| 243 | } |
| 244 | case T_SET: |
| 245 | { |
| 246 | uint32_t result = 0; |
| 247 | TType elemType; |
| 248 | uint32_t i, size; |
| 249 | result += prot.readSetBegin(elemType, size); |
| 250 | for (i = 0; i < size; i++) { |
| 251 | result += skip(prot, elemType); |
| 252 | } |
| 253 | result += prot.readSetEnd(); |
| 254 | return result; |
| 255 | } |
| 256 | case T_LIST: |
| 257 | { |
| 258 | uint32_t result = 0; |
| 259 | TType elemType; |
| 260 | uint32_t i, size; |
| 261 | result += prot.readListBegin(elemType, size); |
| 262 | for (i = 0; i < size; i++) { |
| 263 | result += skip(prot, elemType); |
| 264 | } |
| 265 | result += prot.readListEnd(); |
| 266 | return result; |
| 267 | } |
| 268 | default: |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 273 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 274 | * Abstract class for a thrift protocol driver. These are all the methods that |
| 275 | * a protocol must implement. Essentially, there must be some way of reading |
| 276 | * and writing all the base types, plus a mechanism for writing out structs |
Mark Slee | 5d06fea | 2007-03-05 22:18:18 +0000 | [diff] [blame] | 277 | * with indexed fields. |
| 278 | * |
| 279 | * TProtocol objects should not be shared across multiple encoding contexts, |
| 280 | * as they may need to maintain internal state in some protocols (i.e. XML). |
| 281 | * Note that is is acceptable for the TProtocol module to do its own internal |
| 282 | * buffered reads/writes to the underlying TTransport where appropriate (i.e. |
| 283 | * when parsing an input XML stream, reading should be batched rather than |
| 284 | * looking ahead character by character for a close tag). |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 285 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 286 | */ |
| 287 | class TProtocol { |
| 288 | public: |
| 289 | virtual ~TProtocol() {} |
| 290 | |
| 291 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 292 | * Writing functions. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 293 | */ |
| 294 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 295 | virtual uint32_t writeMessageBegin_virt(const std::string& name, |
| 296 | const TMessageType messageType, |
| 297 | const int32_t seqid) = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 298 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 299 | virtual uint32_t writeMessageEnd_virt() = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 300 | |
| 301 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 302 | virtual uint32_t writeStructBegin_virt(const char* name) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 303 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 304 | virtual uint32_t writeStructEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 305 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 306 | virtual uint32_t writeFieldBegin_virt(const char* name, |
| 307 | const TType fieldType, |
| 308 | const int16_t fieldId) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 309 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 310 | virtual uint32_t writeFieldEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 311 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 312 | virtual uint32_t writeFieldStop_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 313 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 314 | virtual uint32_t writeMapBegin_virt(const TType keyType, |
| 315 | const TType valType, |
| 316 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 317 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 318 | virtual uint32_t writeMapEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 319 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 320 | virtual uint32_t writeListBegin_virt(const TType elemType, |
| 321 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 322 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 323 | virtual uint32_t writeListEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 324 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 325 | virtual uint32_t writeSetBegin_virt(const TType elemType, |
| 326 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 327 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 328 | virtual uint32_t writeSetEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 329 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 330 | virtual uint32_t writeBool_virt(const bool value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 331 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 332 | virtual uint32_t writeByte_virt(const int8_t byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 333 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 334 | virtual uint32_t writeI16_virt(const int16_t i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 335 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 336 | virtual uint32_t writeI32_virt(const int32_t i32) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 337 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 338 | virtual uint32_t writeI64_virt(const int64_t i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 339 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 340 | virtual uint32_t writeDouble_virt(const double dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 341 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 342 | virtual uint32_t writeString_virt(const std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 343 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 344 | virtual uint32_t writeBinary_virt(const std::string& str) = 0; |
| 345 | |
| 346 | uint32_t writeMessageBegin(const std::string& name, |
| 347 | const TMessageType messageType, |
| 348 | const int32_t seqid) { |
| 349 | T_VIRTUAL_CALL(); |
| 350 | return writeMessageBegin_virt(name, messageType, seqid); |
| 351 | } |
| 352 | |
| 353 | uint32_t writeMessageEnd() { |
| 354 | T_VIRTUAL_CALL(); |
| 355 | return writeMessageEnd_virt(); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | uint32_t writeStructBegin(const char* name) { |
| 360 | T_VIRTUAL_CALL(); |
| 361 | return writeStructBegin_virt(name); |
| 362 | } |
| 363 | |
| 364 | uint32_t writeStructEnd() { |
| 365 | T_VIRTUAL_CALL(); |
| 366 | return writeStructEnd_virt(); |
| 367 | } |
| 368 | |
| 369 | uint32_t writeFieldBegin(const char* name, |
| 370 | const TType fieldType, |
| 371 | const int16_t fieldId) { |
| 372 | T_VIRTUAL_CALL(); |
| 373 | return writeFieldBegin_virt(name, fieldType, fieldId); |
| 374 | } |
| 375 | |
| 376 | uint32_t writeFieldEnd() { |
| 377 | T_VIRTUAL_CALL(); |
| 378 | return writeFieldEnd_virt(); |
| 379 | } |
| 380 | |
| 381 | uint32_t writeFieldStop() { |
| 382 | T_VIRTUAL_CALL(); |
| 383 | return writeFieldStop_virt(); |
| 384 | } |
| 385 | |
| 386 | uint32_t writeMapBegin(const TType keyType, |
| 387 | const TType valType, |
| 388 | const uint32_t size) { |
| 389 | T_VIRTUAL_CALL(); |
| 390 | return writeMapBegin_virt(keyType, valType, size); |
| 391 | } |
| 392 | |
| 393 | uint32_t writeMapEnd() { |
| 394 | T_VIRTUAL_CALL(); |
| 395 | return writeMapEnd_virt(); |
| 396 | } |
| 397 | |
| 398 | uint32_t writeListBegin(const TType elemType, const uint32_t size) { |
| 399 | T_VIRTUAL_CALL(); |
| 400 | return writeListBegin_virt(elemType, size); |
| 401 | } |
| 402 | |
| 403 | uint32_t writeListEnd() { |
| 404 | T_VIRTUAL_CALL(); |
| 405 | return writeListEnd_virt(); |
| 406 | } |
| 407 | |
| 408 | uint32_t writeSetBegin(const TType elemType, const uint32_t size) { |
| 409 | T_VIRTUAL_CALL(); |
| 410 | return writeSetBegin_virt(elemType, size); |
| 411 | } |
| 412 | |
| 413 | uint32_t writeSetEnd() { |
| 414 | T_VIRTUAL_CALL(); |
| 415 | return writeSetEnd_virt(); |
| 416 | } |
| 417 | |
| 418 | uint32_t writeBool(const bool value) { |
| 419 | T_VIRTUAL_CALL(); |
| 420 | return writeBool_virt(value); |
| 421 | } |
| 422 | |
| 423 | uint32_t writeByte(const int8_t byte) { |
| 424 | T_VIRTUAL_CALL(); |
| 425 | return writeByte_virt(byte); |
| 426 | } |
| 427 | |
| 428 | uint32_t writeI16(const int16_t i16) { |
| 429 | T_VIRTUAL_CALL(); |
| 430 | return writeI16_virt(i16); |
| 431 | } |
| 432 | |
| 433 | uint32_t writeI32(const int32_t i32) { |
| 434 | T_VIRTUAL_CALL(); |
| 435 | return writeI32_virt(i32); |
| 436 | } |
| 437 | |
| 438 | uint32_t writeI64(const int64_t i64) { |
| 439 | T_VIRTUAL_CALL(); |
| 440 | return writeI64_virt(i64); |
| 441 | } |
| 442 | |
| 443 | uint32_t writeDouble(const double dub) { |
| 444 | T_VIRTUAL_CALL(); |
| 445 | return writeDouble_virt(dub); |
| 446 | } |
| 447 | |
| 448 | uint32_t writeString(const std::string& str) { |
| 449 | T_VIRTUAL_CALL(); |
| 450 | return writeString_virt(str); |
| 451 | } |
| 452 | |
| 453 | uint32_t writeBinary(const std::string& str) { |
| 454 | T_VIRTUAL_CALL(); |
| 455 | return writeBinary_virt(str); |
| 456 | } |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 457 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 458 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 459 | * Reading functions |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 460 | */ |
| 461 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 462 | virtual uint32_t readMessageBegin_virt(std::string& name, |
| 463 | TMessageType& messageType, |
| 464 | int32_t& seqid) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 465 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 466 | virtual uint32_t readMessageEnd_virt() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 467 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 468 | virtual uint32_t readStructBegin_virt(std::string& name) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 469 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 470 | virtual uint32_t readStructEnd_virt() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 471 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 472 | virtual uint32_t readFieldBegin_virt(std::string& name, |
| 473 | TType& fieldType, |
| 474 | int16_t& fieldId) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 475 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 476 | virtual uint32_t readFieldEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 477 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 478 | virtual uint32_t readMapBegin_virt(TType& keyType, |
| 479 | TType& valType, |
| 480 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 481 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 482 | virtual uint32_t readMapEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 483 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 484 | virtual uint32_t readListBegin_virt(TType& elemType, |
| 485 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 486 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 487 | virtual uint32_t readListEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 488 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 489 | virtual uint32_t readSetBegin_virt(TType& elemType, |
| 490 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 491 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 492 | virtual uint32_t readSetEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 493 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 494 | virtual uint32_t readBool_virt(bool& value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 495 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 496 | virtual uint32_t readByte_virt(int8_t& byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 497 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 498 | virtual uint32_t readI16_virt(int16_t& i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 499 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 500 | virtual uint32_t readI32_virt(int32_t& i32) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 501 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 502 | virtual uint32_t readI64_virt(int64_t& i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 503 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 504 | virtual uint32_t readDouble_virt(double& dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 505 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 506 | virtual uint32_t readString_virt(std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 507 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 508 | virtual uint32_t readBinary_virt(std::string& str) = 0; |
| 509 | |
| 510 | uint32_t readMessageBegin(std::string& name, |
| 511 | TMessageType& messageType, |
| 512 | int32_t& seqid) { |
| 513 | T_VIRTUAL_CALL(); |
| 514 | return readMessageBegin_virt(name, messageType, seqid); |
| 515 | } |
| 516 | |
| 517 | uint32_t readMessageEnd() { |
| 518 | T_VIRTUAL_CALL(); |
| 519 | return readMessageEnd_virt(); |
| 520 | } |
| 521 | |
| 522 | uint32_t readStructBegin(std::string& name) { |
| 523 | T_VIRTUAL_CALL(); |
| 524 | return readStructBegin_virt(name); |
| 525 | } |
| 526 | |
| 527 | uint32_t readStructEnd() { |
| 528 | T_VIRTUAL_CALL(); |
| 529 | return readStructEnd_virt(); |
| 530 | } |
| 531 | |
| 532 | uint32_t readFieldBegin(std::string& name, |
| 533 | TType& fieldType, |
| 534 | int16_t& fieldId) { |
| 535 | T_VIRTUAL_CALL(); |
| 536 | return readFieldBegin_virt(name, fieldType, fieldId); |
| 537 | } |
| 538 | |
| 539 | uint32_t readFieldEnd() { |
| 540 | T_VIRTUAL_CALL(); |
| 541 | return readFieldEnd_virt(); |
| 542 | } |
| 543 | |
| 544 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size) { |
| 545 | T_VIRTUAL_CALL(); |
| 546 | return readMapBegin_virt(keyType, valType, size); |
| 547 | } |
| 548 | |
| 549 | uint32_t readMapEnd() { |
| 550 | T_VIRTUAL_CALL(); |
| 551 | return readMapEnd_virt(); |
| 552 | } |
| 553 | |
| 554 | uint32_t readListBegin(TType& elemType, uint32_t& size) { |
| 555 | T_VIRTUAL_CALL(); |
| 556 | return readListBegin_virt(elemType, size); |
| 557 | } |
| 558 | |
| 559 | uint32_t readListEnd() { |
| 560 | T_VIRTUAL_CALL(); |
| 561 | return readListEnd_virt(); |
| 562 | } |
| 563 | |
| 564 | uint32_t readSetBegin(TType& elemType, uint32_t& size) { |
| 565 | T_VIRTUAL_CALL(); |
| 566 | return readSetBegin_virt(elemType, size); |
| 567 | } |
| 568 | |
| 569 | uint32_t readSetEnd() { |
| 570 | T_VIRTUAL_CALL(); |
| 571 | return readSetEnd_virt(); |
| 572 | } |
| 573 | |
| 574 | uint32_t readBool(bool& value) { |
| 575 | T_VIRTUAL_CALL(); |
| 576 | return readBool_virt(value); |
| 577 | } |
| 578 | |
| 579 | uint32_t readByte(int8_t& byte) { |
| 580 | T_VIRTUAL_CALL(); |
| 581 | return readByte_virt(byte); |
| 582 | } |
| 583 | |
| 584 | uint32_t readI16(int16_t& i16) { |
| 585 | T_VIRTUAL_CALL(); |
| 586 | return readI16_virt(i16); |
| 587 | } |
| 588 | |
| 589 | uint32_t readI32(int32_t& i32) { |
| 590 | T_VIRTUAL_CALL(); |
| 591 | return readI32_virt(i32); |
| 592 | } |
| 593 | |
| 594 | uint32_t readI64(int64_t& i64) { |
| 595 | T_VIRTUAL_CALL(); |
| 596 | return readI64_virt(i64); |
| 597 | } |
| 598 | |
| 599 | uint32_t readDouble(double& dub) { |
| 600 | T_VIRTUAL_CALL(); |
| 601 | return readDouble_virt(dub); |
| 602 | } |
| 603 | |
| 604 | uint32_t readString(std::string& str) { |
| 605 | T_VIRTUAL_CALL(); |
| 606 | return readString_virt(str); |
| 607 | } |
| 608 | |
| 609 | uint32_t readBinary(std::string& str) { |
| 610 | T_VIRTUAL_CALL(); |
| 611 | return readBinary_virt(str); |
| 612 | } |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 613 | |
David Reiss | 035aed9 | 2009-02-10 21:38:48 +0000 | [diff] [blame] | 614 | uint32_t readBool(std::vector<bool>::reference ref) { |
| 615 | bool value; |
| 616 | uint32_t rv = readBool(value); |
| 617 | ref = value; |
David Reiss | 57b5006 | 2009-02-25 00:59:55 +0000 | [diff] [blame] | 618 | return rv; |
David Reiss | 035aed9 | 2009-02-10 21:38:48 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 621 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 622 | * Method to arbitrarily skip over data. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 623 | */ |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 624 | uint32_t skip(TType type) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame^] | 625 | T_VIRTUAL_CALL(); |
| 626 | return skip_virt(type); |
| 627 | } |
| 628 | virtual uint32_t skip_virt(TType type) { |
| 629 | return ::apache::thrift::protocol::skip(*this, type); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 630 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 631 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 632 | inline boost::shared_ptr<TTransport> getTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 633 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 636 | // TODO: remove these two calls, they are for backwards |
| 637 | // compatibility |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 638 | inline boost::shared_ptr<TTransport> getInputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 639 | return ptrans_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 640 | } |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 641 | inline boost::shared_ptr<TTransport> getOutputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 642 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 645 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 646 | TProtocol(boost::shared_ptr<TTransport> ptrans): |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 647 | ptrans_(ptrans) { |
| 648 | trans_ = ptrans.get(); |
| 649 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 650 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 651 | boost::shared_ptr<TTransport> ptrans_; |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 652 | TTransport* trans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 653 | |
| 654 | private: |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 655 | TProtocol() {} |
| 656 | }; |
| 657 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 658 | /** |
| 659 | * Constructs input and output protocol objects given transports. |
| 660 | */ |
| 661 | class TProtocolFactory { |
| 662 | public: |
| 663 | TProtocolFactory() {} |
| 664 | |
| 665 | virtual ~TProtocolFactory() {} |
| 666 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 667 | virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 670 | }}} // apache::thrift::protocol |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 671 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 672 | #endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |