exception_ptr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Exception Handling support header (exception_ptr class) for -*- C++ -*-
  2. // Copyright (C) 2008-2018 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. bool
  124. operator==(const exception_ptr&, const exception_ptr&)
  125. _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
  126. bool
  127. operator!=(const exception_ptr&, const exception_ptr&)
  128. _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
  129. inline void
  130. swap(exception_ptr& __lhs, exception_ptr& __rhs)
  131. { __lhs.swap(__rhs); }
  132. template<typename _Ex>
  133. inline void
  134. __dest_thunk(void* __x)
  135. { static_cast<_Ex*>(__x)->~_Ex(); }
  136. } // namespace __exception_ptr
  137. /// Obtain an exception_ptr pointing to a copy of the supplied object.
  138. template<typename _Ex>
  139. exception_ptr
  140. make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT
  141. {
  142. #if __cpp_exceptions && __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI
  143. void* __e = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ex));
  144. (void) __cxxabiv1::__cxa_init_primary_exception(
  145. __e, const_cast<std::type_info*>(&typeid(__ex)),
  146. __exception_ptr::__dest_thunk<_Ex>);
  147. try
  148. {
  149. ::new (__e) _Ex(__ex);
  150. return exception_ptr(__e);
  151. }
  152. catch(...)
  153. {
  154. __cxxabiv1::__cxa_free_exception(__e);
  155. return current_exception();
  156. }
  157. #elif __cpp_exceptions
  158. try
  159. {
  160. throw __ex;
  161. }
  162. catch(...)
  163. {
  164. return current_exception();
  165. }
  166. #else // no RTTI and no exceptions
  167. return exception_ptr();
  168. #endif
  169. }
  170. // @} group exceptions
  171. } // namespace std
  172. } // extern "C++"
  173. #pragma GCC visibility pop
  174. #endif