throw_delegate.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "absl/base/internal/throw_delegate.h"
  15. #include <cstdlib>
  16. #include <functional>
  17. #include <new>
  18. #include <stdexcept>
  19. #include "absl/base/config.h"
  20. #include "absl/base/internal/raw_logging.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace base_internal {
  24. // NOTE: The various STL exception throwing functions are placed within the
  25. // #ifdef blocks so the symbols aren't exposed on platforms that don't support
  26. // them, such as the Android NDK. For example, ANGLE fails to link when building
  27. // within AOSP without them, since the STL functions don't exist.
  28. namespace {
  29. #ifdef ABSL_HAVE_EXCEPTIONS
  30. template <typename T>
  31. [[noreturn]] void Throw(const T& error) {
  32. throw error;
  33. }
  34. #endif
  35. } // namespace
  36. void ThrowStdLogicError(const std::string& what_arg) {
  37. #ifdef ABSL_HAVE_EXCEPTIONS
  38. Throw(std::logic_error(what_arg));
  39. #else
  40. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  41. std::abort();
  42. #endif
  43. }
  44. void ThrowStdLogicError(const char* what_arg) {
  45. #ifdef ABSL_HAVE_EXCEPTIONS
  46. Throw(std::logic_error(what_arg));
  47. #else
  48. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  49. std::abort();
  50. #endif
  51. }
  52. void ThrowStdInvalidArgument(const std::string& what_arg) {
  53. #ifdef ABSL_HAVE_EXCEPTIONS
  54. Throw(std::invalid_argument(what_arg));
  55. #else
  56. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  57. std::abort();
  58. #endif
  59. }
  60. void ThrowStdInvalidArgument(const char* what_arg) {
  61. #ifdef ABSL_HAVE_EXCEPTIONS
  62. Throw(std::invalid_argument(what_arg));
  63. #else
  64. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  65. std::abort();
  66. #endif
  67. }
  68. void ThrowStdDomainError(const std::string& what_arg) {
  69. #ifdef ABSL_HAVE_EXCEPTIONS
  70. Throw(std::domain_error(what_arg));
  71. #else
  72. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  73. std::abort();
  74. #endif
  75. }
  76. void ThrowStdDomainError(const char* what_arg) {
  77. #ifdef ABSL_HAVE_EXCEPTIONS
  78. Throw(std::domain_error(what_arg));
  79. #else
  80. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  81. std::abort();
  82. #endif
  83. }
  84. void ThrowStdLengthError(const std::string& what_arg) {
  85. #ifdef ABSL_HAVE_EXCEPTIONS
  86. Throw(std::length_error(what_arg));
  87. #else
  88. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  89. std::abort();
  90. #endif
  91. }
  92. void ThrowStdLengthError(const char* what_arg) {
  93. #ifdef ABSL_HAVE_EXCEPTIONS
  94. Throw(std::length_error(what_arg));
  95. #else
  96. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  97. std::abort();
  98. #endif
  99. }
  100. void ThrowStdOutOfRange(const std::string& what_arg) {
  101. #ifdef ABSL_HAVE_EXCEPTIONS
  102. 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. }
  108. void ThrowStdOutOfRange(const char* what_arg) {
  109. #ifdef ABSL_HAVE_EXCEPTIONS
  110. Throw(std::out_of_range(what_arg));
  111. #else
  112. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  113. std::abort();
  114. #endif
  115. }
  116. void ThrowStdRuntimeError(const std::string& what_arg) {
  117. #ifdef ABSL_HAVE_EXCEPTIONS
  118. Throw(std::runtime_error(what_arg));
  119. #else
  120. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  121. std::abort();
  122. #endif
  123. }
  124. void ThrowStdRuntimeError(const char* what_arg) {
  125. #ifdef ABSL_HAVE_EXCEPTIONS
  126. Throw(std::runtime_error(what_arg));
  127. #else
  128. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  129. std::abort();
  130. #endif
  131. }
  132. void ThrowStdRangeError(const std::string& what_arg) {
  133. #ifdef ABSL_HAVE_EXCEPTIONS
  134. Throw(std::range_error(what_arg));
  135. #else
  136. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  137. std::abort();
  138. #endif
  139. }
  140. void ThrowStdRangeError(const char* what_arg) {
  141. #ifdef ABSL_HAVE_EXCEPTIONS
  142. Throw(std::range_error(what_arg));
  143. #else
  144. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  145. std::abort();
  146. #endif
  147. }
  148. void ThrowStdOverflowError(const std::string& what_arg) {
  149. #ifdef ABSL_HAVE_EXCEPTIONS
  150. Throw(std::overflow_error(what_arg));
  151. #else
  152. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  153. std::abort();
  154. #endif
  155. }
  156. void ThrowStdOverflowError(const char* what_arg) {
  157. #ifdef ABSL_HAVE_EXCEPTIONS
  158. Throw(std::overflow_error(what_arg));
  159. #else
  160. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  161. std::abort();
  162. #endif
  163. }
  164. void ThrowStdUnderflowError(const std::string& what_arg) {
  165. #ifdef ABSL_HAVE_EXCEPTIONS
  166. Throw(std::underflow_error(what_arg));
  167. #else
  168. ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
  169. std::abort();
  170. #endif
  171. }
  172. void ThrowStdUnderflowError(const char* what_arg) {
  173. #ifdef ABSL_HAVE_EXCEPTIONS
  174. Throw(std::underflow_error(what_arg));
  175. #else
  176. ABSL_RAW_LOG(FATAL, "%s", what_arg);
  177. std::abort();
  178. #endif
  179. }
  180. void ThrowStdBadFunctionCall() {
  181. #ifdef ABSL_HAVE_EXCEPTIONS
  182. Throw(std::bad_function_call());
  183. #else
  184. std::abort();
  185. #endif
  186. }
  187. void ThrowStdBadAlloc() {
  188. #ifdef ABSL_HAVE_EXCEPTIONS
  189. Throw(std::bad_alloc());
  190. #else
  191. std::abort();
  192. #endif
  193. }
  194. } // namespace base_internal
  195. ABSL_NAMESPACE_END
  196. } // namespace absl