File indexing completed on 2026-05-03 08:13:46
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
0010 #define _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
0011
0012 #include <__config>
0013 #include <__exception/exception_ptr.h>
0014 #include <__memory/addressof.h>
0015 #include <__type_traits/decay.h>
0016 #include <__type_traits/enable_if.h>
0017 #include <__type_traits/integral_constant.h>
0018 #include <__type_traits/is_base_of.h>
0019 #include <__type_traits/is_class.h>
0020 #include <__type_traits/is_constructible.h>
0021 #include <__type_traits/is_convertible.h>
0022 #include <__type_traits/is_final.h>
0023 #include <__type_traits/is_polymorphic.h>
0024 #include <__utility/forward.h>
0025
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 # pragma GCC system_header
0028 #endif
0029
0030 namespace std {
0031
0032 class _LIBCPP_EXPORTED_FROM_ABI nested_exception {
0033 exception_ptr __ptr_;
0034
0035 public:
0036 nested_exception() _NOEXCEPT;
0037 _LIBCPP_HIDE_FROM_ABI nested_exception(const nested_exception&) _NOEXCEPT = default;
0038 _LIBCPP_HIDE_FROM_ABI nested_exception& operator=(const nested_exception&) _NOEXCEPT = default;
0039 virtual ~nested_exception() _NOEXCEPT;
0040
0041
0042 [[__noreturn__]] void rethrow_nested() const;
0043 _LIBCPP_HIDE_FROM_ABI exception_ptr nested_ptr() const _NOEXCEPT { return __ptr_; }
0044 };
0045
0046 template <class _Tp>
0047 struct __nested : public _Tp, public nested_exception {
0048 _LIBCPP_HIDE_FROM_ABI explicit __nested(const _Tp& __t) : _Tp(__t) {}
0049 };
0050
0051 #if _LIBCPP_HAS_EXCEPTIONS
0052 template <class _Tp, class _Up, bool>
0053 struct __throw_with_nested;
0054
0055 template <class _Tp, class _Up>
0056 struct __throw_with_nested<_Tp, _Up, true> {
0057 [[__noreturn__]] static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) {
0058 throw __nested<_Up>(std::forward<_Tp>(__t));
0059 }
0060 };
0061
0062 template <class _Tp, class _Up>
0063 struct __throw_with_nested<_Tp, _Up, false> {
0064 [[__noreturn__]] static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) { throw std::forward<_Tp>(__t); }
0065 };
0066 #endif
0067
0068 template <class _Tp>
0069 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI void throw_with_nested(_Tp&& __t) {
0070 #if _LIBCPP_HAS_EXCEPTIONS
0071 using _Up = __decay_t<_Tp>;
0072 static_assert(is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
0073 __throw_with_nested<_Tp,
0074 _Up,
0075 is_class<_Up>::value && !is_base_of<nested_exception, _Up>::value &&
0076 !__libcpp_is_final<_Up>::value>::__do_throw(std::forward<_Tp>(__t));
0077 #else
0078 ((void)__t);
0079
0080 #endif
0081 }
0082
0083 template <class _From, class _To>
0084 struct __can_dynamic_cast
0085 : _BoolConstant< is_polymorphic<_From>::value &&
0086 (!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {};
0087
0088 template <class _Ep, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
0089 inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) {
0090 const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e));
0091 if (__nep)
0092 __nep->rethrow_nested();
0093 }
0094
0095 template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
0096 inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {}
0097
0098 }
0099
0100 #endif