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