Exception.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <exception>
  18. #include <folly/CPortability.h>
  19. #include <folly/CppAttributes.h>
  20. #include <folly/Portability.h>
  21. namespace folly {
  22. /// throw_exception
  23. ///
  24. /// Throw an exception if exceptions are enabled, or terminate if compiled with
  25. /// -fno-exceptions.
  26. template <typename Ex>
  27. [[noreturn]] FOLLY_NOINLINE FOLLY_COLD void throw_exception(Ex&& ex) {
  28. #if FOLLY_HAS_EXCEPTIONS
  29. throw static_cast<Ex&&>(ex);
  30. #else
  31. (void)ex;
  32. std::terminate();
  33. #endif
  34. }
  35. /// terminate_with
  36. ///
  37. /// Terminates as if by forwarding to throw_exception but in a noexcept context.
  38. template <typename Ex>
  39. [[noreturn]] FOLLY_NOINLINE FOLLY_COLD void terminate_with(Ex&& ex) noexcept {
  40. throw_exception(static_cast<Ex&&>(ex));
  41. }
  42. // clang-format off
  43. namespace detail {
  44. template <typename T>
  45. FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN T&& to_exception_arg_(T&& t) {
  46. return static_cast<T&&>(t);
  47. }
  48. template <std::size_t N>
  49. FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN char const* to_exception_arg_(
  50. char const (&array)[N]) {
  51. return static_cast<char const*>(array);
  52. }
  53. template <typename Ex, typename... Args>
  54. [[noreturn]] FOLLY_NOINLINE FOLLY_COLD void throw_exception_(Args&&... args) {
  55. throw_exception(Ex(static_cast<Args&&>(args)...));
  56. }
  57. template <typename Ex, typename... Args>
  58. [[noreturn]] FOLLY_NOINLINE FOLLY_COLD void terminate_with_(
  59. Args&&... args) noexcept {
  60. throw_exception(Ex(static_cast<Args&&>(args)...));
  61. }
  62. } // namespace detail
  63. // clang-format on
  64. /// throw_exception
  65. ///
  66. /// Construct and throw an exception if exceptions are enabled, or terminate if
  67. /// compiled with -fno-exceptions.
  68. ///
  69. /// Converts any arguments of type `char const[N]` to `char const*`.
  70. template <typename Ex, typename... Args>
  71. [[noreturn]] FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN void
  72. throw_exception(Args&&... args) {
  73. detail::throw_exception_<Ex>(
  74. detail::to_exception_arg_(static_cast<Args&&>(args))...);
  75. }
  76. /// terminate_with
  77. ///
  78. /// Terminates as if by forwarding to throw_exception but in a noexcept context.
  79. // clang-format off
  80. template <typename Ex, typename... Args>
  81. [[noreturn]] FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN void
  82. terminate_with(Args&&... args) noexcept {
  83. detail::terminate_with_<Ex>(
  84. detail::to_exception_arg_(static_cast<Args&&>(args))...);
  85. }
  86. // clang-format on
  87. /// invoke_noreturn_cold
  88. ///
  89. /// Invoke the provided function with the provided arguments. If the invocation
  90. /// returns, terminate.
  91. ///
  92. /// May be used with throw_exception in cases where construction of the object
  93. /// to be thrown requires more than just invoking its constructor with a given
  94. /// sequence of arguments passed by reference - for example, if a string message
  95. /// must be computed before being passed to the constructor of the object to be
  96. /// thrown.
  97. ///
  98. /// Usage note:
  99. /// Passing extra values as arguments rather than capturing them allows smaller
  100. /// bytecode at the call-site.
  101. ///
  102. /// Example:
  103. ///
  104. /// if (i < 0) {
  105. /// invoke_noreturn_cold(
  106. /// [](int j) {
  107. /// throw_exceptions(runtime_error(to<string>("invalid: ", j)));
  108. /// },
  109. /// i);
  110. /// }
  111. template <typename F, typename... A>
  112. [[noreturn]] FOLLY_NOINLINE FOLLY_COLD void invoke_noreturn_cold(
  113. F&& f,
  114. A&&... a) {
  115. static_cast<F&&>(f)(static_cast<A&&>(a)...);
  116. std::terminate();
  117. }
  118. } // namespace folly