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_TEST_GENERICHELPERS_H_
21 #define _THRIFT_TEST_GENERICHELPERS_H_ 1
22 
23 #include <thrift/protocol/TProtocol.h>
24 #include <thrift/stdcxx.h>
25 #include <thrift/Thrift.h>
26 
27 /* ClassName Helper for cleaner exceptions */
28 class ClassNames {
29 public:
30   template <typename T>
getName()31   static const char* getName() {
32     return "Unknown type";
33   }
34 };
35 
36 template <>
37 const char* ClassNames::getName<int8_t>() {
38   return "byte";
39 }
40 template <>
41 const char* ClassNames::getName<int16_t>() {
42   return "short";
43 }
44 template <>
45 const char* ClassNames::getName<int32_t>() {
46   return "int";
47 }
48 template <>
49 const char* ClassNames::getName<int64_t>() {
50   return "long";
51 }
52 template <>
53 const char* ClassNames::getName<double>() {
54   return "double";
55 }
56 template <>
57 const char* ClassNames::getName<std::string>() {
58   return "string";
59 }
60 
61 /* Generic Protocol I/O function for tests */
62 class GenericIO {
63 public:
64   /* Write functions */
65 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const int8_t & val)66   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int8_t& val) {
67     return proto->writeByte(val);
68   }
69 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const int16_t & val)70   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int16_t& val) {
71     return proto->writeI16(val);
72   }
73 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const int32_t & val)74   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int32_t& val) {
75     return proto->writeI32(val);
76   }
77 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const double & val)78   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const double& val) {
79     return proto->writeDouble(val);
80   }
81 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const int64_t & val)82   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int64_t& val) {
83     return proto->writeI64(val);
84   }
85 
write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,const std::string & val)86   static uint32_t write(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, const std::string& val) {
87     return proto->writeString(val);
88   }
89 
90   /* Read functions */
91 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,int8_t & val)92   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, int8_t& val) { return proto->readByte(val); }
93 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,int16_t & val)94   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, int16_t& val) { return proto->readI16(val); }
95 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,int32_t & val)96   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, int32_t& val) { return proto->readI32(val); }
97 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,int64_t & val)98   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, int64_t& val) { return proto->readI64(val); }
99 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,double & val)100   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, double& val) { return proto->readDouble(val); }
101 
read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto,std::string & val)102   static uint32_t read(apache::thrift::stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) {
103     return proto->readString(val);
104   }
105 };
106 
107 #endif
108