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