]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/exception_ptr.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / exception_ptr.h
1 // Exception Handling support header (exception_ptr class) for -*- C++ -*-
2
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file bits/exception_ptr.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{exception}
29 */
30
31 #ifndef _EXCEPTION_PTR_H
32 #define _EXCEPTION_PTR_H
33
34 #pragma GCC visibility push(default)
35
36 #include <bits/c++config.h>
37 #include <bits/exception_defines.h>
38 #include <bits/cxxabi_init_exception.h>
39 #include <typeinfo>
40 #include <new>
41
42 #if ATOMIC_INT_LOCK_FREE < 2
43 # error This platform does not support exception propagation.
44 #endif
45
46 extern "C++" {
47
48 namespace std
49 {
50 class type_info;
51
52 /**
53 * @addtogroup exceptions
54 * @{
55 */
56 namespace __exception_ptr
57 {
58 class exception_ptr;
59 }
60
61 using __exception_ptr::exception_ptr;
62
63 /** Obtain an exception_ptr to the currently handled exception. If there
64 * is none, or the currently handled exception is foreign, return the null
65 * value.
66 */
67 exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
68
69 template<typename _Ex>
70 exception_ptr make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
71
72 /// Throw the object pointed to by the exception_ptr.
73 void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__));
74
75 namespace __exception_ptr
76 {
77 using std::rethrow_exception;
78
79 /**
80 * @brief An opaque pointer to an arbitrary exception.
81 * @ingroup exceptions
82 */
83 class exception_ptr
84 {
85 void* _M_exception_object;
86
87 explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT;
88
89 void _M_addref() _GLIBCXX_USE_NOEXCEPT;
90 void _M_release() _GLIBCXX_USE_NOEXCEPT;
91
92 void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__));
93
94 friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
95 friend void std::rethrow_exception(exception_ptr);
96 template<typename _Ex>
97 friend exception_ptr std::make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
98
99 public:
100 exception_ptr() _GLIBCXX_USE_NOEXCEPT;
101
102 exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
103
104 #if __cplusplus >= 201103L
105 exception_ptr(nullptr_t) noexcept
106 : _M_exception_object(0)
107 { }
108
109 exception_ptr(exception_ptr&& __o) noexcept
110 : _M_exception_object(__o._M_exception_object)
111 { __o._M_exception_object = 0; }
112 #endif
113
114 #if (__cplusplus < 201103L) || defined (_GLIBCXX_EH_PTR_COMPAT)
115 typedef void (exception_ptr::*__safe_bool)();
116
117 // For construction from nullptr or 0.
118 exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT;
119 #endif
120
121 exception_ptr&
122 operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
123
124 #if __cplusplus >= 201103L
125 exception_ptr&
126 operator=(exception_ptr&& __o) noexcept
127 {
128 exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this);
129 return *this;
130 }
131 #endif
132
133 ~exception_ptr() _GLIBCXX_USE_NOEXCEPT;
134
135 void
136 swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
137
138 #ifdef _GLIBCXX_EH_PTR_COMPAT
139 // Retained for compatibility with CXXABI_1.3.
140 void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT
141 __attribute__ ((__const__));
142 bool operator!() const _GLIBCXX_USE_NOEXCEPT
143 __attribute__ ((__pure__));
144 operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT;
145 #endif
146
147 #if __cplusplus >= 201103L
148 explicit operator bool() const
149 { return _M_exception_object; }
150 #endif
151
152 friend bool
153 operator==(const exception_ptr&, const exception_ptr&)
154 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
155
156 const class std::type_info*
157 __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT
158 __attribute__ ((__pure__));
159 };
160
161 bool
162 operator==(const exception_ptr&, const exception_ptr&)
163 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
164
165 bool
166 operator!=(const exception_ptr&, const exception_ptr&)
167 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
168
169 inline void
170 swap(exception_ptr& __lhs, exception_ptr& __rhs)
171 { __lhs.swap(__rhs); }
172
173 template<typename _Ex>
174 inline void
175 __dest_thunk(void* x)
176 { static_cast<_Ex*>(x)->~_Ex(); }
177
178 } // namespace __exception_ptr
179
180 /// Obtain an exception_ptr pointing to a copy of the supplied object.
181 template<typename _Ex>
182 exception_ptr
183 make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
184 {
185 #if __cpp_exceptions
186 try
187 {
188 #if __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI
189 void *__e = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ex));
190 (void)__cxxabiv1::__cxa_init_primary_exception(
191 __e, const_cast<std::type_info*>(&typeid(__ex)),
192 __exception_ptr::__dest_thunk<_Ex>);
193 ::new (__e) _Ex(__ex);
194 return exception_ptr(__e);
195 #else
196 throw __ex;
197 #endif
198 }
199 catch(...)
200 {
201 return current_exception();
202 }
203 #else
204 return exception_ptr();
205 #endif
206 }
207
208 // _GLIBCXX_RESOLVE_LIB_DEFECTS
209 // 1130. copy_exception name misleading
210 /// Obtain an exception_ptr pointing to a copy of the supplied object.
211 /// This function is deprecated, use std::make_exception_ptr instead.
212 template<typename _Ex>
213 exception_ptr
214 copy_exception(_Ex __ex) _GLIBCXX_USE_NOEXCEPT _GLIBCXX_DEPRECATED;
215
216 template<typename _Ex>
217 exception_ptr
218 copy_exception(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
219 { return std::make_exception_ptr<_Ex>(__ex); }
220
221 // @} group exceptions
222 } // namespace std
223
224 } // extern "C++"
225
226 #pragma GCC visibility pop
227
228 #endif