| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _GLIBCXX_NESTED_EXCEPTION_H |
| 31 |
#define _GLIBCXX_NESTED_EXCEPTION_H 1 |
| 32 |
|
| 33 |
#if __cplusplus < 201103L |
| 34 |
# include <bits/c++0x_warning.h> |
| 35 |
#else |
| 36 |
|
| 37 |
#include <bits/move.h> |
| 38 |
#include <bits/exception_ptr.h> |
| 39 |
|
| 40 |
extern "C++" { |
| 41 |
|
| 42 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 43 |
{ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
class nested_exception |
| 60 |
{ |
| 61 |
exception_ptr _M_ptr; |
| 62 |
|
| 63 |
public: |
| 64 |
|
| 65 |
nested_exception() noexcept : _M_ptr(current_exception()) { } |
| 66 |
|
| 67 |
nested_exception(const nested_exception&) noexcept = default; |
| 68 |
|
| 69 |
nested_exception& operator=(const nested_exception&) noexcept = default; |
| 70 |
|
| 71 |
virtual ~nested_exception() noexcept; |
| 72 |
|
| 73 |
|
| 74 |
[[noreturn]] |
| 75 |
void |
| 76 |
rethrow_nested() const |
| 77 |
{ |
| 78 |
if (_M_ptr) |
| 79 |
rethrow_exception(_M_ptr); |
| 80 |
std::terminate(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
exception_ptr |
| 85 |
nested_ptr() const noexcept |
| 86 |
{ return _M_ptr; } |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template<typename _Except> |
| 92 |
struct _Nested_exception : public _Except, public nested_exception |
| 93 |
{ |
| 94 |
explicit _Nested_exception(const _Except& __ex) |
| 95 |
: _Except(__ex) |
| 96 |
{ } |
| 97 |
|
| 98 |
explicit _Nested_exception(_Except&& __ex) |
| 99 |
: _Except(static_cast<_Except&&>(__ex)) |
| 100 |
{ } |
| 101 |
}; |
| 102 |
|
| 103 |
#if __cplusplus < 201703L || ! defined __cpp_if_constexpr |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
template<typename _Tp> |
| 108 |
[[noreturn]] |
| 109 |
inline void |
| 110 |
__throw_with_nested_impl(_Tp&& __t, true_type) |
| 111 |
{ |
| 112 |
throw _Nested_exception<__remove_cvref_t<_Tp>>{std::forward<_Tp>(__t)}; |
| 113 |
} |
| 114 |
|
| 115 |
template<typename _Tp> |
| 116 |
[[noreturn]] |
| 117 |
inline void |
| 118 |
__throw_with_nested_impl(_Tp&& __t, false_type) |
| 119 |
{ throw std::forward<_Tp>(__t); } |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
template<typename _Tp> |
| 146 |
[[noreturn]] |
| 147 |
inline void |
| 148 |
throw_with_nested(_Tp&& __t) |
| 149 |
{ |
| 150 |
using _Up = typename decay<_Tp>::type; |
| 151 |
using _CopyConstructible |
| 152 |
= __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>; |
| 153 |
static_assert(_CopyConstructible::value, |
| 154 |
"throw_with_nested argument must be CopyConstructible"); |
| 155 |
|
| 156 |
#if __cplusplus >= 201703L && __cpp_if_constexpr |
| 157 |
if constexpr (is_class_v<_Up>) |
| 158 |
if constexpr (!is_final_v<_Up>) |
| 159 |
if constexpr (!is_base_of_v<nested_exception, _Up>) |
| 160 |
throw _Nested_exception<_Up>{std::forward<_Tp>(__t)}; |
| 161 |
throw std::forward<_Tp>(__t); |
| 162 |
#else |
| 163 |
using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>, |
| 164 |
__not_<is_base_of<nested_exception, _Up>>>; |
| 165 |
std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{}); |
| 166 |
#endif |
| 167 |
} |
| 168 |
|
| 169 |
#if __cplusplus < 201703L || ! defined __cpp_if_constexpr |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
template<typename _Ex> |
| 174 |
inline void |
| 175 |
__rethrow_if_nested_impl(const _Ex* __ptr, true_type) |
| 176 |
{ |
| 177 |
if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr)) |
| 178 |
__ne_ptr->rethrow_nested(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
inline void |
| 183 |
__rethrow_if_nested_impl(const void*, false_type) |
| 184 |
{ } |
| 185 |
|
| 186 |
|
| 187 |
#endif |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
template<typename _Ex> |
| 204 |
# if ! __cpp_rtti |
| 205 |
[[__gnu__::__always_inline__]] |
| 206 |
#endif |
| 207 |
inline void |
| 208 |
rethrow_if_nested(const _Ex& __ex) |
| 209 |
{ |
| 210 |
const _Ex* __ptr = __builtin_addressof(__ex); |
| 211 |
#if __cplusplus < 201703L || ! defined __cpp_if_constexpr |
| 212 |
# if __cpp_rtti |
| 213 |
using __cast = __and_<is_polymorphic<_Ex>, |
| 214 |
__or_<__not_<is_base_of<nested_exception, _Ex>>, |
| 215 |
is_convertible<_Ex*, nested_exception*>>>; |
| 216 |
# else |
| 217 |
using __cast = __and_<is_polymorphic<_Ex>, |
| 218 |
is_base_of<nested_exception, _Ex>, |
| 219 |
is_convertible<_Ex*, nested_exception*>>; |
| 220 |
# endif |
| 221 |
std::__rethrow_if_nested_impl(__ptr, __cast{}); |
| 222 |
#else |
| 223 |
if constexpr (!is_polymorphic_v<_Ex>) |
| 224 |
return; |
| 225 |
else if constexpr (is_base_of_v<nested_exception, _Ex> |
| 226 |
&& !is_convertible_v<_Ex*, nested_exception*>) |
| 227 |
return; |
| 228 |
# if ! __cpp_rtti |
| 229 |
else if constexpr (!is_base_of_v<nested_exception, _Ex>) |
| 230 |
return; |
| 231 |
# endif |
| 232 |
else if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr)) |
| 233 |
__ne_ptr->rethrow_nested(); |
| 234 |
#endif |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
#endif |
| 243 |
#endif |