Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // 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 Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 21 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 22 | #include "absl/base/config.h" |
| 23 | #include "absl/base/internal/raw_logging.h" |
| 24 | |
| 25 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 26 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 27 | namespace base_internal { |
| 28 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 29 | // 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 Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 33 | namespace { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 34 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 35 | template <typename T> |
| 36 | [[noreturn]] void Throw(const T& error) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 37 | throw error; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 38 | } |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 39 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | void ThrowStdLogicError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 43 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 44 | Throw(std::logic_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 45 | #else |
| 46 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 47 | std::abort(); |
| 48 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 49 | } |
| 50 | void ThrowStdLogicError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 51 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 52 | Throw(std::logic_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 53 | #else |
| 54 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 55 | std::abort(); |
| 56 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 57 | } |
| 58 | void ThrowStdInvalidArgument(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 59 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 60 | Throw(std::invalid_argument(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 61 | #else |
| 62 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 63 | std::abort(); |
| 64 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 65 | } |
| 66 | void ThrowStdInvalidArgument(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 67 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 68 | Throw(std::invalid_argument(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 69 | #else |
| 70 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 71 | std::abort(); |
| 72 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void ThrowStdDomainError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 76 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 77 | Throw(std::domain_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 78 | #else |
| 79 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 80 | std::abort(); |
| 81 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 82 | } |
| 83 | void ThrowStdDomainError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 84 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 85 | Throw(std::domain_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 86 | #else |
| 87 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 88 | std::abort(); |
| 89 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ThrowStdLengthError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 93 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 94 | Throw(std::length_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 95 | #else |
| 96 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 97 | std::abort(); |
| 98 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 99 | } |
| 100 | void ThrowStdLengthError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 101 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 102 | Throw(std::length_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 103 | #else |
| 104 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 105 | std::abort(); |
| 106 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void ThrowStdOutOfRange(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 110 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 111 | Throw(std::out_of_range(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 112 | #else |
| 113 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 114 | std::abort(); |
| 115 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 116 | } |
| 117 | void ThrowStdOutOfRange(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 118 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 119 | Throw(std::out_of_range(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 120 | #else |
| 121 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 122 | std::abort(); |
| 123 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void ThrowStdRuntimeError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 127 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 128 | Throw(std::runtime_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 129 | #else |
| 130 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 131 | std::abort(); |
| 132 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 133 | } |
| 134 | void ThrowStdRuntimeError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 135 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 136 | Throw(std::runtime_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 137 | #else |
| 138 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 139 | std::abort(); |
| 140 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void ThrowStdRangeError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 144 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 145 | Throw(std::range_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 146 | #else |
| 147 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 148 | std::abort(); |
| 149 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 150 | } |
| 151 | void ThrowStdRangeError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 152 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 153 | Throw(std::range_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 154 | #else |
| 155 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 156 | std::abort(); |
| 157 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ThrowStdOverflowError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 161 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 162 | Throw(std::overflow_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 163 | #else |
| 164 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 165 | std::abort(); |
| 166 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 167 | } |
| 168 | void ThrowStdOverflowError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 169 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 170 | Throw(std::overflow_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 171 | #else |
| 172 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 173 | std::abort(); |
| 174 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void ThrowStdUnderflowError(const std::string& what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 178 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 179 | Throw(std::underflow_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 180 | #else |
| 181 | ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); |
| 182 | std::abort(); |
| 183 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 184 | } |
| 185 | void ThrowStdUnderflowError(const char* what_arg) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 186 | #ifdef ABSL_HAVE_EXCEPTIONS |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 187 | Throw(std::underflow_error(what_arg)); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 188 | #else |
| 189 | ABSL_RAW_LOG(FATAL, "%s", what_arg); |
| 190 | std::abort(); |
| 191 | #endif |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 194 | void ThrowStdBadFunctionCall() { |
| 195 | #ifdef ABSL_HAVE_EXCEPTIONS |
| 196 | Throw(std::bad_function_call()); |
| 197 | #else |
| 198 | std::abort(); |
| 199 | #endif |
| 200 | } |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 201 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 202 | void ThrowStdBadAlloc() { |
| 203 | #ifdef ABSL_HAVE_EXCEPTIONS |
| 204 | Throw(std::bad_alloc()); |
| 205 | #else |
| 206 | std::abort(); |
| 207 | #endif |
| 208 | } |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 209 | |
| 210 | } // namespace base_internal |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 211 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 212 | } // namespace absl |