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-2019 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     [[noreturn]]
00096     inline void
00097     __throw_with_nested_impl(_Tp&& __t, true_type)
00098     {
00099       using _Up = typename remove_reference<_Tp>::type;
00100       throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
00101     }
00102 
00103   template<typename _Tp>
00104     [[noreturn]]
00105     inline void
00106     __throw_with_nested_impl(_Tp&& __t, false_type)
00107     { throw std::forward<_Tp>(__t); }
00108 
00109   /// If @p __t is derived from nested_exception, throws @p __t.
00110   /// Else, throws an implementation-defined object derived from both.
00111   template<typename _Tp>
00112     [[noreturn]]
00113     inline void
00114     throw_with_nested(_Tp&& __t)
00115     {
00116       using _Up = typename decay<_Tp>::type;
00117       using _CopyConstructible
00118         = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
00119       static_assert(_CopyConstructible::value,
00120           "throw_with_nested argument must be CopyConstructible");
00121       using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
00122                             __not_<is_base_of<nested_exception, _Up>>>;
00123       std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
00124     }
00125 
00126   // Determine if dynamic_cast<const nested_exception&> would be well-formed.
00127   template<typename _Tp>
00128     using __rethrow_if_nested_cond = typename enable_if<
00129       __and_<is_polymorphic<_Tp>,
00130              __or_<__not_<is_base_of<nested_exception, _Tp>>,
00131                    is_convertible<_Tp*, nested_exception*>>>::value
00132     >::type;
00133 
00134   // Attempt dynamic_cast to nested_exception and call rethrow_nested().
00135   template<typename _Ex>
00136     inline __rethrow_if_nested_cond<_Ex>
00137     __rethrow_if_nested_impl(const _Ex* __ptr)
00138     {
00139       if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
00140         __ne_ptr->rethrow_nested();
00141     }
00142 
00143   // Otherwise, no effects.
00144   inline void
00145   __rethrow_if_nested_impl(const void*)
00146   { }
00147 
00148   /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
00149   template<typename _Ex>
00150     inline void
00151     rethrow_if_nested(const _Ex& __ex)
00152     { std::__rethrow_if_nested_impl(std::__addressof(__ex)); }
00153 
00154   // @} group exceptions
00155 } // namespace std
00156 
00157 } // extern "C++"
00158 
00159 #endif // C++11
00160 
00161 #pragma GCC visibility pop
00162 
00163 #endif // _GLIBCXX_NESTED_EXCEPTION_H