annotate libstdc++-v3/libsupc++/nested_exception.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Nested Exception support header (nested_exception class) for -*- C++ -*-
kono
parents:
diff changeset
2
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
3 // Copyright (C) 2009-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4 //
kono
parents:
diff changeset
5 // This file is part of the GNU ISO C++ Library. This library is free
kono
parents:
diff changeset
6 // software; you can redistribute it and/or modify it under the
kono
parents:
diff changeset
7 // terms of the GNU General Public License as published by the
kono
parents:
diff changeset
8 // Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 // any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 // This library is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 // GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 // Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 // permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 // 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 // a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 // <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /** @file bits/nested_exception.h
kono
parents:
diff changeset
26 * This is an internal header file, included by other library headers.
kono
parents:
diff changeset
27 * Do not attempt to use it directly. @headername{exception}
kono
parents:
diff changeset
28 */
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #ifndef _GLIBCXX_NESTED_EXCEPTION_H
kono
parents:
diff changeset
31 #define _GLIBCXX_NESTED_EXCEPTION_H 1
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #pragma GCC visibility push(default)
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #if __cplusplus < 201103L
kono
parents:
diff changeset
36 # include <bits/c++0x_warning.h>
kono
parents:
diff changeset
37 #else
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 #include <bits/c++config.h>
kono
parents:
diff changeset
40 #include <bits/move.h>
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 extern "C++" {
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 namespace std
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 /**
kono
parents:
diff changeset
47 * @addtogroup exceptions
kono
parents:
diff changeset
48 * @{
kono
parents:
diff changeset
49 */
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 /// Exception class with exception_ptr data member.
kono
parents:
diff changeset
52 class nested_exception
kono
parents:
diff changeset
53 {
kono
parents:
diff changeset
54 exception_ptr _M_ptr;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 public:
kono
parents:
diff changeset
57 nested_exception() noexcept : _M_ptr(current_exception()) { }
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 nested_exception(const nested_exception&) noexcept = default;
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 nested_exception& operator=(const nested_exception&) noexcept = default;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 virtual ~nested_exception() noexcept;
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 [[noreturn]]
kono
parents:
diff changeset
66 void
kono
parents:
diff changeset
67 rethrow_nested() const
kono
parents:
diff changeset
68 {
kono
parents:
diff changeset
69 if (_M_ptr)
kono
parents:
diff changeset
70 rethrow_exception(_M_ptr);
kono
parents:
diff changeset
71 std::terminate();
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 exception_ptr
kono
parents:
diff changeset
75 nested_ptr() const noexcept
kono
parents:
diff changeset
76 { return _M_ptr; }
kono
parents:
diff changeset
77 };
kono
parents:
diff changeset
78
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
79 /// @cond undocumented
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
80
111
kono
parents:
diff changeset
81 template<typename _Except>
kono
parents:
diff changeset
82 struct _Nested_exception : public _Except, public nested_exception
kono
parents:
diff changeset
83 {
kono
parents:
diff changeset
84 explicit _Nested_exception(const _Except& __ex)
kono
parents:
diff changeset
85 : _Except(__ex)
kono
parents:
diff changeset
86 { }
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 explicit _Nested_exception(_Except&& __ex)
kono
parents:
diff changeset
89 : _Except(static_cast<_Except&&>(__ex))
kono
parents:
diff changeset
90 { }
kono
parents:
diff changeset
91 };
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 // [except.nested]/8
kono
parents:
diff changeset
94 // Throw an exception of unspecified type that is publicly derived from
kono
parents:
diff changeset
95 // both remove_reference_t<_Tp> and nested_exception.
kono
parents:
diff changeset
96 template<typename _Tp>
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
97 [[noreturn]]
111
kono
parents:
diff changeset
98 inline void
kono
parents:
diff changeset
99 __throw_with_nested_impl(_Tp&& __t, true_type)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 using _Up = typename remove_reference<_Tp>::type;
kono
parents:
diff changeset
102 throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 template<typename _Tp>
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
106 [[noreturn]]
111
kono
parents:
diff changeset
107 inline void
kono
parents:
diff changeset
108 __throw_with_nested_impl(_Tp&& __t, false_type)
kono
parents:
diff changeset
109 { throw std::forward<_Tp>(__t); }
kono
parents:
diff changeset
110
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
111 /// @endcond
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
112
111
kono
parents:
diff changeset
113 /// If @p __t is derived from nested_exception, throws @p __t.
kono
parents:
diff changeset
114 /// Else, throws an implementation-defined object derived from both.
kono
parents:
diff changeset
115 template<typename _Tp>
kono
parents:
diff changeset
116 [[noreturn]]
kono
parents:
diff changeset
117 inline void
kono
parents:
diff changeset
118 throw_with_nested(_Tp&& __t)
kono
parents:
diff changeset
119 {
kono
parents:
diff changeset
120 using _Up = typename decay<_Tp>::type;
kono
parents:
diff changeset
121 using _CopyConstructible
kono
parents:
diff changeset
122 = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
kono
parents:
diff changeset
123 static_assert(_CopyConstructible::value,
kono
parents:
diff changeset
124 "throw_with_nested argument must be CopyConstructible");
kono
parents:
diff changeset
125 using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
kono
parents:
diff changeset
126 __not_<is_base_of<nested_exception, _Up>>>;
kono
parents:
diff changeset
127 std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
130 /// @cond undocumented
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
131
111
kono
parents:
diff changeset
132 // Determine if dynamic_cast<const nested_exception&> would be well-formed.
kono
parents:
diff changeset
133 template<typename _Tp>
kono
parents:
diff changeset
134 using __rethrow_if_nested_cond = typename enable_if<
kono
parents:
diff changeset
135 __and_<is_polymorphic<_Tp>,
kono
parents:
diff changeset
136 __or_<__not_<is_base_of<nested_exception, _Tp>>,
kono
parents:
diff changeset
137 is_convertible<_Tp*, nested_exception*>>>::value
kono
parents:
diff changeset
138 >::type;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 // Attempt dynamic_cast to nested_exception and call rethrow_nested().
kono
parents:
diff changeset
141 template<typename _Ex>
kono
parents:
diff changeset
142 inline __rethrow_if_nested_cond<_Ex>
kono
parents:
diff changeset
143 __rethrow_if_nested_impl(const _Ex* __ptr)
kono
parents:
diff changeset
144 {
kono
parents:
diff changeset
145 if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
kono
parents:
diff changeset
146 __ne_ptr->rethrow_nested();
kono
parents:
diff changeset
147 }
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 // Otherwise, no effects.
kono
parents:
diff changeset
150 inline void
kono
parents:
diff changeset
151 __rethrow_if_nested_impl(const void*)
kono
parents:
diff changeset
152 { }
kono
parents:
diff changeset
153
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
154 /// @endcond
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
155
111
kono
parents:
diff changeset
156 /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
kono
parents:
diff changeset
157 template<typename _Ex>
kono
parents:
diff changeset
158 inline void
kono
parents:
diff changeset
159 rethrow_if_nested(const _Ex& __ex)
kono
parents:
diff changeset
160 { std::__rethrow_if_nested_impl(std::__addressof(__ex)); }
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 // @} group exceptions
kono
parents:
diff changeset
163 } // namespace std
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 } // extern "C++"
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 #endif // C++11
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 #pragma GCC visibility pop
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 #endif // _GLIBCXX_NESTED_EXCEPTION_H