libstdc++
exception_ptr.h
Go to the documentation of this file.
00001 // Exception Handling support header (exception_ptr class) for -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of GCC.
00006 //
00007 // GCC is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 // 
00012 // GCC is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file bits/exception_ptr.h
00027  *  This is an internal header file, included by other library headers.
00028  *  Do not attempt to use it directly. @headername{exception}
00029  */
00030 
00031 #ifndef _EXCEPTION_PTR_H
00032 #define _EXCEPTION_PTR_H
00033 
00034 #pragma GCC visibility push(default)
00035 
00036 #include <bits/c++config.h>
00037 #include <bits/exception_defines.h>
00038 #include <bits/cxxabi_init_exception.h>
00039 #include <typeinfo>
00040 #include <new>
00041 
00042 extern "C++" {
00043 
00044 namespace std 
00045 {
00046   class type_info;
00047 
00048   /**
00049    * @addtogroup exceptions
00050    * @{
00051    */
00052   namespace __exception_ptr
00053   {
00054     class exception_ptr;
00055   }
00056 
00057   using __exception_ptr::exception_ptr;
00058 
00059   /** Obtain an exception_ptr to the currently handled exception. If there
00060    *  is none, or the currently handled exception is foreign, return the null
00061    *  value.
00062    */
00063   exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
00064 
00065   template<typename _Ex>
00066   exception_ptr make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
00067 
00068   /// Throw the object pointed to by the exception_ptr.
00069   void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__));
00070 
00071   namespace __exception_ptr
00072   {
00073     using std::rethrow_exception;
00074 
00075     /**
00076      *  @brief An opaque pointer to an arbitrary exception.
00077      *  @ingroup exceptions
00078      */
00079     class exception_ptr
00080     {
00081       void* _M_exception_object;
00082 
00083       explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT;
00084 
00085       void _M_addref() _GLIBCXX_USE_NOEXCEPT;
00086       void _M_release() _GLIBCXX_USE_NOEXCEPT;
00087 
00088       void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__));
00089 
00090       friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
00091       friend void std::rethrow_exception(exception_ptr);
00092       template<typename _Ex>
00093       friend exception_ptr std::make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT;
00094 
00095     public:
00096       exception_ptr() _GLIBCXX_USE_NOEXCEPT;
00097 
00098       exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
00099 
00100 #if __cplusplus >= 201103L
00101       exception_ptr(nullptr_t) noexcept
00102       : _M_exception_object(0)
00103       { }
00104 
00105       exception_ptr(exception_ptr&& __o) noexcept
00106       : _M_exception_object(__o._M_exception_object)
00107       { __o._M_exception_object = 0; }
00108 #endif
00109 
00110 #if (__cplusplus < 201103L) || defined (_GLIBCXX_EH_PTR_COMPAT)
00111       typedef void (exception_ptr::*__safe_bool)();
00112 
00113       // For construction from nullptr or 0.
00114       exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT;
00115 #endif
00116 
00117       exception_ptr& 
00118       operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
00119 
00120 #if __cplusplus >= 201103L
00121       exception_ptr& 
00122       operator=(exception_ptr&& __o) noexcept
00123       {
00124         exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this);
00125         return *this;
00126       }
00127 #endif
00128 
00129       ~exception_ptr() _GLIBCXX_USE_NOEXCEPT;
00130 
00131       void 
00132       swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
00133 
00134 #ifdef _GLIBCXX_EH_PTR_COMPAT
00135       // Retained for compatibility with CXXABI_1.3.
00136       void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT
00137         __attribute__ ((__const__));
00138       bool operator!() const _GLIBCXX_USE_NOEXCEPT
00139         __attribute__ ((__pure__));
00140       operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT;
00141 #endif
00142 
00143 #if __cplusplus >= 201103L
00144       explicit operator bool() const
00145       { return _M_exception_object; }
00146 #endif
00147 
00148       friend bool 
00149       operator==(const exception_ptr&, const exception_ptr&)
00150         _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
00151 
00152       const class std::type_info*
00153       __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT
00154         __attribute__ ((__pure__));
00155     };
00156 
00157     bool 
00158     operator==(const exception_ptr&, const exception_ptr&)
00159       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
00160 
00161     bool 
00162     operator!=(const exception_ptr&, const exception_ptr&)
00163       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
00164 
00165     inline void
00166     swap(exception_ptr& __lhs, exception_ptr& __rhs)
00167     { __lhs.swap(__rhs); }
00168 
00169     template<typename _Ex>
00170       inline void
00171       __dest_thunk(void* __x)
00172       { static_cast<_Ex*>(__x)->~_Ex(); }
00173 
00174   } // namespace __exception_ptr
00175 
00176   /// Obtain an exception_ptr pointing to a copy of the supplied object.
00177   template<typename _Ex>
00178     exception_ptr 
00179     make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
00180     {
00181 #if __cpp_exceptions && __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI
00182       void* __e = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ex));
00183       (void) __cxxabiv1::__cxa_init_primary_exception(
00184           __e, const_cast<std::type_info*>(&typeid(__ex)),
00185           __exception_ptr::__dest_thunk<_Ex>);
00186       try
00187         {
00188           ::new (__e) _Ex(__ex);
00189           return exception_ptr(__e);
00190         }
00191       catch(...)
00192         {
00193           __cxxabiv1::__cxa_free_exception(__e);
00194           return current_exception();
00195         }
00196 #elif __cpp_exceptions
00197       try
00198         {
00199           throw __ex;
00200         }
00201       catch(...)
00202         {
00203           return current_exception();
00204         }
00205 #else // no RTTI and no exceptions
00206       return exception_ptr();
00207 #endif
00208     }
00209 
00210   // @} group exceptions
00211 } // namespace std
00212 
00213 } // extern "C++"
00214 
00215 #pragma GCC visibility pop
00216 
00217 #endif