nested_exception.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
  9. #define _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
  10. #include <__config>
  11. #include <__exception/exception_ptr.h>
  12. #include <__memory/addressof.h>
  13. #include <__type_traits/decay.h>
  14. #include <__type_traits/is_base_of.h>
  15. #include <__type_traits/is_class.h>
  16. #include <__type_traits/is_convertible.h>
  17. #include <__type_traits/is_copy_constructible.h>
  18. #include <__type_traits/is_final.h>
  19. #include <__type_traits/is_polymorphic.h>
  20. #include <__utility/forward.h>
  21. #include <cstddef>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. namespace std { // purposefully not using versioning namespace
  26. class _LIBCPP_EXPORTED_FROM_ABI nested_exception {
  27. exception_ptr __ptr_;
  28. public:
  29. nested_exception() _NOEXCEPT;
  30. _LIBCPP_HIDE_FROM_ABI nested_exception(const nested_exception&) _NOEXCEPT = default;
  31. _LIBCPP_HIDE_FROM_ABI nested_exception& operator=(const nested_exception&) _NOEXCEPT = default;
  32. virtual ~nested_exception() _NOEXCEPT;
  33. // access functions
  34. _LIBCPP_NORETURN void rethrow_nested() const;
  35. _LIBCPP_HIDE_FROM_ABI exception_ptr nested_ptr() const _NOEXCEPT { return __ptr_; }
  36. };
  37. template <class _Tp>
  38. struct __nested : public _Tp, public nested_exception {
  39. _LIBCPP_HIDE_FROM_ABI explicit __nested(const _Tp& __t) : _Tp(__t) {}
  40. };
  41. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  42. template <class _Tp, class _Up, bool>
  43. struct __throw_with_nested;
  44. template <class _Tp, class _Up>
  45. struct __throw_with_nested<_Tp, _Up, true> {
  46. _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void __do_throw(_Tp&& __t) {
  47. throw __nested<_Up>(std::forward<_Tp>(__t));
  48. }
  49. };
  50. template <class _Tp, class _Up>
  51. struct __throw_with_nested<_Tp, _Up, false> {
  52. _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void __do_throw(_Tp&& __t) { throw std::forward<_Tp>(__t); }
  53. };
  54. #endif
  55. template <class _Tp>
  56. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void throw_with_nested(_Tp&& __t) {
  57. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  58. using _Up = __decay_t<_Tp>;
  59. static_assert(is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
  60. __throw_with_nested<_Tp,
  61. _Up,
  62. is_class<_Up>::value && !is_base_of<nested_exception, _Up>::value &&
  63. !__libcpp_is_final<_Up>::value>::__do_throw(std::forward<_Tp>(__t));
  64. #else
  65. ((void)__t);
  66. // FIXME: Make this abort
  67. #endif
  68. }
  69. template <class _From, class _To>
  70. struct __can_dynamic_cast
  71. : _BoolConstant< is_polymorphic<_From>::value &&
  72. (!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {};
  73. template <class _Ep>
  74. inline _LIBCPP_HIDE_FROM_ABI void
  75. rethrow_if_nested(const _Ep& __e, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {
  76. const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e));
  77. if (__nep)
  78. __nep->rethrow_nested();
  79. }
  80. template <class _Ep>
  81. inline _LIBCPP_HIDE_FROM_ABI void
  82. rethrow_if_nested(const _Ep&, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {}
  83. } // namespace std
  84. #endif // _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H