1 // Nested Exception support header (nested_exception class) for -*- C++ -*-
3 // Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/nested_exception.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{exception}
30 #ifndef _GLIBCXX_NESTED_EXCEPTION_H
31 #define _GLIBCXX_NESTED_EXCEPTION_H 1
33 #pragma GCC visibility push(default)
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
39 #include <bits/c++config.h>
40 #include <bits/move.h>
47 * @addtogroup exceptions
51 /// Exception class with exception_ptr data member.
52 class nested_exception
57 nested_exception() noexcept
: _M_ptr(current_exception()) { }
59 nested_exception(const nested_exception
&) noexcept
= default;
61 nested_exception
& operator=(const nested_exception
&) noexcept
= default;
63 virtual ~nested_exception() noexcept
;
67 rethrow_nested() const
70 rethrow_exception(_M_ptr
);
75 nested_ptr() const noexcept
79 template<typename _Except
>
80 struct _Nested_exception
: public _Except
, public nested_exception
82 explicit _Nested_exception(const _Except
& __ex
)
86 explicit _Nested_exception(_Except
&& __ex
)
87 : _Except(static_cast<_Except
&&>(__ex
))
92 // Throw an exception of unspecified type that is publicly derived from
93 // both remove_reference_t<_Tp> and nested_exception.
94 template<typename _Tp
>
96 __throw_with_nested_impl(_Tp
&& __t
, true_type
)
98 using _Up
= typename remove_reference
<_Tp
>::type
;
99 throw _Nested_exception
<_Up
>{std::forward
<_Tp
>(__t
)};
102 template<typename _Tp
>
104 __throw_with_nested_impl(_Tp
&& __t
, false_type
)
105 { throw std::forward
<_Tp
>(__t
); }
107 /// If @p __t is derived from nested_exception, throws @p __t.
108 /// Else, throws an implementation-defined object derived from both.
109 template<typename _Tp
>
112 throw_with_nested(_Tp
&& __t
)
114 using _Up
= typename decay
<_Tp
>::type
;
115 using _CopyConstructible
116 = __and_
<is_copy_constructible
<_Up
>, is_move_constructible
<_Up
>>;
117 static_assert(_CopyConstructible::value
,
118 "throw_with_nested argument must be CopyConstructible");
119 using __nest
= __and_
<is_class
<_Up
>, __bool_constant
<!__is_final(_Up
)>,
120 __not_
<is_base_of
<nested_exception
, _Up
>>>;
121 std::__throw_with_nested_impl(std::forward
<_Tp
>(__t
), __nest
{});
124 // Determine if dynamic_cast<const nested_exception&> would be well-formed.
125 template<typename _Tp
>
126 using __rethrow_if_nested_cond
= typename enable_if
<
127 __and_
<is_polymorphic
<_Tp
>,
128 __or_
<__not_
<is_base_of
<nested_exception
, _Tp
>>,
129 is_convertible
<_Tp
*, nested_exception
*>>>::value
132 // Attempt dynamic_cast to nested_exception and call rethrow_nested().
133 template<typename _Ex
>
134 inline __rethrow_if_nested_cond
<_Ex
>
135 __rethrow_if_nested_impl(const _Ex
* __ptr
)
137 if (auto __ne_ptr
= dynamic_cast<const nested_exception
*>(__ptr
))
138 __ne_ptr
->rethrow_nested();
141 // Otherwise, no effects.
143 __rethrow_if_nested_impl(const void*)
146 /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
147 template<typename _Ex
>
149 rethrow_if_nested(const _Ex
& __ex
)
150 { std::__rethrow_if_nested_impl(std::__addressof(__ex
)); }
152 // @} group exceptions
159 #pragma GCC visibility pop
161 #endif // _GLIBCXX_NESTED_EXCEPTION_H