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-2017 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 extern "C++" {
00043 
00044 namespace std
00045 {
00046   /**
00047    * @addtogroup exceptions
00048    * @{
00049    */
00050 
00051   /// Exception class with exception_ptr data member.
00052   class nested_exception
00053   {
00054     exception_ptr _M_ptr;
00055 
00056   public:
00057     nested_exception() noexcept : _M_ptr(current_exception()) { }
00058 
00059     nested_exception(const nested_exception&) noexcept = default;
00060 
00061     nested_exception& operator=(const nested_exception&) noexcept = default;
00062 
00063     virtual ~nested_exception() noexcept;
00064 
00065     [[noreturn]]
00066     void
00067     rethrow_nested() const
00068     {
00069       if (_M_ptr)
00070         rethrow_exception(_M_ptr);
00071       std::terminate();
00072     }
00073 
00074     exception_ptr
00075     nested_ptr() const noexcept
00076     { return _M_ptr; }
00077   };
00078 
00079   template<typename _Except>
00080     struct _Nested_exception : public _Except, public nested_exception
00081     {
00082       explicit _Nested_exception(const _Except& __ex)
00083       : _Except(__ex)
00084       { }
00085 
00086       explicit _Nested_exception(_Except&& __ex)
00087       : _Except(static_cast<_Except&&>(__ex))
00088       { }
00089     };
00090 
00091   // [except.nested]/8
00092   // Throw an exception of unspecified type that is publicly derived from
00093   // both remove_reference_t<_Tp> and nested_exception.
00094   template<typename _Tp>
00095     inline void
00096     __throw_with_nested_impl(_Tp&& __t, true_type)
00097     {
00098       using _Up = typename remove_reference<_Tp>::type;
00099       throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
00100     }
00101 
00102   template<typename _Tp>
00103     inline void
00104     __throw_with_nested_impl(_Tp&& __t, false_type)
00105     { throw std::forward<_Tp>(__t); }
00106 
00107   /// If @p __t is derived from nested_exception, throws @p __t.
00108   /// Else, throws an implementation-defined object derived from both.
00109   template<typename _Tp>
00110     [[noreturn]]
00111     inline void
00112     throw_with_nested(_Tp&& __t)
00113     {
00114       using _Up = typename decay<_Tp>::type;
00115       using _CopyConstructible
00116         = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
00117       static_assert(_CopyConstructible::value,
00118           "throw_with_nested argument must be CopyConstructible");
00119       using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
00120                             __not_<is_base_of<nested_exception, _Up>>>;
00121       std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
00122     }
00123 
00124   // Determine if dynamic_cast<const nested_exception&> would be well-formed.
00125   template<typename _Tp>
00126     using __rethrow_if_nested_cond = typename enable_if<
00127       __and_<is_polymorphic<_Tp>,
00128              __or_<__not_<is_base_of<nested_exception, _Tp>>,
00129                    is_convertible<_Tp*, nested_exception*>>>::value
00130     >::type;
00131 
00132   // Attempt dynamic_cast to nested_exception and call rethrow_nested().
00133   template<typename _Ex>
00134     inline __rethrow_if_nested_cond<_Ex>
00135     __rethrow_if_nested_impl(const _Ex* __ptr)
00136     {
00137       if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
00138         __ne_ptr->rethrow_nested();
00139     }
00140 
00141   // Otherwise, no effects.
00142   inline void
00143   __rethrow_if_nested_impl(const void*)
00144   { }
00145 
00146   /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
00147   template<typename _Ex>
00148     inline void
00149     rethrow_if_nested(const _Ex& __ex)
00150     { std::__rethrow_if_nested_impl(std::__addressof(__ex)); }
00151 
00152   // @} group exceptions
00153 } // namespace std
00154 
00155 } // extern "C++"
00156 
00157 #endif // C++11
00158 
00159 #pragma GCC visibility pop
00160 
00161 #endif // _GLIBCXX_NESTED_EXCEPTION_H