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