blob: c175561c122708c5ae6aa0ce4f4aab359d2e6512 [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
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/protocol/TBinaryProtocol.h>
24#include <thrift/transport/TBufferTransports.h>
25#include <thrift/Thrift.h>
David Reisse4d4ea02009-04-02 21:37:17 +000026
27using boost::shared_ptr;
28using namespace apache::thrift::protocol;
29
30/* ClassName Helper for cleaner exceptions */
31class ClassNames {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010032public:
David Reisse4d4ea02009-04-02 21:37:17 +000033 template <typename T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034 static const char* getName() {
35 return "Unknown type";
36 }
David Reisse4d4ea02009-04-02 21:37:17 +000037};
38
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039template <>
40const char* ClassNames::getName<int8_t>() {
41 return "byte";
42}
43template <>
44const char* ClassNames::getName<int16_t>() {
45 return "short";
46}
47template <>
48const char* ClassNames::getName<int32_t>() {
49 return "int";
50}
51template <>
52const char* ClassNames::getName<int64_t>() {
53 return "long";
54}
55template <>
56const char* ClassNames::getName<double>() {
57 return "double";
58}
59template <>
60const char* ClassNames::getName<std::string>() {
61 return "string";
62}
David Reisse4d4ea02009-04-02 21:37:17 +000063
64/* Generic Protocol I/O function for tests */
65class GenericIO {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066public:
David Reisse4d4ea02009-04-02 21:37:17 +000067 /* 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 Grochowski16a23a62014-11-13 15:33:38 +010095 static uint32_t read(shared_ptr<TProtocol> proto, int8_t& val) { return proto->readByte(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000096
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 static uint32_t read(shared_ptr<TProtocol> proto, int16_t& val) { return proto->readI16(val); }
David Reisse4d4ea02009-04-02 21:37:17 +000098
Konrad Grochowski16a23a62014-11-13 15:33:38 +010099 static uint32_t read(shared_ptr<TProtocol> proto, int32_t& val) { return proto->readI32(val); }
David Reisse4d4ea02009-04-02 21:37:17 +0000100
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101 static uint32_t read(shared_ptr<TProtocol> proto, int64_t& val) { return proto->readI64(val); }
David Reisse4d4ea02009-04-02 21:37:17 +0000102
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100103 static uint32_t read(shared_ptr<TProtocol> proto, double& val) { return proto->readDouble(val); }
David Reisse4d4ea02009-04-02 21:37:17 +0000104
105 static uint32_t read(shared_ptr<TProtocol> proto, std::string& val) {
106 return proto->readString(val);
107 }
David Reisse4d4ea02009-04-02 21:37:17 +0000108};
109
110#endif