blob: 9133dbedcf80e96ce3de6bb3c4e37e608be8261c [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) {
45 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
46 "this protocol does not support reading (yet).");
47 }
48
49 uint32_t readMessageEnd() {
50 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
51 "this protocol does not support reading (yet).");
52 }
53
54 uint32_t readStructBegin(std::string& name) {
55 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
56 "this protocol does not support reading (yet).");
57 }
58
59 uint32_t readStructEnd() {
60 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
61 "this protocol does not support reading (yet).");
62 }
63
64 uint32_t readFieldBegin(std::string& name,
65 TType& fieldType,
66 int16_t& fieldId) {
67 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
68 "this protocol does not support reading (yet).");
69 }
70
71 uint32_t readFieldEnd() {
72 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
73 "this protocol does not support reading (yet).");
74 }
75
76 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size) {
77 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
78 "this protocol does not support reading (yet).");
79 }
80
81 uint32_t readMapEnd() {
82 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
83 "this protocol does not support reading (yet).");
84 }
85
86 uint32_t readListBegin(TType& elemType, uint32_t& size) {
87 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
88 "this protocol does not support reading (yet).");
89 }
90
91 uint32_t readListEnd() {
92 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
93 "this protocol does not support reading (yet).");
94 }
95
96 uint32_t readSetBegin(TType& elemType, uint32_t& size) {
97 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
98 "this protocol does not support reading (yet).");
99 }
100
101 uint32_t readSetEnd() {
102 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
103 "this protocol does not support reading (yet).");
104 }
105
106 uint32_t readBool(bool& value) {
107 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
108 "this protocol does not support reading (yet).");
109 }
110
David Reiss8dfc7322010-10-06 17:09:58 +0000111 uint32_t readBool(std::vector<bool>::reference value) {
112 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
113 "this protocol does not support reading (yet).");
114 }
115
David Reiss6806fb82010-10-06 17:09:52 +0000116 uint32_t readByte(int8_t& byte) {
117 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
118 "this protocol does not support reading (yet).");
119 }
120
121 uint32_t readI16(int16_t& i16) {
122 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
123 "this protocol does not support reading (yet).");
124 }
125
126 uint32_t readI32(int32_t& i32) {
127 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
128 "this protocol does not support reading (yet).");
129 }
130
131 uint32_t readI64(int64_t& i64) {
132 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
133 "this protocol does not support reading (yet).");
134 }
135
136 uint32_t readDouble(double& dub) {
137 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
138 "this protocol does not support reading (yet).");
139 }
140
141 uint32_t readString(std::string& str) {
142 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
143 "this protocol does not support reading (yet).");
144 }
145
146 uint32_t readBinary(std::string& str) {
147 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
148 "this protocol does not support reading (yet).");
149 }
150
151 uint32_t writeMessageBegin(const std::string& name,
152 const TMessageType messageType,
153 const int32_t seqid) {
154 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
155 "this protocol does not support writing (yet).");
156 }
157
158 uint32_t writeMessageEnd() {
159 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
160 "this protocol does not support writing (yet).");
161 }
162
163
164 uint32_t writeStructBegin(const char* name) {
165 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
166 "this protocol does not support writing (yet).");
167 }
168
169 uint32_t writeStructEnd() {
170 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
171 "this protocol does not support writing (yet).");
172 }
173
174 uint32_t writeFieldBegin(const char* name,
175 const TType fieldType,
176 const int16_t fieldId) {
177 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
178 "this protocol does not support writing (yet).");
179 }
180
181 uint32_t writeFieldEnd() {
182 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
183 "this protocol does not support writing (yet).");
184 }
185
186 uint32_t writeFieldStop() {
187 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
188 "this protocol does not support writing (yet).");
189 }
190
191 uint32_t writeMapBegin(const TType keyType,
192 const TType valType,
193 const uint32_t size) {
194 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
195 "this protocol does not support writing (yet).");
196 }
197
198 uint32_t writeMapEnd() {
199 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
200 "this protocol does not support writing (yet).");
201 }
202
203 uint32_t writeListBegin(const TType elemType, const uint32_t size) {
204 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
205 "this protocol does not support writing (yet).");
206 }
207
208 uint32_t writeListEnd() {
209 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
210 "this protocol does not support writing (yet).");
211 }
212
213 uint32_t writeSetBegin(const TType elemType, const uint32_t size) {
214 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
215 "this protocol does not support writing (yet).");
216 }
217
218 uint32_t writeSetEnd() {
219 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
220 "this protocol does not support writing (yet).");
221 }
222
223 uint32_t writeBool(const bool value) {
224 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
225 "this protocol does not support writing (yet).");
226 }
227
228 uint32_t writeByte(const int8_t byte) {
229 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
230 "this protocol does not support writing (yet).");
231 }
232
233 uint32_t writeI16(const int16_t i16) {
234 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
235 "this protocol does not support writing (yet).");
236 }
237
238 uint32_t writeI32(const int32_t i32) {
239 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
240 "this protocol does not support writing (yet).");
241 }
242
243 uint32_t writeI64(const int64_t i64) {
244 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
245 "this protocol does not support writing (yet).");
246 }
247
248 uint32_t writeDouble(const double dub) {
249 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
250 "this protocol does not support writing (yet).");
251 }
252
253 uint32_t writeString(const std::string& str) {
254 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
255 "this protocol does not support writing (yet).");
256 }
257
258 uint32_t writeBinary(const std::string& str) {
259 throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
260 "this protocol does not support writing (yet).");
261 }
262
263 uint32_t skip(TType type) {
264 return ::apache::thrift::protocol::skip(*this, type);
265 }
266
267 protected:
268 TProtocolDefaults(boost::shared_ptr<TTransport> ptrans)
269 : TProtocol(ptrans)
270 {}
271};
272
273/**
274 * Concrete TProtocol classes should inherit from TVirtualProtocol
275 * so they don't have to manually override virtual methods.
276 */
277template <class Protocol_, class Super_=TProtocolDefaults>
278class TVirtualProtocol : public Super_ {
279 public:
280 /**
281 * Writing functions.
282 */
283
284 virtual uint32_t writeMessageBegin_virt(const std::string& name,
285 const TMessageType messageType,
286 const int32_t seqid) {
287 return static_cast<Protocol_*>(this)->writeMessageBegin(name, messageType,
288 seqid);
289 }
290
291 virtual uint32_t writeMessageEnd_virt() {
292 return static_cast<Protocol_*>(this)->writeMessageEnd();
293 }
294
295
296 virtual uint32_t writeStructBegin_virt(const char* name) {
297 return static_cast<Protocol_*>(this)->writeStructBegin(name);
298 }
299
300 virtual uint32_t writeStructEnd_virt() {
301 return static_cast<Protocol_*>(this)->writeStructEnd();
302 }
303
304 virtual uint32_t writeFieldBegin_virt(const char* name,
305 const TType fieldType,
306 const int16_t fieldId) {
307 return static_cast<Protocol_*>(this)->writeFieldBegin(name, fieldType,
308 fieldId);
309 }
310
311 virtual uint32_t writeFieldEnd_virt() {
312 return static_cast<Protocol_*>(this)->writeFieldEnd();
313 }
314
315 virtual uint32_t writeFieldStop_virt() {
316 return static_cast<Protocol_*>(this)->writeFieldStop();
317 }
318
319 virtual uint32_t writeMapBegin_virt(const TType keyType,
320 const TType valType,
321 const uint32_t size) {
322 return static_cast<Protocol_*>(this)->writeMapBegin(keyType, valType, size);
323 }
324
325 virtual uint32_t writeMapEnd_virt() {
326 return static_cast<Protocol_*>(this)->writeMapEnd();
327 }
328
329 virtual uint32_t writeListBegin_virt(const TType elemType,
330 const uint32_t size) {
331 return static_cast<Protocol_*>(this)->writeListBegin(elemType, size);
332 }
333
334 virtual uint32_t writeListEnd_virt() {
335 return static_cast<Protocol_*>(this)->writeListEnd();
336 }
337
338 virtual uint32_t writeSetBegin_virt(const TType elemType,
339 const uint32_t size) {
340 return static_cast<Protocol_*>(this)->writeSetBegin(elemType, size);
341 }
342
343 virtual uint32_t writeSetEnd_virt() {
344 return static_cast<Protocol_*>(this)->writeSetEnd();
345 }
346
347 virtual uint32_t writeBool_virt(const bool value) {
348 return static_cast<Protocol_*>(this)->writeBool(value);
349 }
350
351 virtual uint32_t writeByte_virt(const int8_t byte) {
352 return static_cast<Protocol_*>(this)->writeByte(byte);
353 }
354
355 virtual uint32_t writeI16_virt(const int16_t i16) {
356 return static_cast<Protocol_*>(this)->writeI16(i16);
357 }
358
359 virtual uint32_t writeI32_virt(const int32_t i32) {
360 return static_cast<Protocol_*>(this)->writeI32(i32);
361 }
362
363 virtual uint32_t writeI64_virt(const int64_t i64) {
364 return static_cast<Protocol_*>(this)->writeI64(i64);
365 }
366
367 virtual uint32_t writeDouble_virt(const double dub) {
368 return static_cast<Protocol_*>(this)->writeDouble(dub);
369 }
370
371 virtual uint32_t writeString_virt(const std::string& str) {
372 return static_cast<Protocol_*>(this)->writeString(str);
373 }
374
375 virtual uint32_t writeBinary_virt(const std::string& str) {
376 return static_cast<Protocol_*>(this)->writeBinary(str);
377 }
378
379 /**
380 * Reading functions
381 */
382
383 virtual uint32_t readMessageBegin_virt(std::string& name,
384 TMessageType& messageType,
385 int32_t& seqid) {
386 return static_cast<Protocol_*>(this)->readMessageBegin(name, messageType,
387 seqid);
388 }
389
390 virtual uint32_t readMessageEnd_virt() {
391 return static_cast<Protocol_*>(this)->readMessageEnd();
392 }
393
394 virtual uint32_t readStructBegin_virt(std::string& name) {
395 return static_cast<Protocol_*>(this)->readStructBegin(name);
396 }
397
398 virtual uint32_t readStructEnd_virt() {
399 return static_cast<Protocol_*>(this)->readStructEnd();
400 }
401
402 virtual uint32_t readFieldBegin_virt(std::string& name,
403 TType& fieldType,
404 int16_t& fieldId) {
405 return static_cast<Protocol_*>(this)->readFieldBegin(name, fieldType,
406 fieldId);
407 }
408
409 virtual uint32_t readFieldEnd_virt() {
410 return static_cast<Protocol_*>(this)->readFieldEnd();
411 }
412
413 virtual uint32_t readMapBegin_virt(TType& keyType,
414 TType& valType,
415 uint32_t& size) {
416 return static_cast<Protocol_*>(this)->readMapBegin(keyType, valType, size);
417 }
418
419 virtual uint32_t readMapEnd_virt() {
420 return static_cast<Protocol_*>(this)->readMapEnd();
421 }
422
423 virtual uint32_t readListBegin_virt(TType& elemType,
424 uint32_t& size) {
425 return static_cast<Protocol_*>(this)->readListBegin(elemType, size);
426 }
427
428 virtual uint32_t readListEnd_virt() {
429 return static_cast<Protocol_*>(this)->readListEnd();
430 }
431
432 virtual uint32_t readSetBegin_virt(TType& elemType,
433 uint32_t& size) {
434 return static_cast<Protocol_*>(this)->readSetBegin(elemType, size);
435 }
436
437 virtual uint32_t readSetEnd_virt() {
438 return static_cast<Protocol_*>(this)->readSetEnd();
439 }
440
441 virtual uint32_t readBool_virt(bool& value) {
442 return static_cast<Protocol_*>(this)->readBool(value);
443 }
444
David Reiss8dfc7322010-10-06 17:09:58 +0000445 virtual uint32_t readBool_virt(std::vector<bool>::reference value) {
446 return static_cast<Protocol_*>(this)->readBool(value);
447 }
448
David Reiss6806fb82010-10-06 17:09:52 +0000449 virtual uint32_t readByte_virt(int8_t& byte) {
450 return static_cast<Protocol_*>(this)->readByte(byte);
451 }
452
453 virtual uint32_t readI16_virt(int16_t& i16) {
454 return static_cast<Protocol_*>(this)->readI16(i16);
455 }
456
457 virtual uint32_t readI32_virt(int32_t& i32) {
458 return static_cast<Protocol_*>(this)->readI32(i32);
459 }
460
461 virtual uint32_t readI64_virt(int64_t& i64) {
462 return static_cast<Protocol_*>(this)->readI64(i64);
463 }
464
465 virtual uint32_t readDouble_virt(double& dub) {
466 return static_cast<Protocol_*>(this)->readDouble(dub);
467 }
468
469 virtual uint32_t readString_virt(std::string& str) {
470 return static_cast<Protocol_*>(this)->readString(str);
471 }
472
473 virtual uint32_t readBinary_virt(std::string& str) {
474 return static_cast<Protocol_*>(this)->readBinary(str);
475 }
476
477 virtual uint32_t skip_virt(TType type) {
478 return static_cast<Protocol_*>(this)->skip(type);
479 }
480
481 /*
482 * Provide a default skip() implementation that uses non-virtual read
483 * methods.
484 *
485 * Note: subclasses that use TVirtualProtocol to derive from another protocol
486 * implementation (i.e., not TProtocolDefaults) should beware that this may
487 * override any non-default skip() implementation provided by the parent
488 * transport class. They may need to explicitly redefine skip() to call the
489 * correct parent implementation, if desired.
490 */
491 uint32_t skip(TType type) {
492 Protocol_* const prot = static_cast<Protocol_*>(this);
493 return ::apache::thrift::protocol::skip(*prot, type);
494 }
495
David Reiss8dfc7322010-10-06 17:09:58 +0000496 /*
497 * Provide a default readBool() implementation for use with
498 * std::vector<bool>, that behaves the same as reading into a normal bool.
499 *
500 * Subclasses can override this if desired, but there normally shouldn't
501 * be a need to.
502 */
503 uint32_t readBool(std::vector<bool>::reference value) {
504 bool b = false;
505 uint32_t ret = static_cast<Protocol_*>(this)->readBool(b);
506 value = b;
507 return ret;
508 }
509 using Super_::readBool; // so we don't hide readBool(bool&)
510
David Reiss6806fb82010-10-06 17:09:52 +0000511 protected:
512 TVirtualProtocol(boost::shared_ptr<TTransport> ptrans)
513 : Super_(ptrans)
514 {}
515};
516
517}}} // apache::thrift::protocol
518
519#endif // #define _THRIFT_PROTOCOL_TVIRTUALPROTOCOL_H_ 1