David Reiss | ea2cba8 | 2009-03-30 21:35:00 +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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_THRIFT_H_ |
| 21 | #define _THRIFT_THRIFT_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Roger Meier | 8cd3b1a | 2011-09-23 14:25:22 +0000 | [diff] [blame^] | 23 | #ifdef _WIN32 |
| 24 | #include "windows/config.h" |
| 25 | #endif |
| 26 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include "config.h" |
| 29 | #endif |
David Reiss | 7247b8c | 2009-04-02 23:05:40 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 31 | #include <assert.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 32 | |
David Reiss | 08d2f11 | 2009-05-21 02:28:36 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 34 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | #include <netinet/in.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 36 | #endif |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 37 | #ifdef HAVE_INTTYPES_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 38 | #include <inttypes.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 39 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 40 | #include <string> |
| 41 | #include <map> |
| 42 | #include <list> |
| 43 | #include <set> |
Mark Slee | 4ecbebc | 2006-09-05 00:14:21 +0000 | [diff] [blame] | 44 | #include <vector> |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 45 | #include <exception> |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 46 | #include <typeinfo> |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 47 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 48 | #include <boost/utility/enable_if.hpp> |
| 49 | #include <boost/type_traits/is_convertible.hpp> |
| 50 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 51 | #include "TLogging.h" |
| 52 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Helper macros to allow function overloading even when using |
| 55 | * boost::shared_ptr. |
| 56 | * |
| 57 | * shared_ptr makes overloading really annoying, since shared_ptr defines |
| 58 | * constructor methods to allow one shared_ptr type to be constructed from any |
| 59 | * other shared_ptr type. (Even if it would be a compile error to actually try |
| 60 | * to instantiate the constructor.) These macros add an extra argument to the |
| 61 | * function to cause it to only be instantiated if a pointer of type T is |
| 62 | * convertible to a pointer of type U. |
| 63 | * |
| 64 | * THRIFT_OVERLOAD_IF should be used in function declarations. |
| 65 | * THRIFT_OVERLOAD_IF_DEFN should be used in the function definition, if it is |
| 66 | * defined separately from where it is declared. |
| 67 | */ |
| 68 | #define THRIFT_OVERLOAD_IF_DEFN(T, Y) \ |
| 69 | typename ::boost::enable_if<typename ::boost::is_convertible<T*, Y*>::type, \ |
| 70 | void*>::type |
| 71 | |
| 72 | #define THRIFT_OVERLOAD_IF(T, Y) \ |
| 73 | THRIFT_OVERLOAD_IF_DEFN(T, Y) = NULL |
| 74 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 75 | namespace apache { namespace thrift { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 76 | |
Roger Meier | 330b5ae | 2011-04-18 19:46:02 +0000 | [diff] [blame] | 77 | class TEnumIterator : public std::iterator<std::forward_iterator_tag, std::pair<int, const char*> > { |
David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 78 | public: |
| 79 | TEnumIterator(int n, |
| 80 | int* enums, |
| 81 | const char** names) : |
| 82 | ii_(0), n_(n), enums_(enums), names_(names) { |
| 83 | } |
| 84 | |
| 85 | int operator ++() { |
| 86 | return ++ii_; |
| 87 | } |
| 88 | |
| 89 | bool operator !=(const TEnumIterator& end) { |
| 90 | assert(end.n_ == -1); |
| 91 | return (ii_ != n_); |
| 92 | } |
| 93 | |
| 94 | std::pair<int, const char*> operator*() const { |
| 95 | return std::make_pair(enums_[ii_], names_[ii_]); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | int ii_; |
| 100 | const int n_; |
| 101 | int* enums_; |
| 102 | const char** names_; |
| 103 | }; |
| 104 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 105 | class TOutput { |
| 106 | public: |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 107 | TOutput() : f_(&errorTimeWrapper) {} |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 108 | |
| 109 | inline void setOutputFunction(void (*function)(const char *)){ |
| 110 | f_ = function; |
| 111 | } |
| 112 | |
| 113 | inline void operator()(const char *message){ |
| 114 | f_(message); |
| 115 | } |
| 116 | |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 117 | // It is important to have a const char* overload here instead of |
| 118 | // just the string version, otherwise errno could be corrupted |
| 119 | // if there is some problem allocating memory when constructing |
| 120 | // the string. |
| 121 | void perror(const char *message, int errno_copy); |
| 122 | inline void perror(const std::string &message, int errno_copy) { |
| 123 | perror(message.c_str(), errno_copy); |
| 124 | } |
| 125 | |
| 126 | void printf(const char *message, ...); |
| 127 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 128 | inline static void errorTimeWrapper(const char* msg) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 129 | time_t now; |
David Reiss | d28ce10 | 2009-05-21 02:28:14 +0000 | [diff] [blame] | 130 | char dbgtime[26]; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 131 | time(&now); |
| 132 | ctime_r(&now, dbgtime); |
| 133 | dbgtime[24] = 0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 134 | fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 135 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 136 | |
| 137 | /** Just like strerror_r but returns a C++ string object. */ |
| 138 | static std::string strerror_s(int errno_copy); |
| 139 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 140 | private: |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 141 | void (*f_)(const char *); |
| 142 | }; |
| 143 | |
| 144 | extern TOutput GlobalOutput; |
| 145 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 146 | class TException : public std::exception { |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 147 | public: |
Roger Meier | 178f8f2 | 2010-10-25 12:36:04 +0000 | [diff] [blame] | 148 | TException(): |
| 149 | message_() {} |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 150 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 151 | TException(const std::string& message) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 152 | message_(message) {} |
| 153 | |
Martin Kraemer | 8196a61 | 2006-12-09 01:57:58 +0000 | [diff] [blame] | 154 | virtual ~TException() throw() {} |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 155 | |
Martin Kraemer | 92a2eac | 2007-02-05 20:58:41 +0000 | [diff] [blame] | 156 | virtual const char* what() const throw() { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 157 | if (message_.empty()) { |
| 158 | return "Default TException."; |
| 159 | } else { |
| 160 | return message_.c_str(); |
| 161 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 162 | } |
| 163 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 164 | protected: |
Mark Slee | 2abc9df | 2006-12-16 01:06:49 +0000 | [diff] [blame] | 165 | std::string message_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 166 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 169 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 170 | // Forward declare this structure used by TDenseProtocol |
| 171 | namespace reflection { namespace local { |
| 172 | struct TypeSpec; |
| 173 | }} |
| 174 | |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 175 | class TDelayedException { |
| 176 | public: |
| 177 | template <class E> static TDelayedException* delayException(const E& e); |
| 178 | virtual void throw_it() = 0; |
| 179 | virtual ~TDelayedException() {}; |
| 180 | }; |
| 181 | |
| 182 | template <class E> class TExceptionWrapper : public TDelayedException { |
| 183 | public: |
| 184 | TExceptionWrapper(const E& e) : e_(e) {} |
| 185 | virtual void throw_it() { |
| 186 | E temp(e_); |
| 187 | delete this; |
| 188 | throw temp; |
| 189 | } |
| 190 | private: |
| 191 | E e_; |
| 192 | }; |
| 193 | |
| 194 | template <class E> |
| 195 | TDelayedException* TDelayedException::delayException(const E& e) { |
| 196 | return new TExceptionWrapper<E>(e); |
| 197 | } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 198 | |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 199 | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
| 200 | void profile_virtual_call(const std::type_info& info); |
| 201 | void profile_generic_protocol(const std::type_info& template_type, |
| 202 | const std::type_info& prot_type); |
| 203 | void profile_print_info(FILE *f); |
| 204 | void profile_print_info(); |
| 205 | void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f); |
| 206 | #endif |
| 207 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 208 | }} // apache::thrift |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 209 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 210 | #endif // #ifndef _THRIFT_THRIFT_H_ |