blob: bcef9f24252805cc30b0a3ed3870cbc4009e7243 [file] [log] [blame]
David Reisse4d4ea02009-04-02 21:37:17 +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_TEST_GENERICHELPERS_H_
21#define _THRIFT_TEST_GENERICHELPERS_H_ 1
22
James E. King, III82ae9572017-08-05 12:23:54 -040023#include <thrift/protocol/TProtocol.h>
cyy316723a2019-01-05 16:35:14 +080024#include <memory>
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/Thrift.h>
David Reisse4d4ea02009-04-02 21:37:17 +000026
David Reisse4d4ea02009-04-02 21:37:17 +000027/* ClassName Helper for cleaner exceptions */
28class ClassNames {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029public:
David Reisse4d4ea02009-04-02 21:37:17 +000030 template <typename T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010031 static const char* getName() {
32 return "Unknown type";
33 }
David Reisse4d4ea02009-04-02 21:37:17 +000034};
35
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036template <>
37const char* ClassNames::getName<int8_t>() {
38 return "byte";
39}
40template <>
41const char* ClassNames::getName<int16_t>() {
42 return "short";
43}
44template <>
45const char* ClassNames::getName<int32_t>() {
46 return "int";
47}
48template <>
49const char* ClassNames::getName<int64_t>() {
50 return "long";
51}
52template <>
53const char* ClassNames::getName<double>() {
54 return "double";
55}
56template <>
57const char* ClassNames::getName<std::string>() {
58 return "string";
59}
David Reisse4d4ea02009-04-02 21:37:17 +000060
61/* Generic Protocol I/O function for tests */
62class GenericIO {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063public:
David Reisse4d4ea02009-04-02 21:37:17 +000064 /* Write functions */
65
cyy316723a2019-01-05 16:35:14 +080066 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int8_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000067 return proto->writeByte(val);
68 }
69
cyy316723a2019-01-05 16:35:14 +080070 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int16_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000071 return proto->writeI16(val);
72 }
73
cyy316723a2019-01-05 16:35:14 +080074 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int32_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000075 return proto->writeI32(val);
76 }
77
cyy316723a2019-01-05 16:35:14 +080078 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const double& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000079 return proto->writeDouble(val);
80 }
81
cyy316723a2019-01-05 16:35:14 +080082 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int64_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000083 return proto->writeI64(val);
84 }
85
cyy316723a2019-01-05 16:35:14 +080086 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const std::string& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000087 return proto->writeString(val);
88 }
89
90 /* Read functions */
91
cyy316723a2019-01-05 16:35:14 +080092 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int8_t& val) { return proto->readByte(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000093
cyy316723a2019-01-05 16:35:14 +080094 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int16_t& val) { return proto->readI16(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000095
cyy316723a2019-01-05 16:35:14 +080096 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int32_t& val) { return proto->readI32(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000097
cyy316723a2019-01-05 16:35:14 +080098 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int64_t& val) { return proto->readI64(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000099
cyy316723a2019-01-05 16:35:14 +0800100 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, double& val) { return proto->readDouble(val); }
David Reisse4d4ea02009-04-02 21:37:17 +0000101
cyy316723a2019-01-05 16:35:14 +0800102 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) {
David Reisse4d4ea02009-04-02 21:37:17 +0000103 return proto->readString(val);
104 }
David Reisse4d4ea02009-04-02 21:37:17 +0000105};
106
107#endif