throw_delegate.cc 4.8 KB

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