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