1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H |
11 | #define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H |
12 | |
13 | #include <__compare/synth_three_way.h> |
14 | #include <__config> |
15 | #include <__functional/weak_result_type.h> |
16 | #include <__memory/addressof.h> |
17 | #include <__type_traits/desugars_to.h> |
18 | #include <__type_traits/enable_if.h> |
19 | #include <__type_traits/invoke.h> |
20 | #include <__type_traits/is_const.h> |
21 | #include <__type_traits/is_core_convertible.h> |
22 | #include <__type_traits/is_same.h> |
23 | #include <__type_traits/remove_cvref.h> |
24 | #include <__type_traits/void_t.h> |
25 | #include <__utility/declval.h> |
26 | #include <__utility/forward.h> |
27 | |
28 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
29 | # pragma GCC system_header |
30 | #endif |
31 | |
32 | _LIBCPP_BEGIN_NAMESPACE_STD |
33 | |
34 | template <class _Tp> |
35 | class reference_wrapper : public __weak_result_type<_Tp> { |
36 | public: |
37 | // types |
38 | typedef _Tp type; |
39 | |
40 | private: |
41 | type* __f_; |
42 | |
43 | static void __fun(_Tp&) _NOEXCEPT; |
44 | static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR54276 |
45 | |
46 | public: |
47 | template <class _Up, |
48 | class = __void_t<decltype(__fun(std::declval<_Up>()))>, |
49 | __enable_if_t<!is_same<__remove_cvref_t<_Up>, reference_wrapper>::value, int> = 0> |
50 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u) |
51 | _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) { |
52 | type& __f = static_cast<_Up&&>(__u); |
53 | __f_ = std::addressof(__f); |
54 | } |
55 | |
56 | // access |
57 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator type&() const _NOEXCEPT { return *__f_; } |
58 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 type& get() const _NOEXCEPT { return *__f_; } |
59 | |
60 | // invoke |
61 | template <class... _ArgTypes> |
62 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<type&, _ArgTypes...> |
63 | operator()(_ArgTypes&&... __args) const |
64 | #if _LIBCPP_STD_VER >= 17 |
65 | // Since is_nothrow_invocable requires C++17 LWG3764 is not backported |
66 | // to earlier versions. |
67 | noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>) |
68 | #endif |
69 | { |
70 | return std::__invoke(get(), std::forward<_ArgTypes>(__args)...); |
71 | } |
72 | |
73 | #if _LIBCPP_STD_VER >= 26 |
74 | |
75 | // [refwrap.comparisons], comparisons |
76 | |
77 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y) |
78 | requires requires { |
79 | { __x.get() == __y.get() } -> __core_convertible_to<bool>; |
80 | } |
81 | { |
82 | return __x.get() == __y.get(); |
83 | } |
84 | |
85 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y) |
86 | requires requires { |
87 | { __x.get() == __y } -> __core_convertible_to<bool>; |
88 | } |
89 | { |
90 | return __x.get() == __y; |
91 | } |
92 | |
93 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y) |
94 | requires(!is_const_v<_Tp>) && requires { |
95 | { __x.get() == __y.get() } -> __core_convertible_to<bool>; |
96 | } |
97 | { |
98 | return __x.get() == __y.get(); |
99 | } |
100 | |
101 | _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper __y) |
102 | requires requires { std::__synth_three_way(__x.get(), __y.get()); } |
103 | { |
104 | return std::__synth_three_way(__x.get(), __y.get()); |
105 | } |
106 | |
107 | _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, const _Tp& __y) |
108 | requires requires { std::__synth_three_way(__x.get(), __y); } |
109 | { |
110 | return std::__synth_three_way(__x.get(), __y); |
111 | } |
112 | |
113 | _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(reference_wrapper __x, reference_wrapper<const _Tp> __y) |
114 | requires(!is_const_v<_Tp>) && requires { std::__synth_three_way(__x.get(), __y.get()); } |
115 | { |
116 | return std::__synth_three_way(__x.get(), __y.get()); |
117 | } |
118 | |
119 | #endif // _LIBCPP_STD_VER >= 26 |
120 | }; |
121 | |
122 | #if _LIBCPP_STD_VER >= 17 |
123 | template <class _Tp> |
124 | reference_wrapper(_Tp&) -> reference_wrapper<_Tp>; |
125 | #endif |
126 | |
127 | template <class _Tp> |
128 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT { |
129 | return reference_wrapper<_Tp>(__t); |
130 | } |
131 | |
132 | template <class _Tp> |
133 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> |
134 | ref(reference_wrapper<_Tp> __t) _NOEXCEPT { |
135 | return __t; |
136 | } |
137 | |
138 | template <class _Tp> |
139 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT { |
140 | return reference_wrapper<const _Tp>(__t); |
141 | } |
142 | |
143 | template <class _Tp> |
144 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<const _Tp> |
145 | cref(reference_wrapper<_Tp> __t) _NOEXCEPT { |
146 | return __t; |
147 | } |
148 | |
149 | template <class _Tp> |
150 | void ref(const _Tp&&) = delete; |
151 | template <class _Tp> |
152 | void cref(const _Tp&&) = delete; |
153 | |
154 | // Let desugars-to pass through std::reference_wrapper |
155 | template <class _CanonicalTag, class _Operation, class... _Args> |
156 | inline const bool __desugars_to_v<_CanonicalTag, reference_wrapper<_Operation>, _Args...> = |
157 | __desugars_to_v<_CanonicalTag, _Operation, _Args...>; |
158 | |
159 | _LIBCPP_END_NAMESPACE_STD |
160 | |
161 | #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H |
162 | |