libstdc++
|
00001 // Nested Exception support header (nested_exception class) for -*- C++ -*- 00002 00003 // Copyright (C) 2009-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/nested_exception.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{exception} 00028 */ 00029 00030 #ifndef _GLIBCXX_NESTED_EXCEPTION_H 00031 #define _GLIBCXX_NESTED_EXCEPTION_H 1 00032 00033 #pragma GCC visibility push(default) 00034 00035 #if __cplusplus < 201103L 00036 # include <bits/c++0x_warning.h> 00037 #else 00038 00039 #include <bits/c++config.h> 00040 #include <bits/move.h> 00041 00042 #if ATOMIC_INT_LOCK_FREE < 2 00043 # error This platform does not support exception propagation. 00044 #endif 00045 00046 extern "C++" { 00047 00048 namespace std 00049 { 00050 /** 00051 * @addtogroup exceptions 00052 * @{ 00053 */ 00054 00055 /// Exception class with exception_ptr data member. 00056 class nested_exception 00057 { 00058 exception_ptr _M_ptr; 00059 00060 public: 00061 nested_exception() noexcept : _M_ptr(current_exception()) { } 00062 00063 nested_exception(const nested_exception&) noexcept = default; 00064 00065 nested_exception& operator=(const nested_exception&) noexcept = default; 00066 00067 virtual ~nested_exception() noexcept; 00068 00069 [[noreturn]] 00070 void 00071 rethrow_nested() const 00072 { 00073 if (_M_ptr) 00074 rethrow_exception(_M_ptr); 00075 std::terminate(); 00076 } 00077 00078 exception_ptr 00079 nested_ptr() const noexcept 00080 { return _M_ptr; } 00081 }; 00082 00083 template<typename _Except> 00084 struct _Nested_exception : public _Except, public nested_exception 00085 { 00086 explicit _Nested_exception(const _Except& __ex) 00087 : _Except(__ex) 00088 { } 00089 00090 explicit _Nested_exception(_Except&& __ex) 00091 : _Except(static_cast<_Except&&>(__ex)) 00092 { } 00093 }; 00094 00095 template<typename _Tp, 00096 bool __with_nested = !__is_base_of(nested_exception, _Tp)> 00097 struct _Throw_with_nested_impl 00098 { 00099 template<typename _Up> 00100 static void _S_throw(_Up&& __t) 00101 { throw _Nested_exception<_Tp>{static_cast<_Up&&>(__t)}; } 00102 }; 00103 00104 template<typename _Tp> 00105 struct _Throw_with_nested_impl<_Tp, false> 00106 { 00107 template<typename _Up> 00108 static void _S_throw(_Up&& __t) 00109 { throw static_cast<_Up&&>(__t); } 00110 }; 00111 00112 template<typename _Tp, bool = __is_class(_Tp) && !__is_final(_Tp)> 00113 struct _Throw_with_nested_helper : _Throw_with_nested_impl<_Tp> 00114 { }; 00115 00116 template<typename _Tp> 00117 struct _Throw_with_nested_helper<_Tp, false> 00118 : _Throw_with_nested_impl<_Tp, false> 00119 { }; 00120 00121 template<typename _Tp> 00122 struct _Throw_with_nested_helper<_Tp&, false> 00123 : _Throw_with_nested_helper<_Tp> 00124 { }; 00125 00126 template<typename _Tp> 00127 struct _Throw_with_nested_helper<_Tp&&, false> 00128 : _Throw_with_nested_helper<_Tp> 00129 { }; 00130 00131 /// If @p __t is derived from nested_exception, throws @p __t. 00132 /// Else, throws an implementation-defined object derived from both. 00133 template<typename _Tp> 00134 [[noreturn]] 00135 inline void 00136 throw_with_nested(_Tp&& __t) 00137 { 00138 _Throw_with_nested_helper<_Tp>::_S_throw(static_cast<_Tp&&>(__t)); 00139 } 00140 00141 template<typename _Tp, bool = __is_polymorphic(_Tp)> 00142 struct _Rethrow_if_nested_impl 00143 { 00144 static void _S_rethrow(const _Tp& __t) 00145 { 00146 if (auto __tp = 00147 dynamic_cast<const nested_exception*>(std::__addressof(__t))) 00148 __tp->rethrow_nested(); 00149 } 00150 }; 00151 00152 template<typename _Tp> 00153 struct _Rethrow_if_nested_impl<_Tp, false> 00154 { 00155 static void _S_rethrow(const _Tp&) { } 00156 }; 00157 00158 /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested(). 00159 template<typename _Ex> 00160 inline void 00161 rethrow_if_nested(const _Ex& __ex) 00162 { 00163 _Rethrow_if_nested_impl<_Ex>::_S_rethrow(__ex); 00164 } 00165 00166 // @} group exceptions 00167 } // namespace std 00168 00169 } // extern "C++" 00170 00171 #endif // C++11 00172 00173 #pragma GCC visibility pop 00174 00175 #endif // _GLIBCXX_NESTED_EXCEPTION_H