You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.9KB

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