| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
| Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_THRIFT_H_ |
| 21 | #define _THRIFT_THRIFT_H_ 1 |
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
| Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/transport/PlatformSocket.h> |
| Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 24 | |
| 25 | #include <thrift/thrift-config.h> |
| 26 | |
| David Reiss | 7247b8c | 2009-04-02 23:05:40 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 28 | #include <assert.h> |
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 29 | |
| David Reiss | 08d2f11 | 2009-05-21 02:28:36 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 31 | #ifdef HAVE_NETINET_IN_H |
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 32 | #include <netinet/in.h> |
| Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 33 | #endif |
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 34 | #ifdef HAVE_INTTYPES_H |
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | #include <inttypes.h> |
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 36 | #endif |
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 37 | #include <string> |
| 38 | #include <map> |
| 39 | #include <list> |
| 40 | #include <set> |
| Mark Slee | 4ecbebc | 2006-09-05 00:14:21 +0000 | [diff] [blame] | 41 | #include <vector> |
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 42 | #include <exception> |
| David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 43 | #include <typeinfo> |
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 44 | |
| Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 45 | #include <thrift/TLogging.h> |
| Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 46 | #include <thrift/TOutput.h> |
| Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 47 | |
| Ben Craig | 91058ef | 2013-08-29 10:38:25 -0500 | [diff] [blame] | 48 | #define THRIFT_UNUSED_VARIABLE(x) ((void)(x)) |
| Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 49 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | namespace apache { |
| 51 | namespace thrift { |
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 52 | |
| Gary Miguel | ae342c2 | 2023-04-08 21:31:57 +0000 | [diff] [blame] | 53 | class TEnumIterator { |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | public: |
| Gary Miguel | ae342c2 | 2023-04-08 21:31:57 +0000 | [diff] [blame] | 55 | using iterator_category = std::forward_iterator_tag; |
| 56 | using value_type = std::pair<int, const char*>; |
| Hasnain Lakhani | b0f2423 | 2025-08-01 20:20:13 -0700 | [diff] [blame] | 57 | using difference_type = std::ptrdiff_t; |
| 58 | using pointer = value_type*; |
| 59 | using reference = value_type&; |
| Gary Miguel | ae342c2 | 2023-04-08 21:31:57 +0000 | [diff] [blame] | 60 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 61 | TEnumIterator(int n, int* enums, const char** names) |
| 62 | : ii_(0), n_(n), enums_(enums), names_(names) {} |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 63 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | int operator++() { return ++ii_; } |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 65 | |
| africamonkey | 4f06d5e | 2022-07-06 23:55:17 +0800 | [diff] [blame] | 66 | bool operator==(const TEnumIterator& rhs) const { |
| africamonkey | 39de3ad | 2022-09-15 00:33:44 +0800 | [diff] [blame] | 67 | bool is_end = ii_ == n_ || n_ == -1; |
| 68 | bool is_rhs_end = rhs.ii_ == rhs.n_ || rhs.n_ == -1; |
| 69 | return (ii_ == rhs.ii_ && n_ == rhs.n_) || (is_end && is_rhs_end); |
| africamonkey | 4f06d5e | 2022-07-06 23:55:17 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| africamonkey | 42204e7 | 2022-10-31 21:27:38 +0800 | [diff] [blame] | 72 | bool operator!=(const TEnumIterator& rhs) const { return !(*this == rhs); } |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 73 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | std::pair<int, const char*> operator*() const { return std::make_pair(enums_[ii_], names_[ii_]); } |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 75 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | private: |
| David Reiss | 44ff76f | 2010-10-06 17:10:15 +0000 | [diff] [blame] | 77 | int ii_; |
| 78 | const int n_; |
| 79 | int* enums_; |
| 80 | const char** names_; |
| 81 | }; |
| 82 | |
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 83 | class TException : public std::exception { |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 84 | public: |
| 85 | TException() : message_() {} |
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 86 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | TException(const std::string& message) : message_(message) {} |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 88 | |
| Mario Emmenlauer | c061923 | 2020-04-08 14:39:58 +0200 | [diff] [blame] | 89 | virtual ~TException() noexcept override = default; |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 90 | |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 91 | const char* what() const noexcept override { |
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 92 | if (message_.empty()) { |
| 93 | return "Default TException."; |
| 94 | } else { |
| 95 | return message_.c_str(); |
| 96 | } |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | protected: |
| Mark Slee | 2abc9df | 2006-12-16 01:06:49 +0000 | [diff] [blame] | 100 | std::string message_; |
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 103 | class TDelayedException { |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 104 | public: |
| 105 | template <class E> |
| 106 | static TDelayedException* delayException(const E& e); |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 107 | virtual void throw_it() = 0; |
| James E. King III | f95620d | 2019-01-28 18:15:13 -0500 | [diff] [blame] | 108 | virtual ~TDelayedException() = default; |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 111 | template <class E> |
| 112 | class TExceptionWrapper : public TDelayedException { |
| 113 | public: |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 114 | TExceptionWrapper(const E& e) : e_(e) {} |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 115 | void throw_it() override { |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 116 | E temp(e_); |
| 117 | delete this; |
| 118 | throw temp; |
| 119 | } |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 120 | |
| 121 | private: |
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 122 | E e_; |
| 123 | }; |
| 124 | |
| 125 | template <class E> |
| 126 | TDelayedException* TDelayedException::delayException(const E& e) { |
| 127 | return new TExceptionWrapper<E>(e); |
| 128 | } |
| David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 129 | |
| David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 130 | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
| 131 | void profile_virtual_call(const std::type_info& info); |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 132 | void profile_generic_protocol(const std::type_info& template_type, const std::type_info& prot_type); |
| 133 | void profile_print_info(FILE* f); |
| David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 134 | void profile_print_info(); |
| 135 | void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f); |
| 136 | #endif |
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 137 | } |
| 138 | } // apache::thrift |
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 139 | |
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 140 | #endif // #ifndef _THRIFT_THRIFT_H_ |