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, 2010, 2011 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
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&) = default;
00063 
00064     nested_exception& operator=(const nested_exception&) = default;
00065 
00066     virtual ~nested_exception() noexcept;
00067 
00068     void
00069     rethrow_nested() const __attribute__ ((__noreturn__))
00070     { rethrow_exception(_M_ptr); }
00071 
00072     exception_ptr
00073     nested_ptr() const
00074     { return _M_ptr; }
00075   };
00076 
00077   template<typename _Except>
00078     struct _Nested_exception : public _Except, public nested_exception
00079     {
00080       explicit _Nested_exception(_Except&& __ex)
00081       : _Except(static_cast<_Except&&>(__ex))
00082       { }
00083     };
00084 
00085   template<typename _Ex>
00086     struct __get_nested_helper
00087     {
00088       static const nested_exception*
00089       _S_get(const _Ex& __ex)
00090       { return dynamic_cast<const nested_exception*>(&__ex); }
00091     };
00092 
00093   template<typename _Ex>
00094     struct __get_nested_helper<_Ex*>
00095     {
00096       static const nested_exception*
00097       _S_get(const _Ex* __ex)
00098       { return dynamic_cast<const nested_exception*>(__ex); }
00099     };
00100 
00101   template<typename _Ex>
00102     inline const nested_exception*
00103     __get_nested_exception(const _Ex& __ex)
00104     { return __get_nested_helper<_Ex>::_S_get(__ex); }
00105 
00106   template<typename _Ex>
00107     void
00108     __throw_with_nested(_Ex&&, const nested_exception* = 0)
00109     __attribute__ ((__noreturn__));
00110 
00111   template<typename _Ex>
00112     void
00113     __throw_with_nested(_Ex&&, ...) __attribute__ ((__noreturn__));
00114 
00115   // This function should never be called, but is needed to avoid a warning
00116   // about ambiguous base classes when instantiating throw_with_nested<_Ex>()
00117   // with a type that has an accessible nested_exception base.
00118   template<typename _Ex>
00119     inline void
00120     __throw_with_nested(_Ex&& __ex, const nested_exception*)
00121     { throw __ex; }
00122 
00123   template<typename _Ex>
00124     inline void
00125     __throw_with_nested(_Ex&& __ex, ...)
00126     { throw _Nested_exception<_Ex>(static_cast<_Ex&&>(__ex)); }
00127   
00128   template<typename _Ex>
00129     void
00130     throw_with_nested(_Ex __ex) __attribute__ ((__noreturn__));
00131 
00132   /// If @p __ex is derived from nested_exception, @p __ex. 
00133   /// Else, an implementation-defined object derived from both.
00134   template<typename _Ex>
00135     inline void
00136     throw_with_nested(_Ex __ex)
00137     {
00138       if (__get_nested_exception(__ex))
00139         throw __ex;
00140       __throw_with_nested(static_cast<_Ex&&>(__ex), &__ex);
00141     }
00142 
00143   /// If @p __ex is derived from nested_exception, @p __ex.rethrow_nested().
00144   template<typename _Ex>
00145     inline void
00146     rethrow_if_nested(const _Ex& __ex)
00147     {
00148       if (const nested_exception* __nested = __get_nested_exception(__ex))
00149         __nested->rethrow_nested();
00150     }
00151 
00152   /// Overload, See N2619
00153   inline void
00154   rethrow_if_nested(const nested_exception& __ex)
00155   { __ex.rethrow_nested(); }
00156 
00157   // @} group exceptions
00158 } // namespace std
00159 
00160 } // extern "C++"
00161 
00162 #endif // __GXX_EXPERIMENTAL_CXX0X__
00163 
00164 #pragma GCC visibility pop
00165 
00166 #endif // _GLIBCXX_NESTED_EXCEPTION_H