Home | History | Annotate | Download | only in bits
      1 // Exception Handling support header (exception_ptr class) for -*- C++ -*-
      2 
      3 // Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation
      4 //
      5 // This file is part of GCC.
      6 //
      7 // GCC is free software; you can redistribute it and/or modify
      8 // it under the terms of the GNU General Public License as published by
      9 // the Free Software Foundation; either version 3, or (at your option)
     10 // any later version.
     11 //
     12 // GCC is distributed in the hope that it will be useful,
     13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 // GNU General Public License for more details.
     16 //
     17 // Under Section 7 of GPL version 3, you are granted additional
     18 // permissions described in the GCC Runtime Library Exception, version
     19 // 3.1, as published by the Free Software Foundation.
     20 
     21 // You should have received a copy of the GNU General Public License and
     22 // a copy of the GCC Runtime Library Exception along with this program;
     23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 // <http://www.gnu.org/licenses/>.
     25 
     26 /** @file bits/exception_ptr.h
     27  *  This is an internal header file, included by other library headers.
     28  *  Do not attempt to use it directly. @headername{exception}
     29  */
     30 
     31 #ifndef _EXCEPTION_PTR_H
     32 #define _EXCEPTION_PTR_H
     33 
     34 #pragma GCC visibility push(default)
     35 
     36 #include <bits/c++config.h>
     37 #include <bits/exception_defines.h>
     38 
     39 #if ATOMIC_INT_LOCK_FREE < 2
     40 #  error This platform does not support exception propagation.
     41 #endif
     42 
     43 extern "C++" {
     44 
     45 namespace std
     46 {
     47   /**
     48    * @addtogroup exceptions
     49    * @{
     50    */
     51   namespace __exception_ptr
     52   {
     53     class exception_ptr;
     54   }
     55 
     56   using __exception_ptr::exception_ptr;
     57 
     58   /** Obtain an exception_ptr to the currently handled exception. If there
     59    *  is none, or the currently handled exception is foreign, return the null
     60    *  value.
     61    */
     62   exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
     63 
     64   /// Throw the object pointed to by the exception_ptr.
     65   void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__));
     66 
     67   namespace __exception_ptr
     68   {
     69     /**
     70      *  @brief An opaque pointer to an arbitrary exception.
     71      *  @ingroup exceptions
     72      */
     73     class exception_ptr
     74     {
     75       void* _M_exception_object;
     76 
     77       explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT;
     78 
     79       void _M_addref() _GLIBCXX_USE_NOEXCEPT;
     80       void _M_release() _GLIBCXX_USE_NOEXCEPT;
     81 
     82       void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__));
     83 
     84       friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
     85       friend void std::rethrow_exception(exception_ptr);
     86 
     87     public:
     88       exception_ptr() _GLIBCXX_USE_NOEXCEPT;
     89 
     90       exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
     91 
     92 #ifdef __GXX_EXPERIMENTAL_CXX0X__
     93       exception_ptr(nullptr_t) noexcept
     94       : _M_exception_object(0)
     95       { }
     96 
     97       exception_ptr(exception_ptr&& __o) noexcept
     98       : _M_exception_object(__o._M_exception_object)
     99       { __o._M_exception_object = 0; }
    100 #endif
    101 
    102 #if !defined (__GXX_EXPERIMENTAL_CXX0X__) || defined (_GLIBCXX_EH_PTR_COMPAT)
    103       typedef void (exception_ptr::*__safe_bool)();
    104 
    105       // For construction from nullptr or 0.
    106       exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT;
    107 #endif
    108 
    109       exception_ptr&
    110       operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
    111 
    112 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    113       exception_ptr&
    114       operator=(exception_ptr&& __o) noexcept
    115       {
    116         exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this);
    117         return *this;
    118       }
    119 #endif
    120 
    121       ~exception_ptr() _GLIBCXX_USE_NOEXCEPT;
    122 
    123       void
    124       swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
    125 
    126 #ifdef _GLIBCXX_EH_PTR_COMPAT
    127       // Retained for compatibility with CXXABI_1.3.
    128       void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT
    129 	__attribute__ ((__const__));
    130       bool operator!() const _GLIBCXX_USE_NOEXCEPT
    131 	__attribute__ ((__pure__));
    132       operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT;
    133 #endif
    134 
    135 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    136       explicit operator bool() const
    137       { return _M_exception_object; }
    138 #endif
    139 
    140       friend bool
    141       operator==(const exception_ptr&, const exception_ptr&)
    142 	_GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
    143 
    144       const class type_info*
    145       __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT
    146 	__attribute__ ((__pure__));
    147     };
    148 
    149     bool
    150     operator==(const exception_ptr&, const exception_ptr&)
    151       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
    152 
    153     bool
    154     operator!=(const exception_ptr&, const exception_ptr&)
    155       _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
    156 
    157     inline void
    158     swap(exception_ptr& __lhs, exception_ptr& __rhs)
    159     { __lhs.swap(__rhs); }
    160 
    161   } // namespace __exception_ptr
    162 
    163 
    164   /// Obtain an exception_ptr pointing to a copy of the supplied object.
    165   template<typename _Ex>
    166     exception_ptr
    167     copy_exception(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
    168     {
    169       __try
    170 	{
    171 #ifdef __EXCEPTIONS
    172 	  throw __ex;
    173 #endif
    174 	}
    175       __catch(...)
    176 	{
    177 	  return current_exception();
    178 	}
    179     }
    180 
    181   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    182   // 1130. copy_exception name misleading
    183   /// Obtain an exception_ptr pointing to a copy of the supplied object.
    184   template<typename _Ex>
    185     exception_ptr
    186     make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
    187     { return std::copy_exception<_Ex>(__ex); }
    188 
    189   // @} group exceptions
    190 } // namespace std
    191 
    192 } // extern "C++"
    193 
    194 #pragma GCC visibility pop
    195 
    196 #endif
    197