OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Thrift.h
Go to the documentation of this file.
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  * Copyright 2006-2017 The Apache Software Foundation.
20  * https://github.com/apache/thrift
21  */
22 
23 #ifndef _SANDESH_THRIFT_H_
24 #define _SANDESH_THRIFT_H_ 1
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <stdio.h>
30 #include <assert.h>
31 
32 #include <sys/types.h>
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #ifdef HAVE_INTTYPES_H
37 #include <inttypes.h>
38 #endif
39 
43 #include <netinet/in.h>
44 #include <sys/socket.h>
45 
46 #include <string>
47 #include <map>
48 #include <list>
49 #include <set>
50 #include <vector>
51 #include <exception>
52 #include <typeinfo>
53 
54 #include <boost/utility/enable_if.hpp>
55 #include <boost/type_traits/is_convertible.hpp>
56 
72 #define THRIFT_OVERLOAD_IF_DEFN(T, Y) \
73  typename ::boost::enable_if<typename ::boost::is_convertible<T*, Y*>::type, \
74  void*>::type
75 
76 #define THRIFT_OVERLOAD_IF(T, Y) \
77  THRIFT_OVERLOAD_IF_DEFN(T, Y) = NULL
78 
79 namespace contrail { namespace sandesh {
80 
81 class TEnumIterator : public std::iterator<std::forward_iterator_tag, std::pair<int, const char*> > {
82  public:
84  int* enums,
85  const char** names) :
86  ii_(0), n_(n), enums_(enums), names_(names) {
87  }
88 
89  int operator ++() {
90  return ++ii_;
91  }
92 
93  bool operator !=(const TEnumIterator& end) {
94  assert(end.n_ == -1);
95  return (ii_ != n_);
96  }
97 
98  bool operator ==(const TEnumIterator& end) {
99  return !(operator !=(end));
100  }
101 
102  std::pair<int, const char*> operator*() const {
103  return std::make_pair(enums_[ii_], names_[ii_]);
104  }
105 
106  private:
107  int ii_;
108  const int n_;
109  int* enums_;
110  const char** names_;
111 };
112 
113 class TOutput {
114  public:
116 
117  inline void setOutputFunction(void (*function)(const char *)){
118  f_ = function;
119  }
120 
121  inline void operator()(const char *message){
122  f_(message);
123  }
124 
125  // It is important to have a const char* overload here instead of
126  // just the string version, otherwise errno could be corrupted
127  // if there is some problem allocating memory when constructing
128  // the string.
129  void perror(const char *message, int errno_copy);
130  inline void perror(const std::string &message, int errno_copy) {
131  perror(message.c_str(), errno_copy);
132  }
133 
134  void printf(const char *message, ...);
135 
136  inline static void errorTimeWrapper(const char* msg) {
137  time_t now;
138  char dbgtime[26];
139  time(&now);
140  ctime_r(&now, dbgtime);
141  dbgtime[24] = 0;
142  fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
143  }
144 
146  static std::string strerror_s(int errno_copy);
147 
148  private:
149  void (*f_)(const char *);
150 };
151 
152 extern TOutput GlobalOutput;
153 #if defined(__EXCEPTIONS)
154 class TException : public std::exception {
155  public:
156  TException():
157  message_() {}
158 
159  TException(const std::string& message) :
160  message_(message) {}
161 
162  virtual ~TException() throw() {}
163 
164  virtual const char* what() const throw() {
165  if (message_.empty()) {
166  return "Default TException.";
167  } else {
168  return message_.c_str();
169  }
170  }
171 
172  protected:
173  std::string message_;
174 
175 };
176 #endif
177 
178 // Forward declare this structure used by TDenseProtocol
179 namespace reflection { namespace local {
180 struct TypeSpec;
181 }}
182 
183 #if defined(__EXCEPTIONS)
184 class TDelayedException {
185  public:
186  template <class E> static TDelayedException* delayException(const E& e);
187  virtual void throw_it() = 0;
188  virtual ~TDelayedException() {};
189 };
190 
191 template <class E> class TExceptionWrapper : public TDelayedException {
192  public:
193  TExceptionWrapper(const E& e) : e_(e) {}
194  virtual void throw_it() {
195  E temp(e_);
196  delete this;
197  throw temp;
198  }
199  private:
200  E e_;
201 };
202 
203 template <class E>
204 TDelayedException* TDelayedException::delayException(const E& e) {
205  return new TExceptionWrapper<E>(e);
206 }
207 #endif
208 
209 #if T_GLOBAL_DEBUG_VIRTUAL > 1
210 void profile_virtual_call(const std::type_info& info);
211 void profile_generic_protocol(const std::type_info& template_type,
212  const std::type_info& prot_type);
213 void profile_print_info(FILE *f);
214 void profile_print_info();
215 void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f);
216 #endif
217 
218 }} // contrail::sandesh
219 
220 #endif // #ifndef _SANDESH_THRIFT_H_
static void errorTimeWrapper(const char *msg)
Definition: Thrift.h:136
bool operator!=(const TEnumIterator &end)
Definition: Thrift.h:93
bool operator==(const TEnumIterator &end)
Definition: Thrift.h:98
void(* f_)(const char *)
Definition: Thrift.h:149
TEnumIterator(int n, int *enums, const char **names)
Definition: Thrift.h:83
void setOutputFunction(void(*function)(const char *))
Definition: Thrift.h:117
void perror(const char *message, int errno_copy)
std::pair< int, const char * > operator*() const
Definition: Thrift.h:102
void perror(const std::string &message, int errno_copy)
Definition: Thrift.h:130
static std::string strerror_s(int errno_copy)
void printf(const char *message,...)
void operator()(const char *message)
Definition: Thrift.h:121