blob: e72033ac2032a07ff16bff468b9d297ef9826051 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_PROTOCOL_TPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/transport/TTransport.h>
24#include <thrift/protocol/TProtocolException.h>
Marc Slemko16698852006-08-04 03:16:10 +000025
26#include <boost/shared_ptr.hpp>
David Reisse4d4ea02009-04-02 21:37:17 +000027#include <boost/static_assert.hpp>
Marc Slemko16698852006-08-04 03:16:10 +000028
Roger Meier2fa9c312011-09-05 19:15:53 +000029#ifdef HAVE_NETINET_IN_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000030#include <netinet/in.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000031#endif
Mark Sleee8540632006-05-30 09:24:40 +000032#include <sys/types.h>
33#include <string>
34#include <map>
David Reiss8dfc7322010-10-06 17:09:58 +000035#include <vector>
Mark Sleee8540632006-05-30 09:24:40 +000036
David Reisse4d4ea02009-04-02 21:37:17 +000037
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
45template <typename To, typename From>
46static 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 Farrellc1fe30b2011-06-01 20:34:26 +000078#ifdef HAVE_SYS_PARAM_H
Bryan Duxbury184d2622010-08-17 17:43:58 +000079#include <sys/param.h>
Mark Slee4f261c52007-04-13 00:33:24 +000080#endif
81
Roger Meierf9f841d2012-06-19 20:42:33 +000082#ifndef __THRIFT_BYTE_ORDER
Mark Slee1d2ead32007-06-09 01:23:04 +000083# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
Roger Meierf9f841d2012-06-19 20:42:33 +000084# define __THRIFT_BYTE_ORDER BYTE_ORDER
85# define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN
86# define __THRIFT_BIG_ENDIAN BIG_ENDIAN
Mark Slee1d2ead32007-06-09 01:23:04 +000087# else
David Reiss9b903442009-10-21 05:51:28 +000088# include <boost/config.hpp>
Roger Meiera93848b2011-09-12 22:20:11 +000089# include <boost/detail/endian.hpp>
Roger Meierf9f841d2012-06-19 20:42:33 +000090# define __THRIFT_BYTE_ORDER BOOST_BYTE_ORDER
Roger Meiera93848b2011-09-12 22:20:11 +000091# ifdef BOOST_LITTLE_ENDIAN
Roger Meierf9f841d2012-06-19 20:42:33 +000092# define __THRIFT_LITTLE_ENDIAN __THRIFT_BYTE_ORDER
93# define __THRIFT_BIG_ENDIAN 0
Roger Meiera93848b2011-09-12 22:20:11 +000094# else
Roger Meierf9f841d2012-06-19 20:42:33 +000095# define __THRIFT_LITTLE_ENDIAN 0
96# define __THRIFT_BIG_ENDIAN __THRIFT_BYTE_ORDER
Roger Meiera93848b2011-09-12 22:20:11 +000097# endif
Mark Slee1d2ead32007-06-09 01:23:04 +000098# endif
99#endif
100
Roger Meierf9f841d2012-06-19 20:42:33 +0000101#if __THRIFT_BYTE_ORDER == __THRIFT_BIG_ENDIAN
David Reisse4d4ea02009-04-02 21:37:17 +0000102# 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 Reiss30297862009-08-05 16:42:59 +0000118# define htolell(n) bswap_64(n)
119# define letohll(n) bswap_64(n)
David Reisse4d4ea02009-04-02 21:37:17 +0000120# endif /* GNUC & GLIBC */
Roger Meierf9f841d2012-06-19 20:42:33 +0000121#elif __THRIFT_BYTE_ORDER == __THRIFT_LITTLE_ENDIAN
David Reisse4d4ea02009-04-02 21:37:17 +0000122# define htolell(n) (n)
123# define letohll(n) (n)
Mark Slee4f261c52007-04-13 00:33:24 +0000124# if defined(__GNUC__) && defined(__GLIBC__)
125# include <byteswap.h>
126# define ntohll(n) bswap_64(n)
127# define htonll(n) bswap_64(n)
Roger Meierb69d24d2012-10-04 18:02:15 +0000128# 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 Meier82525772012-11-16 00:38:27 +0000132# 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 Meierb69d24d2012-10-04 18:02:15 +0000134# endif /* GNUC/GLIBC or MSVC or something else */
Roger Meierf9f841d2012-06-19 20:42:33 +0000135#else /* __THRIFT_BYTE_ORDER */
Mark Slee4f261c52007-04-13 00:33:24 +0000136# error "Can't define htonll or ntohll!"
Mark Sleedc8a2a22006-09-19 22:20:18 +0000137#endif
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138
Ben Craigf4e6e622013-11-05 19:49:12 -0600139namespace apache { namespace thrift { namespace protocol {
140
141using apache::thrift::transport::TTransport;
142
Mark Sleee8540632006-05-30 09:24:40 +0000143/**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000144 * 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 */
148enum TType {
Marc Slemkod42a2c22006-08-10 03:30:18 +0000149 T_STOP = 0,
Marc Slemko5b126d62006-08-11 23:03:42 +0000150 T_VOID = 1,
151 T_BOOL = 2,
152 T_BYTE = 3,
Mark Sleecfc01932006-09-01 22:18:16 +0000153 T_I08 = 3,
Marc Slemko5b126d62006-08-11 23:03:42 +0000154 T_I16 = 6,
Marc Slemko5b126d62006-08-11 23:03:42 +0000155 T_I32 = 8,
156 T_U64 = 9,
157 T_I64 = 10,
Mark Sleec98d0502006-09-06 02:42:25 +0000158 T_DOUBLE = 4,
Marc Slemko5b126d62006-08-11 23:03:42 +0000159 T_STRING = 11,
Marc Slemkod97eb612006-08-24 23:37:36 +0000160 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 Slee8d7e1f62006-06-07 06:48:56 +0000167};
168
169/**
Mark Sleef5f2be42006-09-05 21:05:31 +0000170 * Enumerated definition of the message types that the Thrift protocol
171 * supports.
Marc Slemko16698852006-08-04 03:16:10 +0000172 */
173enum TMessageType {
174 T_CALL = 1,
Mark Sleef9831082007-02-20 20:59:21 +0000175 T_REPLY = 2,
David Reissdeda1412009-04-02 19:22:31 +0000176 T_EXCEPTION = 3,
177 T_ONEWAY = 4
Marc Slemko16698852006-08-04 03:16:10 +0000178};
179
David Reiss6806fb82010-10-06 17:09:52 +0000180
181/**
182 * Helper template for implementing TProtocol::skip().
183 *
184 * Templatized to avoid having to make virtual function calls.
185 */
186template <class Protocol_>
187uint32_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 Meierc0b5d902010-11-30 20:23:44 +0000280 case T_STOP: case T_VOID: case T_U64: case T_UTF8: case T_UTF16:
281 break;
David Reiss6806fb82010-10-06 17:09:52 +0000282 }
Roger Meierc0b5d902010-11-30 20:23:44 +0000283 return 0;
David Reiss6806fb82010-10-06 17:09:52 +0000284}
285
Marc Slemko16698852006-08-04 03:16:10 +0000286/**
Mark Sleee8540632006-05-30 09:24:40 +0000287 * 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 Slee5d06fea2007-03-05 22:18:18 +0000290 * 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 Sleee8540632006-05-30 09:24:40 +0000298 *
Mark Sleee8540632006-05-30 09:24:40 +0000299 */
300class TProtocol {
301 public:
302 virtual ~TProtocol() {}
303
304 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000305 * Writing functions.
Mark Sleee8540632006-05-30 09:24:40 +0000306 */
307
David Reiss6806fb82010-10-06 17:09:52 +0000308 virtual uint32_t writeMessageBegin_virt(const std::string& name,
309 const TMessageType messageType,
310 const int32_t seqid) = 0;
Marc Slemko16698852006-08-04 03:16:10 +0000311
David Reiss6806fb82010-10-06 17:09:52 +0000312 virtual uint32_t writeMessageEnd_virt() = 0;
Marc Slemko16698852006-08-04 03:16:10 +0000313
314
David Reiss6806fb82010-10-06 17:09:52 +0000315 virtual uint32_t writeStructBegin_virt(const char* name) = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000316
David Reiss6806fb82010-10-06 17:09:52 +0000317 virtual uint32_t writeStructEnd_virt() = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000318
David Reiss6806fb82010-10-06 17:09:52 +0000319 virtual uint32_t writeFieldBegin_virt(const char* name,
320 const TType fieldType,
321 const int16_t fieldId) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000322
David Reiss6806fb82010-10-06 17:09:52 +0000323 virtual uint32_t writeFieldEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000324
David Reiss6806fb82010-10-06 17:09:52 +0000325 virtual uint32_t writeFieldStop_virt() = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000326
David Reiss6806fb82010-10-06 17:09:52 +0000327 virtual uint32_t writeMapBegin_virt(const TType keyType,
328 const TType valType,
329 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000330
David Reiss6806fb82010-10-06 17:09:52 +0000331 virtual uint32_t writeMapEnd_virt() = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000332
David Reiss6806fb82010-10-06 17:09:52 +0000333 virtual uint32_t writeListBegin_virt(const TType elemType,
334 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000335
David Reiss6806fb82010-10-06 17:09:52 +0000336 virtual uint32_t writeListEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000337
David Reiss6806fb82010-10-06 17:09:52 +0000338 virtual uint32_t writeSetBegin_virt(const TType elemType,
339 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000340
David Reiss6806fb82010-10-06 17:09:52 +0000341 virtual uint32_t writeSetEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000342
David Reiss6806fb82010-10-06 17:09:52 +0000343 virtual uint32_t writeBool_virt(const bool value) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000344
David Reiss6806fb82010-10-06 17:09:52 +0000345 virtual uint32_t writeByte_virt(const int8_t byte) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000346
David Reiss6806fb82010-10-06 17:09:52 +0000347 virtual uint32_t writeI16_virt(const int16_t i16) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000348
David Reiss6806fb82010-10-06 17:09:52 +0000349 virtual uint32_t writeI32_virt(const int32_t i32) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000350
David Reiss6806fb82010-10-06 17:09:52 +0000351 virtual uint32_t writeI64_virt(const int64_t i64) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000352
David Reiss6806fb82010-10-06 17:09:52 +0000353 virtual uint32_t writeDouble_virt(const double dub) = 0;
Mark Sleec98d0502006-09-06 02:42:25 +0000354
David Reiss6806fb82010-10-06 17:09:52 +0000355 virtual uint32_t writeString_virt(const std::string& str) = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000356
David Reiss6806fb82010-10-06 17:09:52 +0000357 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 Reissc005b1b2008-02-15 01:38:18 +0000470
Mark Sleee8540632006-05-30 09:24:40 +0000471 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000472 * Reading functions
Mark Sleee8540632006-05-30 09:24:40 +0000473 */
474
David Reiss6806fb82010-10-06 17:09:52 +0000475 virtual uint32_t readMessageBegin_virt(std::string& name,
476 TMessageType& messageType,
477 int32_t& seqid) = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000478
David Reiss6806fb82010-10-06 17:09:52 +0000479 virtual uint32_t readMessageEnd_virt() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000480
David Reiss6806fb82010-10-06 17:09:52 +0000481 virtual uint32_t readStructBegin_virt(std::string& name) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000482
David Reiss6806fb82010-10-06 17:09:52 +0000483 virtual uint32_t readStructEnd_virt() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000484
David Reiss6806fb82010-10-06 17:09:52 +0000485 virtual uint32_t readFieldBegin_virt(std::string& name,
486 TType& fieldType,
487 int16_t& fieldId) = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000488
David Reiss6806fb82010-10-06 17:09:52 +0000489 virtual uint32_t readFieldEnd_virt() = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000490
David Reiss6806fb82010-10-06 17:09:52 +0000491 virtual uint32_t readMapBegin_virt(TType& keyType,
492 TType& valType,
493 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000494
David Reiss6806fb82010-10-06 17:09:52 +0000495 virtual uint32_t readMapEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000496
David Reiss6806fb82010-10-06 17:09:52 +0000497 virtual uint32_t readListBegin_virt(TType& elemType,
498 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000499
David Reiss6806fb82010-10-06 17:09:52 +0000500 virtual uint32_t readListEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000501
David Reiss6806fb82010-10-06 17:09:52 +0000502 virtual uint32_t readSetBegin_virt(TType& elemType,
503 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000504
David Reiss6806fb82010-10-06 17:09:52 +0000505 virtual uint32_t readSetEnd_virt() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000506
David Reiss6806fb82010-10-06 17:09:52 +0000507 virtual uint32_t readBool_virt(bool& value) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000508
David Reiss8dfc7322010-10-06 17:09:58 +0000509 virtual uint32_t readBool_virt(std::vector<bool>::reference value) = 0;
510
David Reiss6806fb82010-10-06 17:09:52 +0000511 virtual uint32_t readByte_virt(int8_t& byte) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000512
David Reiss6806fb82010-10-06 17:09:52 +0000513 virtual uint32_t readI16_virt(int16_t& i16) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000514
David Reiss6806fb82010-10-06 17:09:52 +0000515 virtual uint32_t readI32_virt(int32_t& i32) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000516
David Reiss6806fb82010-10-06 17:09:52 +0000517 virtual uint32_t readI64_virt(int64_t& i64) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000518
David Reiss6806fb82010-10-06 17:09:52 +0000519 virtual uint32_t readDouble_virt(double& dub) = 0;
Mark Sleec98d0502006-09-06 02:42:25 +0000520
David Reiss6806fb82010-10-06 17:09:52 +0000521 virtual uint32_t readString_virt(std::string& str) = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000522
David Reiss6806fb82010-10-06 17:09:52 +0000523 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 Reissc005b1b2008-02-15 01:38:18 +0000628
David Reiss8dfc7322010-10-06 17:09:58 +0000629 /*
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 Reiss035aed92009-02-10 21:38:48 +0000637 }
638
Mark Sleee8540632006-05-30 09:24:40 +0000639 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000640 * Method to arbitrarily skip over data.
Mark Sleee8540632006-05-30 09:24:40 +0000641 */
Mark Slee4af6ed72006-10-25 19:02:49 +0000642 uint32_t skip(TType type) {
David Reiss6806fb82010-10-06 17:09:52 +0000643 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 Slee8d7e1f62006-06-07 06:48:56 +0000648 }
Mark Sleee8540632006-05-30 09:24:40 +0000649
Mark Slee5ea15f92007-03-05 22:55:59 +0000650 inline boost::shared_ptr<TTransport> getTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000651 return ptrans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000652 }
653
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000654 // TODO: remove these two calls, they are for backwards
655 // compatibility
Mark Slee5ea15f92007-03-05 22:55:59 +0000656 inline boost::shared_ptr<TTransport> getInputTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000657 return ptrans_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000658 }
Mark Slee5ea15f92007-03-05 22:55:59 +0000659 inline boost::shared_ptr<TTransport> getOutputTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000660 return ptrans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000661 }
662
Mark Sleee8540632006-05-30 09:24:40 +0000663 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000664 TProtocol(boost::shared_ptr<TTransport> ptrans):
Mark Slee43b6c632007-02-07 00:54:17 +0000665 ptrans_(ptrans) {
Mark Slee43b6c632007-02-07 00:54:17 +0000666 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000667
Mark Slee5ea15f92007-03-05 22:55:59 +0000668 boost::shared_ptr<TTransport> ptrans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000669
670 private:
Mark Sleee8540632006-05-30 09:24:40 +0000671 TProtocol() {}
672};
673
Mark Slee4af6ed72006-10-25 19:02:49 +0000674/**
675 * Constructs input and output protocol objects given transports.
676 */
677class TProtocolFactory {
678 public:
679 TProtocolFactory() {}
680
681 virtual ~TProtocolFactory() {}
682
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000683 virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0;
Mark Slee4af6ed72006-10-25 19:02:49 +0000684};
685
David Reissb7762a02010-10-06 17:10:00 +0000686/**
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 */
692class TDummyProtocol : public TProtocol {
693};
694
T Jake Lucianib5e62212009-01-31 22:36:20 +0000695}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000696
Mark Sleef5f2be42006-09-05 21:05:31 +0000697#endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1