libstdc++
nested_exception.h
Go to the documentation of this file.
00001 // Nested Exception support header (nested_exception class) for -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2015 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 
00041 #if ATOMIC_INT_LOCK_FREE < 2
00042 #  error This platform does not support exception propagation.
00043 #endif
00044 
00045 extern "C++" {
00046 
00047 namespace std
00048 {
00049   /**
00050    * @addtogroup exceptions
00051    * @{
00052    */
00053 
00054   /// Exception class with exception_ptr data member.
00055   class nested_exception
00056   {
00057     exception_ptr _M_ptr;
00058 
00059   public:
00060     nested_exception() noexcept : _M_ptr(current_exception()) { }
00061 
00062     nested_exception(const nested_exception&) noexcept = default;
00063 
00064     nested_exception& operator=(const nested_exception&) noexcept = default;
00065 
00066     virtual ~nested_exception() noexcept;
00067 
00068     [[noreturn]]
00069     void
00070     rethrow_nested() const
00071     {
00072       if (_M_ptr)
00073         rethrow_exception(_M_ptr);
00074       std::terminate();
00075     }
00076 
00077     exception_ptr
00078     nested_ptr() const noexcept
00079     { return _M_ptr; }
00080   };
00081 
00082   template<typename _Except>
00083     struct _Nested_exception : public _Except, public nested_exception
00084     {
00085       explicit _Nested_exception(const _Except& __ex)
00086       : _Except(__ex)
00087       { }
00088 
00089       explicit _Nested_exception(_Except&& __ex)
00090       : _Except(static_cast<_Except&&>(__ex))
00091       { }
00092     };
00093 
00094   template<typename _Tp,
00095            bool __with_nested = !__is_base_of(nested_exception, _Tp)>
00096     struct _Throw_with_nested_impl
00097     {
00098       template<typename _Up>
00099         static void _S_throw(_Up&& __t)
00100         { throw _Nested_exception<_Tp>{static_cast<_Up&&>(__t)}; }
00101     };
00102 
00103   template<typename _Tp>
00104     struct _Throw_with_nested_impl<_Tp, false>
00105     {
00106       template<typename _Up>
00107         static void _S_throw(_Up&& __t)
00108         { throw static_cast<_Up&&>(__t); }
00109     };
00110 
00111   template<typename _Tp, bool = __is_class(_Tp) && !__is_final(_Tp)>
00112     struct _Throw_with_nested_helper : _Throw_with_nested_impl<_Tp>
00113     { };
00114 
00115   template<typename _Tp>
00116     struct _Throw_with_nested_helper<_Tp, false>
00117     : _Throw_with_nested_impl<_Tp, false>
00118     { };
00119 
00120   template<typename _Tp>
00121     struct _Throw_with_nested_helper<_Tp&, false>
00122     : _Throw_with_nested_helper<_Tp>
00123     { };
00124 
00125   template<typename _Tp>
00126     struct _Throw_with_nested_helper<_Tp&&, false>
00127     : _Throw_with_nested_helper<_Tp>
00128     { };
00129 
00130   /// If @p __t is derived from nested_exception, throws @p __t.
00131   /// Else, throws an implementation-defined object derived from both.
00132   template<typename _Tp>
00133     [[noreturn]]
00134     inline void
00135     throw_with_nested(_Tp&& __t)
00136     {
00137       _Throw_with_nested_helper<_Tp>::_S_throw(static_cast<_Tp&&>(__t));
00138     }
00139 
00140   template<typename _Tp, bool = __is_polymorphic(_Tp)>
00141     struct _Rethrow_if_nested_impl
00142     {
00143       static void _S_rethrow(const _Tp& __t)
00144       {
00145         if (auto __tp = dynamic_cast<const nested_exception*>(&__t))
00146           __tp->rethrow_nested();
00147       }
00148     };
00149 
00150   template<typename _Tp>
00151     struct _Rethrow_if_nested_impl<_Tp, false>
00152     {
00153       static void _S_rethrow(const _Tp&) { }
00154     };
00155 
00156   /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
00157   template<typename _Ex>
00158     inline void
00159     rethrow_if_nested(const _Ex& __ex)
00160     {
00161       _Rethrow_if_nested_impl<_Ex>::_S_rethrow(__ex);
00162     }
00163 
00164   // @} group exceptions
00165 } // namespace std
00166 
00167 } // extern "C++"
00168 
00169 #endif // C++11
00170 
00171 #pragma GCC visibility pop
00172 
00173 #endif // _GLIBCXX_NESTED_EXCEPTION_H