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_THRIFT_H_
21 #define _THRIFT_THRIFT_H_ 1
22 
23 #include <thrift/transport/PlatformSocket.h>
24 
25 #include <thrift/thrift-config.h>
26 
27 #include <stdio.h>
28 #include <assert.h>
29 
30 #include <sys/types.h>
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34 #ifdef HAVE_INTTYPES_H
35 #include <inttypes.h>
36 #endif
37 #include <string>
38 #include <map>
39 #include <list>
40 #include <set>
41 #include <vector>
42 #include <exception>
43 #include <typeinfo>
44 
45 #include <boost/utility/enable_if.hpp>
46 #include <boost/type_traits/is_convertible.hpp>
47 
48 #include <thrift/TLogging.h>
49 #include <thrift/TOutput.h>
50 
51 #define THRIFT_UNUSED_VARIABLE(x) ((void)(x))
52 
53 namespace apache {
54 namespace thrift {
55 
56 class TEnumIterator
57     : public std::iterator<std::forward_iterator_tag, std::pair<int, const char*> > {
58 public:
TEnumIterator(int n,int * enums,const char ** names)59   TEnumIterator(int n, int* enums, const char** names)
60     : ii_(0), n_(n), enums_(enums), names_(names) {}
61 
62   int operator++() { return ++ii_; }
63 
64   bool operator!=(const TEnumIterator& end) {
65     THRIFT_UNUSED_VARIABLE(end);
66     assert(end.n_ == -1);
67     return (ii_ != n_);
68   }
69 
70   std::pair<int, const char*> operator*() const { return std::make_pair(enums_[ii_], names_[ii_]); }
71 
72 private:
73   int ii_;
74   const int n_;
75   int* enums_;
76   const char** names_;
77 };
78 
79 class TException : public std::exception {
80 public:
TException()81   TException() : message_() {}
82 
TException(const std::string & message)83   TException(const std::string& message) : message_(message) {}
84 
~TException()85   virtual ~TException() throw() {}
86 
what()87   virtual const char* what() const throw() {
88     if (message_.empty()) {
89       return "Default TException.";
90     } else {
91       return message_.c_str();
92     }
93   }
94 
95 protected:
96   std::string message_;
97 };
98 
99 class TDelayedException {
100 public:
101   template <class E>
102   static TDelayedException* delayException(const E& e);
103   virtual void throw_it() = 0;
~TDelayedException()104   virtual ~TDelayedException(){};
105 };
106 
107 template <class E>
108 class TExceptionWrapper : public TDelayedException {
109 public:
TExceptionWrapper(const E & e)110   TExceptionWrapper(const E& e) : e_(e) {}
throw_it()111   virtual void throw_it() {
112     E temp(e_);
113     delete this;
114     throw temp;
115   }
116 
117 private:
118   E e_;
119 };
120 
121 template <class E>
delayException(const E & e)122 TDelayedException* TDelayedException::delayException(const E& e) {
123   return new TExceptionWrapper<E>(e);
124 }
125 
126 #if T_GLOBAL_DEBUG_VIRTUAL > 1
127 void profile_virtual_call(const std::type_info& info);
128 void profile_generic_protocol(const std::type_info& template_type, const std::type_info& prot_type);
129 void profile_print_info(FILE* f);
130 void profile_print_info();
131 void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f);
132 #endif
133 }
134 } // apache::thrift
135 
136 #endif // #ifndef _THRIFT_THRIFT_H_
137