blob: cf766f2cc5f00144e78c2011033cb0a325a5cdaa [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
Aditya Agarwald622e962006-10-11 02:42:49 +000042#include "TLogging.h"
43
T Jake Lucianib5e62212009-01-31 22:36:20 +000044namespace apache { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000045
David Reiss44ff76f2010-10-06 17:10:15 +000046class TEnumIterator {
47 public:
48 TEnumIterator(int n,
49 int* enums,
50 const char** names) :
51 ii_(0), n_(n), enums_(enums), names_(names) {
52 }
53
54 int operator ++() {
55 return ++ii_;
56 }
57
58 bool operator !=(const TEnumIterator& end) {
59 assert(end.n_ == -1);
60 return (ii_ != n_);
61 }
62
63 std::pair<int, const char*> operator*() const {
64 return std::make_pair(enums_[ii_], names_[ii_]);
65 }
66
67 private:
68 int ii_;
69 const int n_;
70 int* enums_;
71 const char** names_;
72};
73
David Reissfaebedd2007-09-17 23:20:38 +000074class TOutput {
75 public:
David Reiss9b209552008-04-08 06:26:05 +000076 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000077
78 inline void setOutputFunction(void (*function)(const char *)){
79 f_ = function;
80 }
81
82 inline void operator()(const char *message){
83 f_(message);
84 }
85
David Reiss01e55c12008-07-13 22:18:51 +000086 // It is important to have a const char* overload here instead of
87 // just the string version, otherwise errno could be corrupted
88 // if there is some problem allocating memory when constructing
89 // the string.
90 void perror(const char *message, int errno_copy);
91 inline void perror(const std::string &message, int errno_copy) {
92 perror(message.c_str(), errno_copy);
93 }
94
95 void printf(const char *message, ...);
96
David Reiss9b209552008-04-08 06:26:05 +000097 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000098 time_t now;
David Reissd28ce102009-05-21 02:28:14 +000099 char dbgtime[26];
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000100 time(&now);
101 ctime_r(&now, dbgtime);
102 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +0000103 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000104 }
David Reiss9b209552008-04-08 06:26:05 +0000105
106 /** Just like strerror_r but returns a C++ string object. */
107 static std::string strerror_s(int errno_copy);
108
David Reissfaebedd2007-09-17 23:20:38 +0000109 private:
boz6ded7752007-06-05 22:41:18 +0000110 void (*f_)(const char *);
111};
112
113extern TOutput GlobalOutput;
114
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000115class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +0000116 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000117 TException() {}
118
Mark Slee82a6c0f2007-04-04 21:08:21 +0000119 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +0000120 message_(message) {}
121
Martin Kraemer8196a612006-12-09 01:57:58 +0000122 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +0000123
Martin Kraemer92a2eac2007-02-05 20:58:41 +0000124 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000125 if (message_.empty()) {
126 return "Default TException.";
127 } else {
128 return message_.c_str();
129 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000130 }
131
David Reissfaebedd2007-09-17 23:20:38 +0000132 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +0000133 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000134
Marc Slemko5b126d62006-08-11 23:03:42 +0000135};
136
Mark Sleef9831082007-02-20 20:59:21 +0000137
David Reissd779cbe2007-08-31 01:42:55 +0000138// Forward declare this structure used by TDenseProtocol
139namespace reflection { namespace local {
140struct TypeSpec;
141}}
142
David Reiss5ddabb82010-10-06 17:09:37 +0000143class TDelayedException {
144 public:
145 template <class E> static TDelayedException* delayException(const E& e);
146 virtual void throw_it() = 0;
147 virtual ~TDelayedException() {};
148};
149
150template <class E> class TExceptionWrapper : public TDelayedException {
151 public:
152 TExceptionWrapper(const E& e) : e_(e) {}
153 virtual void throw_it() {
154 E temp(e_);
155 delete this;
156 throw temp;
157 }
158 private:
159 E e_;
160};
161
162template <class E>
163TDelayedException* TDelayedException::delayException(const E& e) {
164 return new TExceptionWrapper<E>(e);
165}
David Reissd779cbe2007-08-31 01:42:55 +0000166
David Reissc3b36222010-10-06 17:10:10 +0000167#if T_GLOBAL_DEBUG_VIRTUAL > 1
168void profile_virtual_call(const std::type_info& info);
169void profile_generic_protocol(const std::type_info& template_type,
170 const std::type_info& prot_type);
171void profile_print_info(FILE *f);
172void profile_print_info();
173void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f);
174#endif
175
T Jake Lucianib5e62212009-01-31 22:36:20 +0000176}} // apache::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000177
Mark Sleef5f2be42006-09-05 21:05:31 +0000178#endif // #ifndef _THRIFT_THRIFT_H_