Thrift.h 3.3 KB

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