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