exception_ptr.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
  9. #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
  10. #include <__config>
  11. #include <__exception/operations.h>
  12. #include <__memory/addressof.h>
  13. #include <cstddef>
  14. #include <cstdlib>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. namespace std { // purposefully not using versioning namespace
  19. class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
  20. void* __ptr_;
  21. public:
  22. _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
  23. _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
  24. exception_ptr(const exception_ptr&) _NOEXCEPT;
  25. exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
  26. ~exception_ptr() _NOEXCEPT;
  27. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; }
  28. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
  29. return __x.__ptr_ == __y.__ptr_;
  30. }
  31. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
  32. return !(__x == __y);
  33. }
  34. friend _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT
  35. {
  36. void* __tmp = __x.__ptr_;
  37. __x.__ptr_ = __y.__ptr_;
  38. __y.__ptr_ = __tmp;
  39. }
  40. friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
  41. friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
  42. };
  43. #ifndef _LIBCPP_ABI_MICROSOFT
  44. template <class _Ep>
  45. _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
  46. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  47. try {
  48. throw __e;
  49. } catch (...) {
  50. return current_exception();
  51. }
  52. # else
  53. ((void)__e);
  54. std::abort();
  55. # endif
  56. }
  57. #else // _LIBCPP_ABI_MICROSOFT
  58. _LIBCPP_HIDE_FROM_ABI exception_ptr __copy_exception_ptr(void *__exception, const void* __ptr);
  59. // This is a built-in template function which automagically extracts the required
  60. // information.
  61. template <class _E>
  62. void* __GetExceptionInfo(_E);
  63. template <class _Ep>
  64. exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
  65. return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e));
  66. }
  67. #endif // _LIBCPP_ABI_MICROSOFT
  68. } // namespace std
  69. #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H