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