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