blob: 1eed4e2b51726aeaa81b2f767c0495911d3b3c1e [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>
Carel Combrink786764b2025-05-15 12:22:37 +000026#include <thrift/TUuid.h>
David Reisse4d4ea02009-04-02 21:37:17 +000027
David Reisse4d4ea02009-04-02 21:37:17 +000028/* ClassName Helper for cleaner exceptions */
29class ClassNames {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010030public:
David Reisse4d4ea02009-04-02 21:37:17 +000031 template <typename T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010032 static const char* getName() {
33 return "Unknown type";
34 }
David Reisse4d4ea02009-04-02 21:37:17 +000035};
36
Konrad Grochowski16a23a62014-11-13 15:33:38 +010037template <>
38const char* ClassNames::getName<int8_t>() {
39 return "byte";
40}
41template <>
42const char* ClassNames::getName<int16_t>() {
43 return "short";
44}
45template <>
46const char* ClassNames::getName<int32_t>() {
47 return "int";
48}
49template <>
50const char* ClassNames::getName<int64_t>() {
51 return "long";
52}
53template <>
54const char* ClassNames::getName<double>() {
55 return "double";
56}
57template <>
58const char* ClassNames::getName<std::string>() {
59 return "string";
60}
Carel Combrink786764b2025-05-15 12:22:37 +000061template <>
62const char* ClassNames::getName<apache::thrift::TUuid>() {
63 return "uuid";
64}
David Reisse4d4ea02009-04-02 21:37:17 +000065
66/* Generic Protocol I/O function for tests */
67class GenericIO {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068public:
David Reisse4d4ea02009-04-02 21:37:17 +000069 /* Write functions */
70
cyy316723a2019-01-05 16:35:14 +080071 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int8_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000072 return proto->writeByte(val);
73 }
74
cyy316723a2019-01-05 16:35:14 +080075 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int16_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000076 return proto->writeI16(val);
77 }
78
cyy316723a2019-01-05 16:35:14 +080079 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int32_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000080 return proto->writeI32(val);
81 }
82
cyy316723a2019-01-05 16:35:14 +080083 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const double& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000084 return proto->writeDouble(val);
85 }
86
cyy316723a2019-01-05 16:35:14 +080087 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int64_t& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000088 return proto->writeI64(val);
89 }
90
cyy316723a2019-01-05 16:35:14 +080091 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const std::string& val) {
David Reisse4d4ea02009-04-02 21:37:17 +000092 return proto->writeString(val);
93 }
94
Carel Combrink786764b2025-05-15 12:22:37 +000095 static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const apache::thrift::TUuid& val) {
96 return proto->writeUUID(val);
97 }
98
David Reisse4d4ea02009-04-02 21:37:17 +000099 /* Read functions */
100
cyy316723a2019-01-05 16:35:14 +0800101 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 +0000102
cyy316723a2019-01-05 16:35:14 +0800103 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 +0000104
cyy316723a2019-01-05 16:35:14 +0800105 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 +0000106
cyy316723a2019-01-05 16:35:14 +0800107 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 +0000108
cyy316723a2019-01-05 16:35:14 +0800109 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 +0000110
cyy316723a2019-01-05 16:35:14 +0800111 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) {
David Reisse4d4ea02009-04-02 21:37:17 +0000112 return proto->readString(val);
113 }
Carel Combrink786764b2025-05-15 12:22:37 +0000114
115 static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, apache::thrift::TUuid& val) {
116 return proto->readUUID(val);
117 }
David Reisse4d4ea02009-04-02 21:37:17 +0000118};
119
120#endif