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 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 79 | #ifdef HAVE_ENDIAN_H |
| 80 | #include <endian.h> |
| 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 |
| 89 | # error "Cannot determine endianness" |
| 90 | # endif |
| 91 | #endif |
| 92 | |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 93 | #if __BYTE_ORDER == __BIG_ENDIAN |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame^] | 94 | # define ntohll(n) (n) |
| 95 | # define htonll(n) (n) |
| 96 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 97 | # include <byteswap.h> |
| 98 | # define htolell(n) bswap_64(n) |
| 99 | # define letohll(n) bswap_64(n) |
| 100 | # else /* GNUC & GLIBC */ |
| 101 | # define bswap_64(n) \ |
| 102 | ( (((n) & 0xff00000000000000ull) >> 56) \ |
| 103 | | (((n) & 0x00ff000000000000ull) >> 40) \ |
| 104 | | (((n) & 0x0000ff0000000000ull) >> 24) \ |
| 105 | | (((n) & 0x000000ff00000000ull) >> 8) \ |
| 106 | | (((n) & 0x00000000ff000000ull) << 8) \ |
| 107 | | (((n) & 0x0000000000ff0000ull) << 24) \ |
| 108 | | (((n) & 0x000000000000ff00ull) << 40) \ |
| 109 | | (((n) & 0x00000000000000ffull) << 56) ) |
| 110 | # define ntolell(n) bswap_64(n) |
| 111 | # define letonll(n) bswap_64(n) |
| 112 | # endif /* GNUC & GLIBC */ |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 113 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame^] | 114 | # define htolell(n) (n) |
| 115 | # define letohll(n) (n) |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 116 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 117 | # include <byteswap.h> |
| 118 | # define ntohll(n) bswap_64(n) |
| 119 | # define htonll(n) bswap_64(n) |
| 120 | # else /* GNUC & GLIBC */ |
| 121 | # define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) ) |
| 122 | # define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) ) |
| 123 | # endif /* GNUC & GLIBC */ |
| 124 | #else /* __BYTE_ORDER */ |
| 125 | # error "Can't define htonll or ntohll!" |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 126 | #endif |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 127 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 128 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 129 | * Enumerated definition of the types that the Thrift protocol supports. |
| 130 | * Take special note of the T_END type which is used specifically to mark |
| 131 | * the end of a sequence of fields. |
| 132 | */ |
| 133 | enum TType { |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 134 | T_STOP = 0, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 135 | T_VOID = 1, |
| 136 | T_BOOL = 2, |
| 137 | T_BYTE = 3, |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 138 | T_I08 = 3, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 139 | T_I16 = 6, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 140 | T_I32 = 8, |
| 141 | T_U64 = 9, |
| 142 | T_I64 = 10, |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 143 | T_DOUBLE = 4, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 144 | T_STRING = 11, |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 145 | T_UTF7 = 11, |
| 146 | T_STRUCT = 12, |
| 147 | T_MAP = 13, |
| 148 | T_SET = 14, |
| 149 | T_LIST = 15, |
| 150 | T_UTF8 = 16, |
| 151 | T_UTF16 = 17 |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 155 | * Enumerated definition of the message types that the Thrift protocol |
| 156 | * supports. |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 157 | */ |
| 158 | enum TMessageType { |
| 159 | T_CALL = 1, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 160 | T_REPLY = 2, |
David Reiss | deda141 | 2009-04-02 19:22:31 +0000 | [diff] [blame] | 161 | T_EXCEPTION = 3, |
| 162 | T_ONEWAY = 4 |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 166 | * Abstract class for a thrift protocol driver. These are all the methods that |
| 167 | * a protocol must implement. Essentially, there must be some way of reading |
| 168 | * 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] | 169 | * with indexed fields. |
| 170 | * |
| 171 | * TProtocol objects should not be shared across multiple encoding contexts, |
| 172 | * as they may need to maintain internal state in some protocols (i.e. XML). |
| 173 | * Note that is is acceptable for the TProtocol module to do its own internal |
| 174 | * buffered reads/writes to the underlying TTransport where appropriate (i.e. |
| 175 | * when parsing an input XML stream, reading should be batched rather than |
| 176 | * looking ahead character by character for a close tag). |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 178 | */ |
| 179 | class TProtocol { |
| 180 | public: |
| 181 | virtual ~TProtocol() {} |
| 182 | |
| 183 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 184 | * Writing functions. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 185 | */ |
| 186 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 187 | virtual uint32_t writeMessageBegin(const std::string& name, |
| 188 | const TMessageType messageType, |
| 189 | const int32_t seqid) = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 190 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 191 | virtual uint32_t writeMessageEnd() = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 192 | |
| 193 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 194 | virtual uint32_t writeStructBegin(const char* name) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 195 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 196 | virtual uint32_t writeStructEnd() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 197 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 198 | virtual uint32_t writeFieldBegin(const char* name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 199 | const TType fieldType, |
| 200 | const int16_t fieldId) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 201 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 202 | virtual uint32_t writeFieldEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 203 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 204 | virtual uint32_t writeFieldStop() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 205 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 206 | virtual uint32_t writeMapBegin(const TType keyType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 207 | const TType valType, |
| 208 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 209 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 210 | virtual uint32_t writeMapEnd() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 211 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 212 | virtual uint32_t writeListBegin(const TType elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 213 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 214 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 215 | virtual uint32_t writeListEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 216 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 217 | virtual uint32_t writeSetBegin(const TType elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 218 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 219 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 220 | virtual uint32_t writeSetEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 221 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 222 | virtual uint32_t writeBool(const bool value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 223 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 224 | virtual uint32_t writeByte(const int8_t byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 225 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 226 | virtual uint32_t writeI16(const int16_t i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 227 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 228 | virtual uint32_t writeI32(const int32_t i32) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 229 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 230 | virtual uint32_t writeI64(const int64_t i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 231 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 232 | virtual uint32_t writeDouble(const double dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 233 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 234 | virtual uint32_t writeString(const std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 235 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 236 | virtual uint32_t writeBinary(const std::string& str) = 0; |
| 237 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 238 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 239 | * Reading functions |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 240 | */ |
| 241 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 242 | virtual uint32_t readMessageBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 243 | TMessageType& messageType, |
| 244 | int32_t& seqid) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 245 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 246 | virtual uint32_t readMessageEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 247 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 248 | virtual uint32_t readStructBegin(std::string& name) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 249 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 250 | virtual uint32_t readStructEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 251 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 252 | virtual uint32_t readFieldBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 253 | TType& fieldType, |
| 254 | int16_t& fieldId) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 255 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 256 | virtual uint32_t readFieldEnd() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 257 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 258 | virtual uint32_t readMapBegin(TType& keyType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 259 | TType& valType, |
| 260 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 261 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 262 | virtual uint32_t readMapEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 263 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 264 | virtual uint32_t readListBegin(TType& elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 265 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 266 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 267 | virtual uint32_t readListEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 268 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 269 | virtual uint32_t readSetBegin(TType& elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 270 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 271 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 272 | virtual uint32_t readSetEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 273 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 274 | virtual uint32_t readBool(bool& value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 275 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 276 | virtual uint32_t readByte(int8_t& byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 277 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 278 | virtual uint32_t readI16(int16_t& i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 279 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 280 | virtual uint32_t readI32(int32_t& i32) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 281 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 282 | virtual uint32_t readI64(int64_t& i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 283 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 284 | virtual uint32_t readDouble(double& dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 285 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 286 | virtual uint32_t readString(std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 287 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 288 | virtual uint32_t readBinary(std::string& str) = 0; |
| 289 | |
David Reiss | 035aed9 | 2009-02-10 21:38:48 +0000 | [diff] [blame] | 290 | uint32_t readBool(std::vector<bool>::reference ref) { |
| 291 | bool value; |
| 292 | uint32_t rv = readBool(value); |
| 293 | ref = value; |
David Reiss | 57b5006 | 2009-02-25 00:59:55 +0000 | [diff] [blame] | 294 | return rv; |
David Reiss | 035aed9 | 2009-02-10 21:38:48 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 297 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 298 | * Method to arbitrarily skip over data. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 299 | */ |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 300 | uint32_t skip(TType type) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 301 | switch (type) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 302 | case T_BOOL: |
| 303 | { |
| 304 | bool boolv; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 305 | return readBool(boolv); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 306 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 307 | case T_BYTE: |
| 308 | { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 309 | int8_t bytev; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 310 | return readByte(bytev); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 311 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 312 | case T_I16: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 313 | { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 314 | int16_t i16; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 315 | return readI16(i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 316 | } |
| 317 | case T_I32: |
| 318 | { |
| 319 | int32_t i32; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 320 | return readI32(i32); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 321 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 322 | case T_I64: |
| 323 | { |
| 324 | int64_t i64; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 325 | return readI64(i64); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 326 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 327 | case T_DOUBLE: |
| 328 | { |
| 329 | double dub; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 330 | return readDouble(dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 331 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 332 | case T_STRING: |
| 333 | { |
| 334 | std::string str; |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 335 | return readBinary(str); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 336 | } |
| 337 | case T_STRUCT: |
| 338 | { |
| 339 | uint32_t result = 0; |
| 340 | std::string name; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 341 | int16_t fid; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 342 | TType ftype; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 343 | result += readStructBegin(name); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 344 | while (true) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 345 | result += readFieldBegin(name, ftype, fid); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 346 | if (ftype == T_STOP) { |
| 347 | break; |
| 348 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 349 | result += skip(ftype); |
| 350 | result += readFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 351 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 352 | result += readStructEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 353 | return result; |
| 354 | } |
| 355 | case T_MAP: |
| 356 | { |
| 357 | uint32_t result = 0; |
| 358 | TType keyType; |
| 359 | TType valType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 360 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 361 | result += readMapBegin(keyType, valType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 362 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 363 | result += skip(keyType); |
| 364 | result += skip(valType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 365 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 366 | result += readMapEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 367 | return result; |
| 368 | } |
| 369 | case T_SET: |
| 370 | { |
| 371 | uint32_t result = 0; |
| 372 | TType elemType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 373 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 374 | result += readSetBegin(elemType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 375 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 376 | result += skip(elemType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 377 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 378 | result += readSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 379 | return result; |
| 380 | } |
| 381 | case T_LIST: |
| 382 | { |
| 383 | uint32_t result = 0; |
| 384 | TType elemType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 385 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 386 | result += readListBegin(elemType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 387 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 388 | result += skip(elemType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 389 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 390 | result += readListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 391 | return result; |
| 392 | } |
| 393 | default: |
| 394 | return 0; |
| 395 | } |
| 396 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 397 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 398 | inline boost::shared_ptr<TTransport> getTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 399 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 402 | // TODO: remove these two calls, they are for backwards |
| 403 | // compatibility |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 404 | inline boost::shared_ptr<TTransport> getInputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 405 | return ptrans_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 406 | } |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 407 | inline boost::shared_ptr<TTransport> getOutputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 408 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 411 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 412 | TProtocol(boost::shared_ptr<TTransport> ptrans): |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 413 | ptrans_(ptrans) { |
| 414 | trans_ = ptrans.get(); |
| 415 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 416 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 417 | boost::shared_ptr<TTransport> ptrans_; |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 418 | TTransport* trans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 419 | |
| 420 | private: |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 421 | TProtocol() {} |
| 422 | }; |
| 423 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 424 | /** |
| 425 | * Constructs input and output protocol objects given transports. |
| 426 | */ |
| 427 | class TProtocolFactory { |
| 428 | public: |
| 429 | TProtocolFactory() {} |
| 430 | |
| 431 | virtual ~TProtocolFactory() {} |
| 432 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 433 | virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 436 | }}} // apache::thrift::protocol |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 437 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 438 | #endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |