blob: 8c330f98ad58d85bdb6397b03095d48e19efc734 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_THRIFT_H_
21#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Roger Meier4285ba22013-06-10 21:17:23 +020023#include <thrift/transport/PlatformSocket.h>
Konrad Grochowski9be4e682013-06-22 22:03:31 +020024
25#include <thrift/thrift-config.h>
26
David Reiss7247b8c2009-04-02 23:05:40 +000027#include <stdio.h>
David Reiss44ff76f2010-10-06 17:10:15 +000028#include <assert.h>
Mark Slee4f261c52007-04-13 00:33:24 +000029
David Reiss08d2f112009-05-21 02:28:36 +000030#include <sys/types.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000031#ifdef HAVE_NETINET_IN_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000032#include <netinet/in.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000033#endif
Mark Slee4f261c52007-04-13 00:33:24 +000034#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000035#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000036#endif
Mark Sleee8540632006-05-30 09:24:40 +000037#include <string>
38#include <map>
39#include <list>
40#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000041#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000042#include <exception>
David Reissc3b36222010-10-06 17:10:10 +000043#include <typeinfo>
Marc Slemko5b126d62006-08-11 23:03:42 +000044
Roger Meier4285ba22013-06-10 21:17:23 +020045#include <thrift/TLogging.h>
Konrad Grochowski24ea0bf2015-05-07 14:59:29 +020046#include <thrift/TOutput.h>
Bryan Duxbury7a9fb812011-09-01 18:31:53 +000047
Ben Craig91058ef2013-08-29 10:38:25 -050048#define THRIFT_UNUSED_VARIABLE(x) ((void)(x))
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040049
Konrad Grochowski16a23a62014-11-13 15:33:38 +010050namespace apache {
51namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000052
Gary Miguelae342c22023-04-08 21:31:57 +000053class TEnumIterator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054public:
Gary Miguelae342c22023-04-08 21:31:57 +000055 using iterator_category = std::forward_iterator_tag;
56 using value_type = std::pair<int, const char*>;
Hasnain Lakhanib0f24232025-08-01 20:20:13 -070057 using difference_type = std::ptrdiff_t;
58 using pointer = value_type*;
59 using reference = value_type&;
Gary Miguelae342c22023-04-08 21:31:57 +000060
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061 TEnumIterator(int n, int* enums, const char** names)
62 : ii_(0), n_(n), enums_(enums), names_(names) {}
David Reiss44ff76f2010-10-06 17:10:15 +000063
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 int operator++() { return ++ii_; }
David Reiss44ff76f2010-10-06 17:10:15 +000065
africamonkey4f06d5e2022-07-06 23:55:17 +080066 bool operator==(const TEnumIterator& rhs) const {
africamonkey39de3ad2022-09-15 00:33:44 +080067 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);
africamonkey4f06d5e2022-07-06 23:55:17 +080070 }
71
africamonkey42204e72022-10-31 21:27:38 +080072 bool operator!=(const TEnumIterator& rhs) const { return !(*this == rhs); }
David Reiss44ff76f2010-10-06 17:10:15 +000073
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074 std::pair<int, const char*> operator*() const { return std::make_pair(enums_[ii_], names_[ii_]); }
David Reiss44ff76f2010-10-06 17:10:15 +000075
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076private:
David Reiss44ff76f2010-10-06 17:10:15 +000077 int ii_;
78 const int n_;
79 int* enums_;
80 const char** names_;
81};
82
Mark Sleeb9ff32a2006-11-16 01:00:24 +000083class TException : public std::exception {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084public:
85 TException() : message_() {}
Mark Sleeb9ff32a2006-11-16 01:00:24 +000086
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 TException(const std::string& message) : message_(message) {}
Mark Slee2f6404d2006-10-10 01:37:40 +000088
Mario Emmenlauerc0619232020-04-08 14:39:58 +020089 virtual ~TException() noexcept override = default;
Mark Slee2f6404d2006-10-10 01:37:40 +000090
Sebastian Zenker042580f2019-01-29 15:48:12 +010091 const char* what() const noexcept override {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000092 if (message_.empty()) {
93 return "Default TException.";
94 } else {
95 return message_.c_str();
96 }
Mark Slee2f6404d2006-10-10 01:37:40 +000097 }
98
Konrad Grochowski16a23a62014-11-13 15:33:38 +010099protected:
Mark Slee2abc9df2006-12-16 01:06:49 +0000100 std::string message_;
Marc Slemko5b126d62006-08-11 23:03:42 +0000101};
102
David Reiss5ddabb82010-10-06 17:09:37 +0000103class TDelayedException {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100104public:
105 template <class E>
106 static TDelayedException* delayException(const E& e);
David Reiss5ddabb82010-10-06 17:09:37 +0000107 virtual void throw_it() = 0;
James E. King IIIf95620d2019-01-28 18:15:13 -0500108 virtual ~TDelayedException() = default;
David Reiss5ddabb82010-10-06 17:09:37 +0000109};
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111template <class E>
112class TExceptionWrapper : public TDelayedException {
113public:
David Reiss5ddabb82010-10-06 17:09:37 +0000114 TExceptionWrapper(const E& e) : e_(e) {}
Sebastian Zenker042580f2019-01-29 15:48:12 +0100115 void throw_it() override {
David Reiss5ddabb82010-10-06 17:09:37 +0000116 E temp(e_);
117 delete this;
118 throw temp;
119 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100120
121private:
David Reiss5ddabb82010-10-06 17:09:37 +0000122 E e_;
123};
124
125template <class E>
126TDelayedException* TDelayedException::delayException(const E& e) {
127 return new TExceptionWrapper<E>(e);
128}
David Reissd779cbe2007-08-31 01:42:55 +0000129
David Reissc3b36222010-10-06 17:10:10 +0000130#if T_GLOBAL_DEBUG_VIRTUAL > 1
131void profile_virtual_call(const std::type_info& info);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100132void profile_generic_protocol(const std::type_info& template_type, const std::type_info& prot_type);
133void profile_print_info(FILE* f);
David Reissc3b36222010-10-06 17:10:10 +0000134void profile_print_info();
135void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f);
136#endif
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137}
138} // apache::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000139
Mark Sleef5f2be42006-09-05 21:05:31 +0000140#endif // #ifndef _THRIFT_THRIFT_H_