throw_delegate.cc (4978B)
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> 21 22 #include "absl/base/config.h" 23 #include "absl/base/internal/raw_logging.h" 24 25 namespace absl { 26 ABSL_NAMESPACE_BEGIN 27 namespace base_internal { 28 29 #ifdef ABSL_HAVE_EXCEPTIONS 30 static_assert(false, "Mozilla - ABSL_HAVE_EXCEPTIONS has been turned on"); 31 #endif 32 33 // NOTE: The exception types, like `std::logic_error`, do not exist on all 34 // platforms. (For example, the Android NDK does not have them.) 35 // Therefore, their use must be guarded by `#ifdef` or equivalent. 36 37 void ThrowStdLogicError(const std::string& what_arg) { 38 #ifdef ABSL_HAVE_EXCEPTIONS 39 throw std::logic_error(what_arg); 40 #else 41 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 42 std::abort(); 43 #endif 44 } 45 void ThrowStdLogicError(const char* what_arg) { 46 #ifdef ABSL_HAVE_EXCEPTIONS 47 throw std::logic_error(what_arg); 48 #else 49 ABSL_RAW_LOG(FATAL, "%s", what_arg); 50 std::abort(); 51 #endif 52 } 53 void ThrowStdInvalidArgument(const std::string& what_arg) { 54 #ifdef ABSL_HAVE_EXCEPTIONS 55 throw std::invalid_argument(what_arg); 56 #else 57 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 58 std::abort(); 59 #endif 60 } 61 void ThrowStdInvalidArgument(const char* what_arg) { 62 #ifdef ABSL_HAVE_EXCEPTIONS 63 throw std::invalid_argument(what_arg); 64 #else 65 ABSL_RAW_LOG(FATAL, "%s", what_arg); 66 std::abort(); 67 #endif 68 } 69 70 void ThrowStdDomainError(const std::string& what_arg) { 71 #ifdef ABSL_HAVE_EXCEPTIONS 72 throw std::domain_error(what_arg); 73 #else 74 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 75 std::abort(); 76 #endif 77 } 78 void ThrowStdDomainError(const char* what_arg) { 79 #ifdef ABSL_HAVE_EXCEPTIONS 80 throw std::domain_error(what_arg); 81 #else 82 ABSL_RAW_LOG(FATAL, "%s", what_arg); 83 std::abort(); 84 #endif 85 } 86 87 void ThrowStdLengthError(const std::string& what_arg) { 88 #ifdef ABSL_HAVE_EXCEPTIONS 89 throw std::length_error(what_arg); 90 #else 91 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 92 std::abort(); 93 #endif 94 } 95 void ThrowStdLengthError(const char* what_arg) { 96 #ifdef ABSL_HAVE_EXCEPTIONS 97 throw std::length_error(what_arg); 98 #else 99 ABSL_RAW_LOG(FATAL, "%s", what_arg); 100 std::abort(); 101 #endif 102 } 103 104 void ThrowStdOutOfRange(const std::string& what_arg) { 105 #ifdef ABSL_HAVE_EXCEPTIONS 106 throw std::out_of_range(what_arg); 107 #else 108 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 109 std::abort(); 110 #endif 111 } 112 void ThrowStdOutOfRange(const char* what_arg) { 113 #ifdef ABSL_HAVE_EXCEPTIONS 114 throw std::out_of_range(what_arg); 115 #else 116 ABSL_RAW_LOG(FATAL, "%s", what_arg); 117 std::abort(); 118 #endif 119 } 120 121 void ThrowStdRuntimeError(const std::string& what_arg) { 122 #ifdef ABSL_HAVE_EXCEPTIONS 123 throw std::runtime_error(what_arg); 124 #else 125 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 126 std::abort(); 127 #endif 128 } 129 void ThrowStdRuntimeError(const char* what_arg) { 130 #ifdef ABSL_HAVE_EXCEPTIONS 131 throw std::runtime_error(what_arg); 132 #else 133 ABSL_RAW_LOG(FATAL, "%s", what_arg); 134 std::abort(); 135 #endif 136 } 137 138 void ThrowStdRangeError(const std::string& what_arg) { 139 #ifdef ABSL_HAVE_EXCEPTIONS 140 throw std::range_error(what_arg); 141 #else 142 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 143 std::abort(); 144 #endif 145 } 146 void ThrowStdRangeError(const char* what_arg) { 147 #ifdef ABSL_HAVE_EXCEPTIONS 148 throw std::range_error(what_arg); 149 #else 150 ABSL_RAW_LOG(FATAL, "%s", what_arg); 151 std::abort(); 152 #endif 153 } 154 155 void ThrowStdOverflowError(const std::string& what_arg) { 156 #ifdef ABSL_HAVE_EXCEPTIONS 157 throw std::overflow_error(what_arg); 158 #else 159 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 160 std::abort(); 161 #endif 162 } 163 void ThrowStdOverflowError(const char* what_arg) { 164 #ifdef ABSL_HAVE_EXCEPTIONS 165 throw std::overflow_error(what_arg); 166 #else 167 ABSL_RAW_LOG(FATAL, "%s", what_arg); 168 std::abort(); 169 #endif 170 } 171 172 void ThrowStdUnderflowError(const std::string& what_arg) { 173 #ifdef ABSL_HAVE_EXCEPTIONS 174 throw std::underflow_error(what_arg); 175 #else 176 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str()); 177 std::abort(); 178 #endif 179 } 180 void ThrowStdUnderflowError(const char* what_arg) { 181 #ifdef ABSL_HAVE_EXCEPTIONS 182 throw std::underflow_error(what_arg); 183 #else 184 ABSL_RAW_LOG(FATAL, "%s", what_arg); 185 std::abort(); 186 #endif 187 } 188 189 void ThrowStdBadFunctionCall() { 190 #ifdef ABSL_HAVE_EXCEPTIONS 191 throw std::bad_function_call(); 192 #else 193 std::abort(); 194 #endif 195 } 196 197 void ThrowStdBadAlloc() { 198 #ifdef ABSL_HAVE_EXCEPTIONS 199 throw std::bad_alloc(); 200 #else 201 std::abort(); 202 #endif 203 } 204 205 } // namespace base_internal 206 ABSL_NAMESPACE_END 207 } // namespace absl