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