1 // Nested Exception support header (nested_exception class) for -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
39 #include <bits/c++config.h>
41 #if ATOMIC_INT_LOCK_FREE < 2
42 # error This platform does not support exception propagation.
50 * @addtogroup exceptions
54 /// Exception class with exception_ptr data member.
55 class nested_exception
60 nested_exception() noexcept
: _M_ptr(current_exception()) { }
62 nested_exception(const nested_exception
&) = default;
64 nested_exception
& operator=(const nested_exception
&) = default;
66 virtual ~nested_exception() noexcept
;
69 rethrow_nested() const __attribute__ ((__noreturn__
))
70 { rethrow_exception(_M_ptr
); }
77 template<typename _Except
>
78 struct _Nested_exception
: public _Except
, public nested_exception
80 explicit _Nested_exception(_Except
&& __ex
)
81 : _Except(static_cast<_Except
&&>(__ex
))
85 template<typename _Ex
>
86 struct __get_nested_helper
88 static const nested_exception
*
89 _S_get(const _Ex
& __ex
)
90 { return dynamic_cast<const nested_exception
*>(&__ex
); }
93 template<typename _Ex
>
94 struct __get_nested_helper
<_Ex
*>
96 static const nested_exception
*
97 _S_get(const _Ex
* __ex
)
98 { return dynamic_cast<const nested_exception
*>(__ex
); }
101 template<typename _Ex
>
102 inline const nested_exception
*
103 __get_nested_exception(const _Ex
& __ex
)
104 { return __get_nested_helper
<_Ex
>::_S_get(__ex
); }
106 template<typename _Ex
>
108 __throw_with_nested(_Ex
&&, const nested_exception
* = 0)
109 __attribute__ ((__noreturn__
));
111 template<typename _Ex
>
113 __throw_with_nested(_Ex
&&, ...) __attribute__ ((__noreturn__
));
115 // This function should never be called, but is needed to avoid a warning
116 // about ambiguous base classes when instantiating throw_with_nested<_Ex>()
117 // with a type that has an accessible nested_exception base.
118 template<typename _Ex
>
120 __throw_with_nested(_Ex
&& __ex
, const nested_exception
*)
123 template<typename _Ex
>
125 __throw_with_nested(_Ex
&& __ex
, ...)
126 { throw _Nested_exception
<_Ex
>(static_cast<_Ex
&&>(__ex
)); }
128 template<typename _Ex
>
130 throw_with_nested(_Ex __ex
) __attribute__ ((__noreturn__
));
132 /// If @p __ex is derived from nested_exception, @p __ex.
133 /// Else, an implementation-defined object derived from both.
134 template<typename _Ex
>
136 throw_with_nested(_Ex __ex
)
138 if (__get_nested_exception(__ex
))
140 __throw_with_nested(static_cast<_Ex
&&>(__ex
), &__ex
);
143 /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
144 template<typename _Ex
>
146 rethrow_if_nested(const _Ex
& __ex
)
148 if (const nested_exception
* __nested
= __get_nested_exception(__ex
))
149 __nested
->rethrow_nested();
152 /// Overload, See N2619
154 rethrow_if_nested(const nested_exception
& __ex
)
155 { __ex
.rethrow_nested(); }
157 // @} group exceptions
162 #endif // __GXX_EXPERIMENTAL_CXX0X__
164 #pragma GCC visibility pop
166 #endif // _GLIBCXX_NESTED_EXCEPTION_H