xref: /freebsd/contrib/llvm-project/libcxx/include/__exception/exception_ptr.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 
9 #ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
10 #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
11 
12 #include <__config>
13 #include <__exception/operations.h>
14 #include <__memory/addressof.h>
15 #include <cstddef>
16 #include <cstdlib>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 namespace std { // purposefully not using versioning namespace
23 
24 #ifndef _LIBCPP_ABI_MICROSOFT
25 
26 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
27   void* __ptr_;
28 
29 public:
30   _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
31   _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
32 
33   exception_ptr(const exception_ptr&) _NOEXCEPT;
34   exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
35   ~exception_ptr() _NOEXCEPT;
36 
37   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; }
38 
39   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
40     return __x.__ptr_ == __y.__ptr_;
41   }
42 
43   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
44     return !(__x == __y);
45   }
46 
47   friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
48   friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
49 };
50 
51 template <class _Ep>
52 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
53 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
54   try {
55     throw __e;
56   } catch (...) {
57     return current_exception();
58   }
59 #  else
60   ((void)__e);
61   std::abort();
62 #  endif
63 }
64 
65 #else // _LIBCPP_ABI_MICROSOFT
66 
67 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
68   _LIBCPP_DIAGNOSTIC_PUSH
69   _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
70   void* __ptr1_;
71   void* __ptr2_;
72   _LIBCPP_DIAGNOSTIC_POP
73 
74 public:
75   exception_ptr() _NOEXCEPT;
76   exception_ptr(nullptr_t) _NOEXCEPT;
77   exception_ptr(const exception_ptr& __other) _NOEXCEPT;
78   exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
79   exception_ptr& operator=(nullptr_t) _NOEXCEPT;
80   ~exception_ptr() _NOEXCEPT;
81   explicit operator bool() const _NOEXCEPT;
82 };
83 
84 _LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
85 
86 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
87   return !(__x == __y);
88 }
89 
90 _LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
91 
92 _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr);
93 _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
94 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
95 
96 // This is a built-in template function which automagically extracts the required
97 // information.
98 template <class _E>
99 void* __GetExceptionInfo(_E);
100 
101 template <class _Ep>
102 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
103   return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e));
104 }
105 
106 #endif // _LIBCPP_ABI_MICROSOFT
107 } // namespace std
108 
109 #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
110