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> |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 26 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 27 | /* ClassName Helper for cleaner exceptions */ |
| 28 | class ClassNames { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | public: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 30 | template <typename T> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | static const char* getName() { |
| 32 | return "Unknown type"; |
| 33 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 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 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 60 | |
| 61 | /* Generic Protocol I/O function for tests */ |
| 62 | class GenericIO { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 63 | public: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 64 | /* Write functions */ |
| 65 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 66 | 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] | 67 | return proto->writeByte(val); |
| 68 | } |
| 69 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 70 | 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] | 71 | return proto->writeI16(val); |
| 72 | } |
| 73 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 74 | 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] | 75 | return proto->writeI32(val); |
| 76 | } |
| 77 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 78 | 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] | 79 | return proto->writeDouble(val); |
| 80 | } |
| 81 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 82 | 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] | 83 | return proto->writeI64(val); |
| 84 | } |
| 85 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 86 | 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] | 87 | return proto->writeString(val); |
| 88 | } |
| 89 | |
| 90 | /* Read functions */ |
| 91 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 92 | 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] | 93 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 94 | 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] | 95 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 96 | 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] | 97 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 98 | 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] | 99 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 100 | 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] | 101 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 102 | 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] | 103 | return proto->readString(val); |
| 104 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |