blob: c260ff1eed636d89be1a98f6dd1c3e6092285acb [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "absl/base/internal/throw_delegate.h"
16
17#include <cstdlib>
18#include <functional>
19#include <new>
20#include <stdexcept>
Austin Schuhb4691e92020-12-31 12:37:18 -080021
Austin Schuh36244a12019-09-21 17:52:38 -070022#include "absl/base/config.h"
23#include "absl/base/internal/raw_logging.h"
24
25namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080026ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070027namespace base_internal {
28
Austin Schuhb4691e92020-12-31 12:37:18 -080029// NOTE: The various STL exception throwing functions are placed within the
30// #ifdef blocks so the symbols aren't exposed on platforms that don't support
31// them, such as the Android NDK. For example, ANGLE fails to link when building
32// within AOSP without them, since the STL functions don't exist.
Austin Schuh36244a12019-09-21 17:52:38 -070033namespace {
Austin Schuhb4691e92020-12-31 12:37:18 -080034#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070035template <typename T>
36[[noreturn]] void Throw(const T& error) {
Austin Schuh36244a12019-09-21 17:52:38 -070037 throw error;
Austin Schuh36244a12019-09-21 17:52:38 -070038}
Austin Schuhb4691e92020-12-31 12:37:18 -080039#endif
Austin Schuh36244a12019-09-21 17:52:38 -070040} // namespace
41
42void ThrowStdLogicError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080043#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070044 Throw(std::logic_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080045#else
46 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
47 std::abort();
48#endif
Austin Schuh36244a12019-09-21 17:52:38 -070049}
50void ThrowStdLogicError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080051#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070052 Throw(std::logic_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080053#else
54 ABSL_RAW_LOG(FATAL, "%s", what_arg);
55 std::abort();
56#endif
Austin Schuh36244a12019-09-21 17:52:38 -070057}
58void ThrowStdInvalidArgument(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080059#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070060 Throw(std::invalid_argument(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080061#else
62 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
63 std::abort();
64#endif
Austin Schuh36244a12019-09-21 17:52:38 -070065}
66void ThrowStdInvalidArgument(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080067#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070068 Throw(std::invalid_argument(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080069#else
70 ABSL_RAW_LOG(FATAL, "%s", what_arg);
71 std::abort();
72#endif
Austin Schuh36244a12019-09-21 17:52:38 -070073}
74
75void ThrowStdDomainError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080076#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070077 Throw(std::domain_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080078#else
79 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
80 std::abort();
81#endif
Austin Schuh36244a12019-09-21 17:52:38 -070082}
83void ThrowStdDomainError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080084#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070085 Throw(std::domain_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080086#else
87 ABSL_RAW_LOG(FATAL, "%s", what_arg);
88 std::abort();
89#endif
Austin Schuh36244a12019-09-21 17:52:38 -070090}
91
92void ThrowStdLengthError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -080093#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -070094 Throw(std::length_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -080095#else
96 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
97 std::abort();
98#endif
Austin Schuh36244a12019-09-21 17:52:38 -070099}
100void ThrowStdLengthError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800101#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700102 Throw(std::length_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800103#else
104 ABSL_RAW_LOG(FATAL, "%s", what_arg);
105 std::abort();
106#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700107}
108
109void ThrowStdOutOfRange(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800110#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700111 Throw(std::out_of_range(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800112#else
113 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
114 std::abort();
115#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700116}
117void ThrowStdOutOfRange(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800118#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700119 Throw(std::out_of_range(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800120#else
121 ABSL_RAW_LOG(FATAL, "%s", what_arg);
122 std::abort();
123#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700124}
125
126void ThrowStdRuntimeError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800127#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700128 Throw(std::runtime_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800129#else
130 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
131 std::abort();
132#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700133}
134void ThrowStdRuntimeError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800135#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700136 Throw(std::runtime_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800137#else
138 ABSL_RAW_LOG(FATAL, "%s", what_arg);
139 std::abort();
140#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700141}
142
143void ThrowStdRangeError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800144#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700145 Throw(std::range_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800146#else
147 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
148 std::abort();
149#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700150}
151void ThrowStdRangeError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800152#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700153 Throw(std::range_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800154#else
155 ABSL_RAW_LOG(FATAL, "%s", what_arg);
156 std::abort();
157#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700158}
159
160void ThrowStdOverflowError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800161#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700162 Throw(std::overflow_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800163#else
164 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
165 std::abort();
166#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700167}
168void ThrowStdOverflowError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800169#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700170 Throw(std::overflow_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800171#else
172 ABSL_RAW_LOG(FATAL, "%s", what_arg);
173 std::abort();
174#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700175}
176
177void ThrowStdUnderflowError(const std::string& what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800178#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700179 Throw(std::underflow_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800180#else
181 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
182 std::abort();
183#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700184}
185void ThrowStdUnderflowError(const char* what_arg) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800186#ifdef ABSL_HAVE_EXCEPTIONS
Austin Schuh36244a12019-09-21 17:52:38 -0700187 Throw(std::underflow_error(what_arg));
Austin Schuhb4691e92020-12-31 12:37:18 -0800188#else
189 ABSL_RAW_LOG(FATAL, "%s", what_arg);
190 std::abort();
191#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700192}
193
Austin Schuhb4691e92020-12-31 12:37:18 -0800194void ThrowStdBadFunctionCall() {
195#ifdef ABSL_HAVE_EXCEPTIONS
196 Throw(std::bad_function_call());
197#else
198 std::abort();
199#endif
200}
Austin Schuh36244a12019-09-21 17:52:38 -0700201
Austin Schuhb4691e92020-12-31 12:37:18 -0800202void ThrowStdBadAlloc() {
203#ifdef ABSL_HAVE_EXCEPTIONS
204 Throw(std::bad_alloc());
205#else
206 std::abort();
207#endif
208}
Austin Schuh36244a12019-09-21 17:52:38 -0700209
210} // namespace base_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800211ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700212} // namespace absl