nested_exception.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Nested Exception support header (nested_exception class) for -*- C++ -*-
  2. // Copyright (C) 2009-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/nested_exception.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{exception}
  23. */
  24. #ifndef _GLIBCXX_NESTED_EXCEPTION_H
  25. #define _GLIBCXX_NESTED_EXCEPTION_H 1
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <bits/move.h>
  30. #include <bits/exception_ptr.h>
  31. extern "C++" {
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. /**
  35. * @addtogroup exceptions
  36. * @{
  37. */
  38. /** Mixin class that stores the current exception.
  39. *
  40. * This type can be used via `std::throw_with_nested` to store
  41. * the current exception nested within another exception.
  42. *
  43. * @headerfile exception
  44. * @since C++11
  45. * @see std::throw_with_nested
  46. * @ingroup exceptions
  47. */
  48. class nested_exception
  49. {
  50. exception_ptr _M_ptr;
  51. public:
  52. /// The default constructor stores the current exception (if any).
  53. nested_exception() noexcept : _M_ptr(current_exception()) { }
  54. nested_exception(const nested_exception&) noexcept = default;
  55. nested_exception& operator=(const nested_exception&) noexcept = default;
  56. virtual ~nested_exception() noexcept;
  57. /// Rethrow the stored exception, or terminate if none was stored.
  58. [[noreturn]]
  59. void
  60. rethrow_nested() const
  61. {
  62. if (_M_ptr)
  63. rethrow_exception(_M_ptr);
  64. std::terminate();
  65. }
  66. /// Access the stored exception.
  67. exception_ptr
  68. nested_ptr() const noexcept
  69. { return _M_ptr; }
  70. };
  71. /// @cond undocumented
  72. template<typename _Except>
  73. struct _Nested_exception : public _Except, public nested_exception
  74. {
  75. explicit _Nested_exception(const _Except& __ex)
  76. : _Except(__ex)
  77. { }
  78. explicit _Nested_exception(_Except&& __ex)
  79. : _Except(static_cast<_Except&&>(__ex))
  80. { }
  81. };
  82. #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
  83. // [except.nested]/8
  84. // Throw an exception of unspecified type that is publicly derived from
  85. // both remove_reference_t<_Tp> and nested_exception.
  86. template<typename _Tp>
  87. [[noreturn]]
  88. inline void
  89. __throw_with_nested_impl(_Tp&& __t, true_type)
  90. {
  91. throw _Nested_exception<__remove_cvref_t<_Tp>>{std::forward<_Tp>(__t)};
  92. }
  93. template<typename _Tp>
  94. [[noreturn]]
  95. inline void
  96. __throw_with_nested_impl(_Tp&& __t, false_type)
  97. { throw std::forward<_Tp>(__t); }
  98. #endif
  99. /// @endcond
  100. /** Throw an exception that also stores the currently active exception.
  101. *
  102. * If `_Tp` is derived from `std::nested_exception` or is not usable
  103. * as a base-class, throws a copy of `__t`.
  104. * Otherwise, throws an object of an implementation-defined type derived
  105. * from both `_Tp` and `std::nested_exception`, containing a copy of `__t`
  106. * and the result of `std::current_exception()`.
  107. *
  108. * In other words, throws the argument as a new exception that contains
  109. * the currently active exception nested within it. This is intended for
  110. * use in a catch handler to replace the caught exception with a different
  111. * type, while still preserving the original exception. When the new
  112. * exception is caught, the nested exception can be rethrown by using
  113. * `std::rethrow_if_nested`.
  114. *
  115. * This can be used at API boundaries, for example to catch a library's
  116. * internal exception type and rethrow it nested with a `std::runtime_error`,
  117. * or vice versa.
  118. *
  119. * @since C++11
  120. */
  121. template<typename _Tp>
  122. [[noreturn]]
  123. inline void
  124. throw_with_nested(_Tp&& __t)
  125. {
  126. using _Up = typename decay<_Tp>::type;
  127. using _CopyConstructible
  128. = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
  129. static_assert(_CopyConstructible::value,
  130. "throw_with_nested argument must be CopyConstructible");
  131. #if __cplusplus >= 201703L && __cpp_if_constexpr
  132. if constexpr (is_class_v<_Up>)
  133. if constexpr (!is_final_v<_Up>)
  134. if constexpr (!is_base_of_v<nested_exception, _Up>)
  135. throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
  136. throw std::forward<_Tp>(__t);
  137. #else
  138. using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
  139. __not_<is_base_of<nested_exception, _Up>>>;
  140. std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
  141. #endif
  142. }
  143. #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
  144. /// @cond undocumented
  145. // Attempt dynamic_cast to nested_exception and call rethrow_nested().
  146. template<typename _Ex>
  147. inline void
  148. __rethrow_if_nested_impl(const _Ex* __ptr, true_type)
  149. {
  150. if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
  151. __ne_ptr->rethrow_nested();
  152. }
  153. // Otherwise, no effects.
  154. inline void
  155. __rethrow_if_nested_impl(const void*, false_type)
  156. { }
  157. /// @endcond
  158. #endif
  159. /** Rethrow a nested exception
  160. *
  161. * If `__ex` contains a `std::nested_exception` object, call its
  162. * `rethrow_nested()` member to rethrow the stored exception.
  163. *
  164. * After catching an exception thrown by a call to `std::throw_with_nested`
  165. * this function can be used to rethrow the exception that was active when
  166. * `std::throw_with_nested` was called.
  167. *
  168. * @since C++11
  169. */
  170. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  171. // 2484. rethrow_if_nested() is doubly unimplementable
  172. // 2784. Resolution to LWG 2484 is missing "otherwise, no effects" and [...]
  173. template<typename _Ex>
  174. # if ! __cpp_rtti
  175. [[__gnu__::__always_inline__]]
  176. #endif
  177. inline void
  178. rethrow_if_nested(const _Ex& __ex)
  179. {
  180. const _Ex* __ptr = __builtin_addressof(__ex);
  181. #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
  182. # if __cpp_rtti
  183. using __cast = __and_<is_polymorphic<_Ex>,
  184. __or_<__not_<is_base_of<nested_exception, _Ex>>,
  185. is_convertible<_Ex*, nested_exception*>>>;
  186. # else
  187. using __cast = __and_<is_polymorphic<_Ex>,
  188. is_base_of<nested_exception, _Ex>,
  189. is_convertible<_Ex*, nested_exception*>>;
  190. # endif
  191. std::__rethrow_if_nested_impl(__ptr, __cast{});
  192. #else
  193. if constexpr (!is_polymorphic_v<_Ex>)
  194. return;
  195. else if constexpr (is_base_of_v<nested_exception, _Ex>
  196. && !is_convertible_v<_Ex*, nested_exception*>)
  197. return; // nested_exception base class is inaccessible or ambiguous.
  198. # if ! __cpp_rtti
  199. else if constexpr (!is_base_of_v<nested_exception, _Ex>)
  200. return; // Cannot do polymorphic casts without RTTI.
  201. # endif
  202. else if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
  203. __ne_ptr->rethrow_nested();
  204. #endif
  205. }
  206. /// @} group exceptions
  207. } // namespace std
  208. } // extern "C++"
  209. #endif // C++11
  210. #endif // _GLIBCXX_NESTED_EXCEPTION_H