exception_ptr.h 5.47 KB
Newer Older
Paolo Carlini committed
1 2
// Exception Handling support header (exception_ptr class) for -*- C++ -*-

3
// Copyright (C) 2008-2013 Free Software Foundation, Inc.
Paolo Carlini committed
4 5 6 7 8
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify
// 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)
Paolo Carlini committed
10 11 12 13 14 15 16
// any later version.
// 
// GCC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
17 18 19
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
Paolo Carlini committed
20

21 22 23 24
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.
Paolo Carlini committed
25

26
/** @file bits/exception_ptr.h
27 28
 *  This is an internal header file, included by other library headers.
 *  Do not attempt to use it directly. @headername{exception}
Paolo Carlini committed
29 30 31 32 33 34 35 36
 */

#ifndef _EXCEPTION_PTR_H
#define _EXCEPTION_PTR_H

#pragma GCC visibility push(default)

#include <bits/c++config.h>
37
#include <bits/exception_defines.h>
Paolo Carlini committed
38

39
#if ATOMIC_INT_LOCK_FREE < 2
Paolo Carlini committed
40 41 42 43 44 45 46
#  error This platform does not support exception propagation.
#endif

extern "C++" {

namespace std 
{
47 48
  class type_info;

Benjamin Kosnik committed
49 50 51 52
  /**
   * @addtogroup exceptions
   * @{
   */
Paolo Carlini committed
53 54 55 56 57 58 59
  namespace __exception_ptr
  {
    class exception_ptr;
  }

  using __exception_ptr::exception_ptr;

60
  /** Obtain an exception_ptr to the currently handled exception. If there
Paolo Carlini committed
61 62 63
   *  is none, or the currently handled exception is foreign, return the null
   *  value.
   */
64
  exception_ptr current_exception() _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
65

66
  /// Throw the object pointed to by the exception_ptr.
Paolo Carlini committed
67 68 69 70
  void rethrow_exception(exception_ptr) __attribute__ ((__noreturn__));

  namespace __exception_ptr
  {
71 72 73 74
    /**
     *  @brief An opaque pointer to an arbitrary exception.
     *  @ingroup exceptions
     */
Paolo Carlini committed
75 76 77 78
    class exception_ptr
    {
      void* _M_exception_object;

79
      explicit exception_ptr(void* __e) _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
80

81 82
      void _M_addref() _GLIBCXX_USE_NOEXCEPT;
      void _M_release() _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
83

84
      void *_M_get() const _GLIBCXX_NOEXCEPT __attribute__ ((__pure__));
Paolo Carlini committed
85

86
      friend exception_ptr std::current_exception() _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
87 88 89
      friend void std::rethrow_exception(exception_ptr);

    public:
90
      exception_ptr() _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
91

92
      exception_ptr(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
93

94
#if __cplusplus >= 201103L
95
      exception_ptr(nullptr_t) noexcept
96 97 98
      : _M_exception_object(0)
      { }

99
      exception_ptr(exception_ptr&& __o) noexcept
Benjamin Kosnik committed
100 101
      : _M_exception_object(__o._M_exception_object)
      { __o._M_exception_object = 0; }
102 103
#endif

104
#if (__cplusplus < 201103L) || defined (_GLIBCXX_EH_PTR_COMPAT)
105 106 107
      typedef void (exception_ptr::*__safe_bool)();

      // For construction from nullptr or 0.
108
      exception_ptr(__safe_bool) _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
109 110
#endif

Benjamin Kosnik committed
111
      exception_ptr& 
112
      operator=(const exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
113

114
#if __cplusplus >= 201103L
Benjamin Kosnik committed
115
      exception_ptr& 
116
      operator=(exception_ptr&& __o) noexcept
Paolo Carlini committed
117
      {
118
        exception_ptr(static_cast<exception_ptr&&>(__o)).swap(*this);
Paolo Carlini committed
119 120 121 122
        return *this;
      }
#endif

123
      ~exception_ptr() _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
124

Benjamin Kosnik committed
125
      void 
126
      swap(exception_ptr&) _GLIBCXX_USE_NOEXCEPT;
Paolo Carlini committed
127

128 129
#ifdef _GLIBCXX_EH_PTR_COMPAT
      // Retained for compatibility with CXXABI_1.3.
130 131 132 133 134
      void _M_safe_bool_dummy() _GLIBCXX_USE_NOEXCEPT
	__attribute__ ((__const__));
      bool operator!() const _GLIBCXX_USE_NOEXCEPT
	__attribute__ ((__pure__));
      operator __safe_bool() const _GLIBCXX_USE_NOEXCEPT;
135
#endif
Paolo Carlini committed
136

137
#if __cplusplus >= 201103L
138 139 140 141
      explicit operator bool() const
      { return _M_exception_object; }
#endif

Benjamin Kosnik committed
142
      friend bool 
143 144
      operator==(const exception_ptr&, const exception_ptr&)
	_GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
Paolo Carlini committed
145

146
      const class std::type_info*
147 148
      __cxa_exception_type() const _GLIBCXX_USE_NOEXCEPT
	__attribute__ ((__pure__));
Paolo Carlini committed
149 150
    };

151
    bool 
152 153
    operator==(const exception_ptr&, const exception_ptr&)
      _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
154 155

    bool 
156 157
    operator!=(const exception_ptr&, const exception_ptr&)
      _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
158 159 160 161 162

    inline void
    swap(exception_ptr& __lhs, exception_ptr& __rhs)
    { __lhs.swap(__rhs); }

Paolo Carlini committed
163 164 165
  } // namespace __exception_ptr


166
  /// Obtain an exception_ptr pointing to a copy of the supplied object.
Benjamin Kosnik committed
167 168
  template<typename _Ex>
    exception_ptr 
169
    make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
Benjamin Kosnik committed
170 171 172
    {
      __try
	{
173
#ifdef __EXCEPTIONS
Benjamin Kosnik committed
174
	  throw __ex;
175
#endif
Benjamin Kosnik committed
176 177 178
	}
      __catch(...)
	{
179
	  return current_exception();
Benjamin Kosnik committed
180 181 182
	}
    }

183 184 185
  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 1130. copy_exception name misleading
  /// Obtain an exception_ptr pointing to a copy of the supplied object.
186
  /// This function is deprecated, use std::make_exception_ptr instead.
187
  template<typename _Ex>
188 189 190 191 192 193 194
    exception_ptr
    copy_exception(_Ex __ex) _GLIBCXX_USE_NOEXCEPT _GLIBCXX_DEPRECATED;

  template<typename _Ex>
    exception_ptr
    copy_exception(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
    { return std::make_exception_ptr<_Ex>(__ex); }
195

Benjamin Kosnik committed
196
  // @} group exceptions
Paolo Carlini committed
197 198 199 200 201 202 203
} // namespace std

} // extern "C++"

#pragma GCC visibility pop

#endif