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