blob: 6c7f519794668c6c670070188448d4562cf9da07 [file] [log] [blame]
David Reiss6806fb82010-10-06 17:09:52 +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 */
19
20#ifndef _THRIFT_PROTOCOL_TVIRTUALPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TVIRTUALPROTOCOL_H_ 1
22
23#include <protocol/TProtocol.h>
24
25namespace apache { namespace thrift { namespace protocol {
26
27using apache::thrift::transport::TTransport;
28
29/**
30 * Helper class that provides default implementations of TProtocol methods.
31 *
32 * This class provides default implementations of the non-virtual TProtocol
33 * methods. It exists primarily so TVirtualProtocol can derive from it. It
34 * prevents TVirtualProtocol methods from causing infinite recursion if the
35 * non-virtual methods are not overridden by the TVirtualProtocol subclass.
36 *
37 * You probably don't want to use this class directly. Use TVirtualProtocol
38 * instead.
39 */
40class TProtocolDefaults : public TProtocol {
41 public:
42 uint32_t readMessageBegin(std::string& name,
43 TMessageType& messageType,
44 int32_t& seqid) {
Roger Meier3b771a12010-11-17 22:11:26 +000045 (void) name;
46 (void) messageType;
47 (void) seqid;
David Reiss6806fb82010-10-06 17:09:52 +000048 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
49 "this protocol does not support reading (yet).");
50 }
51
52 uint32_t readMessageEnd() {
53 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
54 "this protocol does not support reading (yet).");
55 }
56
57 uint32_t readStructBegin(std::string& name) {
Roger Meier3b771a12010-11-17 22:11:26 +000058 (void) name;
David Reiss6806fb82010-10-06 17:09:52 +000059 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
60 "this protocol does not support reading (yet).");
61 }
62
63 uint32_t readStructEnd() {
64 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
65 "this protocol does not support reading (yet).");
66 }
67
68 uint32_t readFieldBegin(std::string& name,
69 TType& fieldType,
70 int16_t& fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +000071 (void) name;
72 (void) fieldType;
73 (void) fieldId;
David Reiss6806fb82010-10-06 17:09:52 +000074 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
75 "this protocol does not support reading (yet).");
76 }
77
78 uint32_t readFieldEnd() {
79 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
80 "this protocol does not support reading (yet).");
81 }
82
83 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size) {
Roger Meier3b771a12010-11-17 22:11:26 +000084 (void) keyType;
85 (void) valType;
86 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +000087 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
88 "this protocol does not support reading (yet).");
89 }
90
91 uint32_t readMapEnd() {
92 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
93 "this protocol does not support reading (yet).");
94 }
95
96 uint32_t readListBegin(TType& elemType, uint32_t& size) {
Roger Meier3b771a12010-11-17 22:11:26 +000097 (void) elemType;
98 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +000099 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
100 "this protocol does not support reading (yet).");
101 }
102
103 uint32_t readListEnd() {
104 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
105 "this protocol does not support reading (yet).");
106 }
107
108 uint32_t readSetBegin(TType& elemType, uint32_t& size) {
Roger Meier3b771a12010-11-17 22:11:26 +0000109 (void) elemType;
110 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +0000111 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
112 "this protocol does not support reading (yet).");
113 }
114
115 uint32_t readSetEnd() {
116 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
117 "this protocol does not support reading (yet).");
118 }
119
120 uint32_t readBool(bool& value) {
Roger Meier3b771a12010-11-17 22:11:26 +0000121 (void) value;
David Reiss6806fb82010-10-06 17:09:52 +0000122 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
123 "this protocol does not support reading (yet).");
124 }
125
David Reiss8dfc7322010-10-06 17:09:58 +0000126 uint32_t readBool(std::vector<bool>::reference value) {
Roger Meier3b771a12010-11-17 22:11:26 +0000127 (void) value;
David Reiss8dfc7322010-10-06 17:09:58 +0000128 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
129 "this protocol does not support reading (yet).");
130 }
131
David Reiss6806fb82010-10-06 17:09:52 +0000132 uint32_t readByte(int8_t& byte) {
Roger Meier3b771a12010-11-17 22:11:26 +0000133 (void) byte;
David Reiss6806fb82010-10-06 17:09:52 +0000134 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
135 "this protocol does not support reading (yet).");
136 }
137
138 uint32_t readI16(int16_t& i16) {
Roger Meier3b771a12010-11-17 22:11:26 +0000139 (void) i16;
David Reiss6806fb82010-10-06 17:09:52 +0000140 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
141 "this protocol does not support reading (yet).");
142 }
143
144 uint32_t readI32(int32_t& i32) {
Roger Meier3b771a12010-11-17 22:11:26 +0000145 (void) i32;
David Reiss6806fb82010-10-06 17:09:52 +0000146 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
147 "this protocol does not support reading (yet).");
148 }
149
150 uint32_t readI64(int64_t& i64) {
Roger Meier3b771a12010-11-17 22:11:26 +0000151 (void) i64;
David Reiss6806fb82010-10-06 17:09:52 +0000152 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
153 "this protocol does not support reading (yet).");
154 }
155
156 uint32_t readDouble(double& dub) {
Roger Meier3b771a12010-11-17 22:11:26 +0000157 (void) dub;
David Reiss6806fb82010-10-06 17:09:52 +0000158 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
159 "this protocol does not support reading (yet).");
160 }
161
162 uint32_t readString(std::string& str) {
Roger Meier3b771a12010-11-17 22:11:26 +0000163 (void) str;
David Reiss6806fb82010-10-06 17:09:52 +0000164 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
165 "this protocol does not support reading (yet).");
166 }
167
168 uint32_t readBinary(std::string& str) {
Roger Meier3b771a12010-11-17 22:11:26 +0000169 (void) str;
David Reiss6806fb82010-10-06 17:09:52 +0000170 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
171 "this protocol does not support reading (yet).");
172 }
173
174 uint32_t writeMessageBegin(const std::string& name,
175 const TMessageType messageType,
176 const int32_t seqid) {
Roger Meier3b771a12010-11-17 22:11:26 +0000177 (void) name;
178 (void) messageType;
179 (void) seqid;
David Reiss6806fb82010-10-06 17:09:52 +0000180 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
181 "this protocol does not support writing (yet).");
182 }
183
184 uint32_t writeMessageEnd() {
185 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
186 "this protocol does not support writing (yet).");
187 }
188
189
190 uint32_t writeStructBegin(const char* name) {
Roger Meier3b771a12010-11-17 22:11:26 +0000191 (void) name;
David Reiss6806fb82010-10-06 17:09:52 +0000192 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
193 "this protocol does not support writing (yet).");
194 }
195
196 uint32_t writeStructEnd() {
197 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
198 "this protocol does not support writing (yet).");
199 }
200
201 uint32_t writeFieldBegin(const char* name,
202 const TType fieldType,
203 const int16_t fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +0000204 (void) name;
205 (void) fieldType;
206 (void) fieldId;
David Reiss6806fb82010-10-06 17:09:52 +0000207 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
208 "this protocol does not support writing (yet).");
209 }
210
211 uint32_t writeFieldEnd() {
212 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
213 "this protocol does not support writing (yet).");
214 }
215
216 uint32_t writeFieldStop() {
217 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
218 "this protocol does not support writing (yet).");
219 }
220
221 uint32_t writeMapBegin(const TType keyType,
222 const TType valType,
223 const uint32_t size) {
Roger Meier3b771a12010-11-17 22:11:26 +0000224 (void) keyType;
225 (void) valType;
226 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +0000227 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
228 "this protocol does not support writing (yet).");
229 }
230
231 uint32_t writeMapEnd() {
232 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
233 "this protocol does not support writing (yet).");
234 }
235
236 uint32_t writeListBegin(const TType elemType, const uint32_t size) {
Roger Meier3b771a12010-11-17 22:11:26 +0000237 (void) elemType;
238 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +0000239 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
240 "this protocol does not support writing (yet).");
241 }
242
243 uint32_t writeListEnd() {
244 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
245 "this protocol does not support writing (yet).");
246 }
247
248 uint32_t writeSetBegin(const TType elemType, const uint32_t size) {
Roger Meier3b771a12010-11-17 22:11:26 +0000249 (void) elemType;
250 (void) size;
David Reiss6806fb82010-10-06 17:09:52 +0000251 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
252 "this protocol does not support writing (yet).");
253 }
254
255 uint32_t writeSetEnd() {
256 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
257 "this protocol does not support writing (yet).");
258 }
259
260 uint32_t writeBool(const bool value) {
Roger Meier3b771a12010-11-17 22:11:26 +0000261 (void) value;
David Reiss6806fb82010-10-06 17:09:52 +0000262 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
263 "this protocol does not support writing (yet).");
264 }
265
266 uint32_t writeByte(const int8_t byte) {
Roger Meier3b771a12010-11-17 22:11:26 +0000267 (void) byte;
David Reiss6806fb82010-10-06 17:09:52 +0000268 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
269 "this protocol does not support writing (yet).");
270 }
271
272 uint32_t writeI16(const int16_t i16) {
Roger Meier3b771a12010-11-17 22:11:26 +0000273 (void) i16;
David Reiss6806fb82010-10-06 17:09:52 +0000274 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
275 "this protocol does not support writing (yet).");
276 }
277
278 uint32_t writeI32(const int32_t i32) {
Roger Meier3b771a12010-11-17 22:11:26 +0000279 (void) i32;
David Reiss6806fb82010-10-06 17:09:52 +0000280 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
281 "this protocol does not support writing (yet).");
282 }
283
284 uint32_t writeI64(const int64_t i64) {
Roger Meier3b771a12010-11-17 22:11:26 +0000285 (void) i64;
David Reiss6806fb82010-10-06 17:09:52 +0000286 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
287 "this protocol does not support writing (yet).");
288 }
289
290 uint32_t writeDouble(const double dub) {
Roger Meier3b771a12010-11-17 22:11:26 +0000291 (void) dub;
David Reiss6806fb82010-10-06 17:09:52 +0000292 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
293 "this protocol does not support writing (yet).");
294 }
295
296 uint32_t writeString(const std::string& str) {
Roger Meier3b771a12010-11-17 22:11:26 +0000297 (void) str;
David Reiss6806fb82010-10-06 17:09:52 +0000298 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
299 "this protocol does not support writing (yet).");
300 }
301
302 uint32_t writeBinary(const std::string& str) {
Roger Meier3b771a12010-11-17 22:11:26 +0000303 (void) str;
David Reiss6806fb82010-10-06 17:09:52 +0000304 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
305 "this protocol does not support writing (yet).");
306 }
307
308 uint32_t skip(TType type) {
309 return ::apache::thrift::protocol::skip(*this, type);
310 }
311
312 protected:
313 TProtocolDefaults(boost::shared_ptr<TTransport> ptrans)
314 : TProtocol(ptrans)
315 {}
316};
317
318/**
319 * Concrete TProtocol classes should inherit from TVirtualProtocol
320 * so they don't have to manually override virtual methods.
321 */
322template <class Protocol_, class Super_=TProtocolDefaults>
323class TVirtualProtocol : public Super_ {
324 public:
325 /**
326 * Writing functions.
327 */
328
329 virtual uint32_t writeMessageBegin_virt(const std::string& name,
330 const TMessageType messageType,
331 const int32_t seqid) {
332 return static_cast<Protocol_*>(this)->writeMessageBegin(name, messageType,
333 seqid);
334 }
335
336 virtual uint32_t writeMessageEnd_virt() {
337 return static_cast<Protocol_*>(this)->writeMessageEnd();
338 }
339
340
341 virtual uint32_t writeStructBegin_virt(const char* name) {
342 return static_cast<Protocol_*>(this)->writeStructBegin(name);
343 }
344
345 virtual uint32_t writeStructEnd_virt() {
346 return static_cast<Protocol_*>(this)->writeStructEnd();
347 }
348
349 virtual uint32_t writeFieldBegin_virt(const char* name,
350 const TType fieldType,
351 const int16_t fieldId) {
352 return static_cast<Protocol_*>(this)->writeFieldBegin(name, fieldType,
353 fieldId);
354 }
355
356 virtual uint32_t writeFieldEnd_virt() {
357 return static_cast<Protocol_*>(this)->writeFieldEnd();
358 }
359
360 virtual uint32_t writeFieldStop_virt() {
361 return static_cast<Protocol_*>(this)->writeFieldStop();
362 }
363
364 virtual uint32_t writeMapBegin_virt(const TType keyType,
365 const TType valType,
366 const uint32_t size) {
367 return static_cast<Protocol_*>(this)->writeMapBegin(keyType, valType, size);
368 }
369
370 virtual uint32_t writeMapEnd_virt() {
371 return static_cast<Protocol_*>(this)->writeMapEnd();
372 }
373
374 virtual uint32_t writeListBegin_virt(const TType elemType,
375 const uint32_t size) {
376 return static_cast<Protocol_*>(this)->writeListBegin(elemType, size);
377 }
378
379 virtual uint32_t writeListEnd_virt() {
380 return static_cast<Protocol_*>(this)->writeListEnd();
381 }
382
383 virtual uint32_t writeSetBegin_virt(const TType elemType,
384 const uint32_t size) {
385 return static_cast<Protocol_*>(this)->writeSetBegin(elemType, size);
386 }
387
388 virtual uint32_t writeSetEnd_virt() {
389 return static_cast<Protocol_*>(this)->writeSetEnd();
390 }
391
392 virtual uint32_t writeBool_virt(const bool value) {
393 return static_cast<Protocol_*>(this)->writeBool(value);
394 }
395
396 virtual uint32_t writeByte_virt(const int8_t byte) {
397 return static_cast<Protocol_*>(this)->writeByte(byte);
398 }
399
400 virtual uint32_t writeI16_virt(const int16_t i16) {
401 return static_cast<Protocol_*>(this)->writeI16(i16);
402 }
403
404 virtual uint32_t writeI32_virt(const int32_t i32) {
405 return static_cast<Protocol_*>(this)->writeI32(i32);
406 }
407
408 virtual uint32_t writeI64_virt(const int64_t i64) {
409 return static_cast<Protocol_*>(this)->writeI64(i64);
410 }
411
412 virtual uint32_t writeDouble_virt(const double dub) {
413 return static_cast<Protocol_*>(this)->writeDouble(dub);
414 }
415
416 virtual uint32_t writeString_virt(const std::string& str) {
417 return static_cast<Protocol_*>(this)->writeString(str);
418 }
419
420 virtual uint32_t writeBinary_virt(const std::string& str) {
421 return static_cast<Protocol_*>(this)->writeBinary(str);
422 }
423
424 /**
425 * Reading functions
426 */
427
428 virtual uint32_t readMessageBegin_virt(std::string& name,
429 TMessageType& messageType,
430 int32_t& seqid) {
431 return static_cast<Protocol_*>(this)->readMessageBegin(name, messageType,
432 seqid);
433 }
434
435 virtual uint32_t readMessageEnd_virt() {
436 return static_cast<Protocol_*>(this)->readMessageEnd();
437 }
438
439 virtual uint32_t readStructBegin_virt(std::string& name) {
440 return static_cast<Protocol_*>(this)->readStructBegin(name);
441 }
442
443 virtual uint32_t readStructEnd_virt() {
444 return static_cast<Protocol_*>(this)->readStructEnd();
445 }
446
447 virtual uint32_t readFieldBegin_virt(std::string& name,
448 TType& fieldType,
449 int16_t& fieldId) {
450 return static_cast<Protocol_*>(this)->readFieldBegin(name, fieldType,
451 fieldId);
452 }
453
454 virtual uint32_t readFieldEnd_virt() {
455 return static_cast<Protocol_*>(this)->readFieldEnd();
456 }
457
458 virtual uint32_t readMapBegin_virt(TType& keyType,
459 TType& valType,
460 uint32_t& size) {
461 return static_cast<Protocol_*>(this)->readMapBegin(keyType, valType, size);
462 }
463
464 virtual uint32_t readMapEnd_virt() {
465 return static_cast<Protocol_*>(this)->readMapEnd();
466 }
467
468 virtual uint32_t readListBegin_virt(TType& elemType,
469 uint32_t& size) {
470 return static_cast<Protocol_*>(this)->readListBegin(elemType, size);
471 }
472
473 virtual uint32_t readListEnd_virt() {
474 return static_cast<Protocol_*>(this)->readListEnd();
475 }
476
477 virtual uint32_t readSetBegin_virt(TType& elemType,
478 uint32_t& size) {
479 return static_cast<Protocol_*>(this)->readSetBegin(elemType, size);
480 }
481
482 virtual uint32_t readSetEnd_virt() {
483 return static_cast<Protocol_*>(this)->readSetEnd();
484 }
485
486 virtual uint32_t readBool_virt(bool& value) {
487 return static_cast<Protocol_*>(this)->readBool(value);
488 }
489
David Reiss8dfc7322010-10-06 17:09:58 +0000490 virtual uint32_t readBool_virt(std::vector<bool>::reference value) {
491 return static_cast<Protocol_*>(this)->readBool(value);
492 }
493
David Reiss6806fb82010-10-06 17:09:52 +0000494 virtual uint32_t readByte_virt(int8_t& byte) {
495 return static_cast<Protocol_*>(this)->readByte(byte);
496 }
497
498 virtual uint32_t readI16_virt(int16_t& i16) {
499 return static_cast<Protocol_*>(this)->readI16(i16);
500 }
501
502 virtual uint32_t readI32_virt(int32_t& i32) {
503 return static_cast<Protocol_*>(this)->readI32(i32);
504 }
505
506 virtual uint32_t readI64_virt(int64_t& i64) {
507 return static_cast<Protocol_*>(this)->readI64(i64);
508 }
509
510 virtual uint32_t readDouble_virt(double& dub) {
511 return static_cast<Protocol_*>(this)->readDouble(dub);
512 }
513
514 virtual uint32_t readString_virt(std::string& str) {
515 return static_cast<Protocol_*>(this)->readString(str);
516 }
517
518 virtual uint32_t readBinary_virt(std::string& str) {
519 return static_cast<Protocol_*>(this)->readBinary(str);
520 }
521
522 virtual uint32_t skip_virt(TType type) {
523 return static_cast<Protocol_*>(this)->skip(type);
524 }
525
526 /*
527 * Provide a default skip() implementation that uses non-virtual read
528 * methods.
529 *
530 * Note: subclasses that use TVirtualProtocol to derive from another protocol
531 * implementation (i.e., not TProtocolDefaults) should beware that this may
532 * override any non-default skip() implementation provided by the parent
533 * transport class. They may need to explicitly redefine skip() to call the
534 * correct parent implementation, if desired.
535 */
536 uint32_t skip(TType type) {
537 Protocol_* const prot = static_cast<Protocol_*>(this);
538 return ::apache::thrift::protocol::skip(*prot, type);
539 }
540
David Reiss8dfc7322010-10-06 17:09:58 +0000541 /*
542 * Provide a default readBool() implementation for use with
543 * std::vector<bool>, that behaves the same as reading into a normal bool.
544 *
545 * Subclasses can override this if desired, but there normally shouldn't
546 * be a need to.
547 */
548 uint32_t readBool(std::vector<bool>::reference value) {
549 bool b = false;
550 uint32_t ret = static_cast<Protocol_*>(this)->readBool(b);
551 value = b;
552 return ret;
553 }
554 using Super_::readBool; // so we don't hide readBool(bool&)
555
David Reiss6806fb82010-10-06 17:09:52 +0000556 protected:
557 TVirtualProtocol(boost::shared_ptr<TTransport> ptrans)
558 : Super_(ptrans)
559 {}
560};
561
562}}} // apache::thrift::protocol
563
564#endif // #define _THRIFT_PROTOCOL_TVIRTUALPROTOCOL_H_ 1