1 // Exception Handling support header (exception_ptr class) for -*- C++ -*- 2 3 // Copyright (C) 2008-2020 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 extern "C++" { 43 44 namespace std 45 { 46 class type_info; 47 48 /** 49 * @addtogroup exceptions 50 * @{ 51 */ 52 53 namespace __exception_ptr 54 { 55 class exception_ptr; 56 } 57 58 using __exception_ptr::exception_ptr; 59 60 /** Obtain an exception_ptr to the currently handled exception. If there 61 * is none, or the currently handled exception is foreign, return the null 62 * value. 63 */ 64 exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT; 65 66 template<typename _Ex> 67 exception_ptr make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT; 68 69 /// Throw the object pointed to by the exception_ptr. 70 void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__)); 71 72 namespace __exception_ptr 73 { 74 using std::rethrow_exception; 75 76 /** 77 * @brief An opaque pointer to an arbitrary exception. 78 * @ingroup exceptions 79 */ 80 class exception_ptr 81 { 82 void* _M_exception_object; 83 84 explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT; 85 86 void _M_addref() _GLIBCXX_USE_NOEXCEPT; 87 void _M_release() _GLIBCXX_USE_NOEXCEPT; 88 89 void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__)); 90 91 friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT; 92 friend void std::rethrow_exception(exception_ptr); 93 template<typename _Ex> 94 friend exception_ptr std::make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT; 95 96 public: 97 exception_ptr() _GLIBCXX_USE_NOEXCEPT; 98 99 exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT; 100 101 #if __cplusplus >= 201103L exception_ptr(nullptr_t)102 exception_ptr(nullptr_t) noexcept 103 : _M_exception_object(0) 104 { } 105 exception_ptr(exception_ptr && __o)106 exception_ptr(exception_ptr&& __o) noexcept 107 : _M_exception_object(__o._M_exception_object) 108 { __o._M_exception_object = 0; } 109 #endif 110 111 #if (__cplusplus < 201103L) || defined (_GLIBCXX_EH_PTR_COMPAT) 112 typedef void (exception_ptr::*__safe_bool)(); 113 114 // For construction from nullptr or 0. 115 exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT; 116 #endif 117 118 exception_ptr& 119 operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT; 120 121 #if __cplusplus >= 201103L 122 exception_ptr& 123 operator=(exception_ptr&& __o) noexcept 124 { 125 exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this); 126 return *this; 127 } 128 #endif 129 130 ~exception_ptr() _GLIBCXX_USE_NOEXCEPT; 131 132 void 133 swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT; 134 135 #ifdef _GLIBCXX_EH_PTR_COMPAT 136 // Retained for compatibility with CXXABI_1.3. 137 void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT 138 __attribute__ ((__const__)); 139 bool operator!() const _GLIBCXX_USE_NOEXCEPT 140 __attribute__ ((__pure__)); 141 operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT; 142 #endif 143 144 #if __cplusplus >= 201103L 145 explicit operator bool() const 146 { return _M_exception_object; } 147 #endif 148 149 friend bool 150 operator==(const exception_ptr&, const exception_ptr&) 151 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__)); 152 153 const class std::type_info* 154 __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT 155 __attribute__ ((__pure__)); 156 }; 157 158 /// @relates exception_ptr @{ 159 160 bool 161 operator==(const exception_ptr&, const exception_ptr&) 162 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__)); 163 164 bool 165 operator!=(const exception_ptr&, const exception_ptr&) 166 _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__)); 167 168 inline void swap(exception_ptr & __lhs,exception_ptr & __rhs)169 swap(exception_ptr& __lhs, exception_ptr& __rhs) 170 { __lhs.swap(__rhs); } 171 172 // @} 173 174 /// @cond undocumented 175 template<typename _Ex> 176 inline void __dest_thunk(void * __x)177 __dest_thunk(void* __x) 178 { static_cast<_Ex*>(__x)->~_Ex(); } 179 /// @endcond 180 181 } // namespace __exception_ptr 182 183 /// Obtain an exception_ptr pointing to a copy of the supplied object. 184 template<typename _Ex> 185 exception_ptr make_exception_ptr(_Ex __ex)186 make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT 187 { 188 #if __cpp_exceptions && __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 try 194 { 195 ::new (__e) _Ex(__ex); 196 return exception_ptr(__e); 197 } 198 catch(...) 199 { 200 __cxxabiv1::__cxa_free_exception(__e); 201 return current_exception(); 202 } 203 #elif __cpp_exceptions 204 try 205 { 206 throw __ex; 207 } 208 catch(...) 209 { 210 return current_exception(); 211 } 212 #else // no RTTI and no exceptions 213 return exception_ptr(); 214 #endif 215 } 216 217 /// @} group exceptions 218 } // namespace std 219 220 } // extern "C++" 221 222 #pragma GCC visibility pop 223 224 #endif 225