David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 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 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 23 | #include <thrift/protocol/TProtocol.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 24 | #include <memory> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/Thrift.h> |
Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 26 | #include <thrift/TUuid.h> |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 27 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 28 | /* ClassName Helper for cleaner exceptions */ |
| 29 | class ClassNames { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 30 | public: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 31 | template <typename T> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 32 | static const char* getName() { |
| 33 | return "Unknown type"; |
| 34 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | template <> |
| 38 | const char* ClassNames::getName<int8_t>() { |
| 39 | return "byte"; |
| 40 | } |
| 41 | template <> |
| 42 | const char* ClassNames::getName<int16_t>() { |
| 43 | return "short"; |
| 44 | } |
| 45 | template <> |
| 46 | const char* ClassNames::getName<int32_t>() { |
| 47 | return "int"; |
| 48 | } |
| 49 | template <> |
| 50 | const char* ClassNames::getName<int64_t>() { |
| 51 | return "long"; |
| 52 | } |
| 53 | template <> |
| 54 | const char* ClassNames::getName<double>() { |
| 55 | return "double"; |
| 56 | } |
| 57 | template <> |
| 58 | const char* ClassNames::getName<std::string>() { |
| 59 | return "string"; |
| 60 | } |
Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 61 | template <> |
| 62 | const char* ClassNames::getName<apache::thrift::TUuid>() { |
| 63 | return "uuid"; |
| 64 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 65 | |
| 66 | /* Generic Protocol I/O function for tests */ |
| 67 | class GenericIO { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 68 | public: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 69 | /* Write functions */ |
| 70 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 71 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int8_t& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 72 | return proto->writeByte(val); |
| 73 | } |
| 74 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 75 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int16_t& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 76 | return proto->writeI16(val); |
| 77 | } |
| 78 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 79 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int32_t& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 80 | return proto->writeI32(val); |
| 81 | } |
| 82 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 83 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const double& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 84 | return proto->writeDouble(val); |
| 85 | } |
| 86 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 87 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const int64_t& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 88 | return proto->writeI64(val); |
| 89 | } |
| 90 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 91 | static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const std::string& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 92 | return proto->writeString(val); |
| 93 | } |
| 94 | |
Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 95 | 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 Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 99 | /* Read functions */ |
| 100 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 101 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int8_t& val) { return proto->readByte(val); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 102 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 103 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int16_t& val) { return proto->readI16(val); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 104 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 105 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int32_t& val) { return proto->readI32(val); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 106 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 107 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int64_t& val) { return proto->readI64(val); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 108 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 109 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, double& val) { return proto->readDouble(val); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 110 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 111 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 112 | return proto->readString(val); |
| 113 | } |
Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 114 | |
| 115 | static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, apache::thrift::TUuid& val) { |
| 116 | return proto->readUUID(val); |
| 117 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | #endif |