blob: df52dabc84aee90fac031db71b709bdb9d423797 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_THRIFT_H_
21#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Mark Slee4f261c52007-04-13 00:33:24 +000023#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
David Reiss7247b8c2009-04-02 23:05:40 +000026#include <stdio.h>
David Reiss44ff76f2010-10-06 17:10:15 +000027#include <assert.h>
Mark Slee4f261c52007-04-13 00:33:24 +000028
David Reiss08d2f112009-05-21 02:28:36 +000029#include <sys/types.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000030#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000031#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000032#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000033#endif
Mark Sleee8540632006-05-30 09:24:40 +000034#include <string>
35#include <map>
36#include <list>
37#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000038#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000039#include <exception>
David Reissc3b36222010-10-06 17:10:10 +000040#include <typeinfo>
Marc Slemko5b126d62006-08-11 23:03:42 +000041
Bryan Duxbury7a9fb812011-09-01 18:31:53 +000042#include <boost/utility/enable_if.hpp>
43#include <boost/type_traits/is_convertible.hpp>
44
Aditya Agarwald622e962006-10-11 02:42:49 +000045#include "TLogging.h"
46
Bryan Duxbury7a9fb812011-09-01 18:31:53 +000047/**
48 * Helper macros to allow function overloading even when using
49 * boost::shared_ptr.
50 *
51 * shared_ptr makes overloading really annoying, since shared_ptr defines
52 * constructor methods to allow one shared_ptr type to be constructed from any
53 * other shared_ptr type. (Even if it would be a compile error to actually try
54 * to instantiate the constructor.) These macros add an extra argument to the
55 * function to cause it to only be instantiated if a pointer of type T is
56 * convertible to a pointer of type U.
57 *
58 * THRIFT_OVERLOAD_IF should be used in function declarations.
59 * THRIFT_OVERLOAD_IF_DEFN should be used in the function definition, if it is
60 * defined separately from where it is declared.
61 */
62#define THRIFT_OVERLOAD_IF_DEFN(T, Y) \
63 typename ::boost::enable_if<typename ::boost::is_convertible<T*, Y*>::type, \
64 void*>::type
65
66#define THRIFT_OVERLOAD_IF(T, Y) \
67 THRIFT_OVERLOAD_IF_DEFN(T, Y) = NULL
68
T Jake Lucianib5e62212009-01-31 22:36:20 +000069namespace apache { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000070
Roger Meier330b5ae2011-04-18 19:46:02 +000071class TEnumIterator : public std::iterator<std::forward_iterator_tag, std::pair<int, const char*> > {
David Reiss44ff76f2010-10-06 17:10:15 +000072 public:
73 TEnumIterator(int n,
74 int* enums,
75 const char** names) :
76 ii_(0), n_(n), enums_(enums), names_(names) {
77 }
78
79 int operator ++() {
80 return ++ii_;
81 }
82
83 bool operator !=(const TEnumIterator& end) {
84 assert(end.n_ == -1);
85 return (ii_ != n_);
86 }
87
88 std::pair<int, const char*> operator*() const {
89 return std::make_pair(enums_[ii_], names_[ii_]);
90 }
91
92 private:
93 int ii_;
94 const int n_;
95 int* enums_;
96 const char** names_;
97};
98
David Reissfaebedd2007-09-17 23:20:38 +000099class TOutput {
100 public:
David Reiss9b209552008-04-08 06:26:05 +0000101 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +0000102
103 inline void setOutputFunction(void (*function)(const char *)){
104 f_ = function;
105 }
106
107 inline void operator()(const char *message){
108 f_(message);
109 }
110
David Reiss01e55c12008-07-13 22:18:51 +0000111 // It is important to have a const char* overload here instead of
112 // just the string version, otherwise errno could be corrupted
113 // if there is some problem allocating memory when constructing
114 // the string.
115 void perror(const char *message, int errno_copy);
116 inline void perror(const std::string &message, int errno_copy) {
117 perror(message.c_str(), errno_copy);
118 }
119
120 void printf(const char *message, ...);
121
David Reiss9b209552008-04-08 06:26:05 +0000122 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000123 time_t now;
David Reissd28ce102009-05-21 02:28:14 +0000124 char dbgtime[26];
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000125 time(&now);
126 ctime_r(&now, dbgtime);
127 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +0000128 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000129 }
David Reiss9b209552008-04-08 06:26:05 +0000130
131 /** Just like strerror_r but returns a C++ string object. */
132 static std::string strerror_s(int errno_copy);
133
David Reissfaebedd2007-09-17 23:20:38 +0000134 private:
boz6ded7752007-06-05 22:41:18 +0000135 void (*f_)(const char *);
136};
137
138extern TOutput GlobalOutput;
139
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000140class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +0000141 public:
Roger Meier178f8f22010-10-25 12:36:04 +0000142 TException():
143 message_() {}
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000144
Mark Slee82a6c0f2007-04-04 21:08:21 +0000145 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +0000146 message_(message) {}
147
Martin Kraemer8196a612006-12-09 01:57:58 +0000148 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +0000149
Martin Kraemer92a2eac2007-02-05 20:58:41 +0000150 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000151 if (message_.empty()) {
152 return "Default TException.";
153 } else {
154 return message_.c_str();
155 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000156 }
157
David Reissfaebedd2007-09-17 23:20:38 +0000158 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +0000159 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000160
Marc Slemko5b126d62006-08-11 23:03:42 +0000161};
162
Mark Sleef9831082007-02-20 20:59:21 +0000163
David Reissd779cbe2007-08-31 01:42:55 +0000164// Forward declare this structure used by TDenseProtocol
165namespace reflection { namespace local {
166struct TypeSpec;
167}}
168
David Reiss5ddabb82010-10-06 17:09:37 +0000169class TDelayedException {
170 public:
171 template <class E> static TDelayedException* delayException(const E& e);
172 virtual void throw_it() = 0;
173 virtual ~TDelayedException() {};
174};
175
176template <class E> class TExceptionWrapper : public TDelayedException {
177 public:
178 TExceptionWrapper(const E& e) : e_(e) {}
179 virtual void throw_it() {
180 E temp(e_);
181 delete this;
182 throw temp;
183 }
184 private:
185 E e_;
186};
187
188template <class E>
189TDelayedException* TDelayedException::delayException(const E& e) {
190 return new TExceptionWrapper<E>(e);
191}
David Reissd779cbe2007-08-31 01:42:55 +0000192
David Reissc3b36222010-10-06 17:10:10 +0000193#if T_GLOBAL_DEBUG_VIRTUAL > 1
194void profile_virtual_call(const std::type_info& info);
195void profile_generic_protocol(const std::type_info& template_type,
196 const std::type_info& prot_type);
197void profile_print_info(FILE *f);
198void profile_print_info();
199void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f);
200#endif
201
T Jake Lucianib5e62212009-01-31 22:36:20 +0000202}} // apache::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000203
Mark Sleef5f2be42006-09-05 21:05:31 +0000204#endif // #ifndef _THRIFT_THRIFT_H_