/src/thrift/lib/cpp/src/thrift/Thrift.h
Line | Count | Source |
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 <thrift/TLogging.h> |
46 | | #include <thrift/TOutput.h> |
47 | | |
48 | 0 | #define THRIFT_UNUSED_VARIABLE(x) ((void)(x)) |
49 | | |
50 | | namespace apache { |
51 | | namespace thrift { |
52 | | |
53 | | class TEnumIterator { |
54 | | public: |
55 | | using iterator_category = std::forward_iterator_tag; |
56 | | using value_type = std::pair<int, const char*>; |
57 | | using difference_type = std::ptrdiff_t; |
58 | | using pointer = value_type*; |
59 | | using reference = value_type&; |
60 | | |
61 | | TEnumIterator(int n, int* enums, const char** names) |
62 | 4 | : ii_(0), n_(n), enums_(enums), names_(names) {} |
63 | | |
64 | 12 | int operator++() { return ++ii_; } |
65 | | |
66 | 14 | bool operator==(const TEnumIterator& rhs) const { |
67 | 14 | bool is_end = ii_ == n_ || n_ == -1; |
68 | 14 | bool is_rhs_end = rhs.ii_ == rhs.n_ || rhs.n_ == -1; |
69 | 14 | return (ii_ == rhs.ii_ && n_ == rhs.n_) || (is_end && is_rhs_end); |
70 | 14 | } |
71 | | |
72 | 14 | bool operator!=(const TEnumIterator& rhs) const { return !(*this == rhs); } |
73 | | |
74 | 12 | std::pair<int, const char*> operator*() const { return std::make_pair(enums_[ii_], names_[ii_]); } |
75 | | |
76 | | private: |
77 | | int ii_; |
78 | | const int n_; |
79 | | int* enums_; |
80 | | const char** names_; |
81 | | }; |
82 | | |
83 | | class TException : public std::exception { |
84 | | public: |
85 | 382 | TException() : message_() {} |
86 | | |
87 | 2.13k | TException(const std::string& message) : message_(message) {} |
88 | | |
89 | 2.51k | virtual ~TException() noexcept override = default; |
90 | | |
91 | 0 | const char* what() const noexcept override { |
92 | 0 | if (message_.empty()) { |
93 | 0 | return "Default TException."; |
94 | 0 | } else { |
95 | 0 | return message_.c_str(); |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | protected: |
100 | | std::string message_; |
101 | | }; |
102 | | |
103 | | class TDelayedException { |
104 | | public: |
105 | | template <class E> |
106 | | static TDelayedException* delayException(const E& e); |
107 | | virtual void throw_it() = 0; |
108 | | virtual ~TDelayedException() = default; |
109 | | }; |
110 | | |
111 | | template <class E> |
112 | | class TExceptionWrapper : public TDelayedException { |
113 | | public: |
114 | | TExceptionWrapper(const E& e) : e_(e) {} |
115 | | void throw_it() override { |
116 | | E temp(e_); |
117 | | delete this; |
118 | | throw temp; |
119 | | } |
120 | | |
121 | | private: |
122 | | E e_; |
123 | | }; |
124 | | |
125 | | template <class E> |
126 | | TDelayedException* TDelayedException::delayException(const E& e) { |
127 | | return new TExceptionWrapper<E>(e); |
128 | | } |
129 | | |
130 | | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
131 | | void profile_virtual_call(const std::type_info& info); |
132 | | void profile_generic_protocol(const std::type_info& template_type, const std::type_info& prot_type); |
133 | | void profile_print_info(FILE* f); |
134 | | void profile_print_info(); |
135 | | void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f); |
136 | | #endif |
137 | | } |
138 | | } // apache::thrift |
139 | | |
140 | | #endif // #ifndef _THRIFT_THRIFT_H_ |