Debian Codegraph @

libcxx/include/__type_traits/invoke.h

raw
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___TYPE_TRAITS_INVOKE_H
11 #define _LIBCPP___TYPE_TRAITS_INVOKE_H
12
13 #include <__config>
14 #include <__type_traits/conditional.h>
15 #include <__type_traits/decay.h>
16 #include <__type_traits/enable_if.h>
17 #include <__type_traits/integral_constant.h>
18 #include <__type_traits/is_base_of.h>
19 #include <__type_traits/is_core_convertible.h>
20 #include <__type_traits/is_member_pointer.h>
21 #include <__type_traits/is_reference_wrapper.h>
22 #include <__type_traits/is_same.h>
23 #include <__type_traits/is_void.h>
24 #include <__type_traits/nat.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 _DecayedFp>
35 struct __member_pointer_class_type {};
36
37 template <class _Ret, class _ClassType>
38 struct __member_pointer_class_type<_Ret _ClassType::*> {
39 typedef _ClassType type;
40 };
41
42 template <class _Fp,
43 class _A0,
44 class _DecayFp = __decay_t<_Fp>,
45 class _DecayA0 = __decay_t<_A0>,
46 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
47 using __enable_if_bullet1 =
48 __enable_if_t<is_member_function_pointer<_DecayFp>::value &&
49 (is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value)>;
50
51 template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> >
52 using __enable_if_bullet2 =
53 __enable_if_t<is_member_function_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>;
54
55 template <class _Fp,
56 class _A0,
57 class _DecayFp = __decay_t<_Fp>,
58 class _DecayA0 = __decay_t<_A0>,
59 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
60 using __enable_if_bullet3 =
61 __enable_if_t<is_member_function_pointer<_DecayFp>::value &&
62 !(is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value) &&
63 !__is_reference_wrapper<_DecayA0>::value>;
64
65 template <class _Fp,
66 class _A0,
67 class _DecayFp = __decay_t<_Fp>,
68 class _DecayA0 = __decay_t<_A0>,
69 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
70 using __enable_if_bullet4 =
71 __enable_if_t<is_member_object_pointer<_DecayFp>::value &&
72 (is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value)>;
73
74 template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> >
75 using __enable_if_bullet5 =
76 __enable_if_t<is_member_object_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>;
77
78 template <class _Fp,
79 class _A0,
80 class _DecayFp = __decay_t<_Fp>,
81 class _DecayA0 = __decay_t<_A0>,
82 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
83 using __enable_if_bullet6 =
84 __enable_if_t<is_member_object_pointer<_DecayFp>::value &&
85 !(is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value) &&
86 !__is_reference_wrapper<_DecayA0>::value>;
87
88 // __invoke forward declarations
89
90 // fall back - none of the bullets
91
92 template <class... _Args>
93 __nat __invoke(_Args&&... __args);
94
95 // bullets 1, 2 and 3
96
97 // clang-format off
98 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet1<_Fp, _A0> >
99 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
100 decltype((std::declval<_A0>().*std::declval<_Fp>())(std::declval<_Args>()...))
101 __invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
102 _NOEXCEPT_(noexcept((static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...)))
103 { return (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...); }
104
105 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet2<_Fp, _A0> >
106 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
107 decltype((std::declval<_A0>().get().*std::declval<_Fp>())(std::declval<_Args>()...))
108 __invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
109 _NOEXCEPT_(noexcept((__a0.get().*__f)(static_cast<_Args&&>(__args)...)))
110 { return (__a0.get().*__f)(static_cast<_Args&&>(__args)...); }
111
112 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet3<_Fp, _A0> >
113 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
114 decltype(((*std::declval<_A0>()).*std::declval<_Fp>())(std::declval<_Args>()...))
115 __invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
116 _NOEXCEPT_(noexcept(((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...)))
117 { return ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...); }
118
119 // bullets 4, 5 and 6
120
121 template <class _Fp, class _A0, class = __enable_if_bullet4<_Fp, _A0> >
122 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
123 decltype(std::declval<_A0>().*std::declval<_Fp>())
124 __invoke(_Fp&& __f, _A0&& __a0)
125 _NOEXCEPT_(noexcept(static_cast<_A0&&>(__a0).*__f))
126 { return static_cast<_A0&&>(__a0).*__f; }
127
128 template <class _Fp, class _A0, class = __enable_if_bullet5<_Fp, _A0> >
129 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
130 decltype(std::declval<_A0>().get().*std::declval<_Fp>())
131 __invoke(_Fp&& __f, _A0&& __a0)
132 _NOEXCEPT_(noexcept(__a0.get().*__f))
133 { return __a0.get().*__f; }
134
135 template <class _Fp, class _A0, class = __enable_if_bullet6<_Fp, _A0> >
136 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
137 decltype((*std::declval<_A0>()).*std::declval<_Fp>())
138 __invoke(_Fp&& __f, _A0&& __a0)
139 _NOEXCEPT_(noexcept((*static_cast<_A0&&>(__a0)).*__f))
140 { return (*static_cast<_A0&&>(__a0)).*__f; }
141
142 // bullet 7
143
144 template <class _Fp, class... _Args>
145 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
146 decltype(std::declval<_Fp>()(std::declval<_Args>()...))
147 __invoke(_Fp&& __f, _Args&&... __args)
148 _NOEXCEPT_(noexcept(static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...)))
149 { return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }
150 // clang-format on
151
152 // __invokable
153 template <class _Ret, class _Fp, class... _Args>
154 struct __invokable_r {
155 template <class _XFp, class... _XArgs>
156 static decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...)) __try_call(int);
157 template <class _XFp, class... _XArgs>
158 static __nat __try_call(...);
159
160 // FIXME: Check that _Ret, _Fp, and _Args... are all complete types, cv void,
161 // or incomplete array types as required by the standard.
162 using _Result = decltype(__try_call<_Fp, _Args...>(0));
163
164 using type = __conditional_t<_IsNotSame<_Result, __nat>::value,
165 __conditional_t<is_void<_Ret>::value, true_type, __is_core_convertible<_Result, _Ret> >,
166 false_type>;
167 static const bool value = type::value;
168 };
169 template <class _Fp, class... _Args>
170 using __invokable = __invokable_r<void, _Fp, _Args...>;
171
172 template <bool _IsInvokable, bool _IsCVVoid, class _Ret, class _Fp, class... _Args>
173 struct __nothrow_invokable_r_imp {
174 static const bool value = false;
175 };
176
177 template <class _Ret, class _Fp, class... _Args>
178 struct __nothrow_invokable_r_imp<true, false, _Ret, _Fp, _Args...> {
179 typedef __nothrow_invokable_r_imp _ThisT;
180
181 template <class _Tp>
182 static void __test_noexcept(_Tp) _NOEXCEPT;
183
184 #ifdef _LIBCPP_CXX03_LANG
185 static const bool value = false;
186 #else
187 static const bool value =
188 noexcept(_ThisT::__test_noexcept<_Ret>(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...)));
189 #endif
190 };
191
192 template <class _Ret, class _Fp, class... _Args>
193 struct __nothrow_invokable_r_imp<true, true, _Ret, _Fp, _Args...> {
194 #ifdef _LIBCPP_CXX03_LANG
195 static const bool value = false;
196 #else
197 static const bool value = noexcept(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...));
198 #endif
199 };
200
201 template <class _Ret, class _Fp, class... _Args>
202 using __nothrow_invokable_r =
203 __nothrow_invokable_r_imp<__invokable_r<_Ret, _Fp, _Args...>::value, is_void<_Ret>::value, _Ret, _Fp, _Args...>;
204
205 template <class _Fp, class... _Args>
206 using __nothrow_invokable = __nothrow_invokable_r_imp<__invokable<_Fp, _Args...>::value, true, void, _Fp, _Args...>;
207
208 template <class _Fp, class... _Args>
209 struct __invoke_of
210 : public enable_if<__invokable<_Fp, _Args...>::value, typename __invokable_r<void, _Fp, _Args...>::_Result> {};
211
212 template <class _Ret, bool = is_void<_Ret>::value>
213 struct __invoke_void_return_wrapper {
214 template <class... _Args>
215 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static _Ret __call(_Args&&... __args) {
216 return std::__invoke(std::forward<_Args>(__args)...);
217 }
218 };
219
220 template <class _Ret>
221 struct __invoke_void_return_wrapper<_Ret, true> {
222 template <class... _Args>
223 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void __call(_Args&&... __args) {
224 std::__invoke(std::forward<_Args>(__args)...);
225 }
226 };
227
228 #if _LIBCPP_STD_VER >= 17
229
230 // is_invocable
231
232 template <class _Fn, class... _Args>
233 struct _LIBCPP_TEMPLATE_VIS is_invocable : integral_constant<bool, __invokable<_Fn, _Args...>::value> {};
234
235 template <class _Ret, class _Fn, class... _Args>
236 struct _LIBCPP_TEMPLATE_VIS is_invocable_r : integral_constant<bool, __invokable_r<_Ret, _Fn, _Args...>::value> {};
237
238 template <class _Fn, class... _Args>
239 inline constexpr bool is_invocable_v = is_invocable<_Fn, _Args...>::value;
240
241 template <class _Ret, class _Fn, class... _Args>
242 inline constexpr bool is_invocable_r_v = is_invocable_r<_Ret, _Fn, _Args...>::value;
243
244 // is_nothrow_invocable
245
246 template <class _Fn, class... _Args>
247 struct _LIBCPP_TEMPLATE_VIS is_nothrow_invocable : integral_constant<bool, __nothrow_invokable<_Fn, _Args...>::value> {
248 };
249
250 template <class _Ret, class _Fn, class... _Args>
251 struct _LIBCPP_TEMPLATE_VIS is_nothrow_invocable_r
252 : integral_constant<bool, __nothrow_invokable_r<_Ret, _Fn, _Args...>::value> {};
253
254 template <class _Fn, class... _Args>
255 inline constexpr bool is_nothrow_invocable_v = is_nothrow_invocable<_Fn, _Args...>::value;
256
257 template <class _Ret, class _Fn, class... _Args>
258 inline constexpr bool is_nothrow_invocable_r_v = is_nothrow_invocable_r<_Ret, _Fn, _Args...>::value;
259
260 template <class _Fn, class... _Args>
261 struct _LIBCPP_TEMPLATE_VIS invoke_result : __invoke_of<_Fn, _Args...> {};
262
263 template <class _Fn, class... _Args>
264 using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
265
266 #endif // _LIBCPP_STD_VER >= 17
267
268 _LIBCPP_END_NAMESPACE_STD
269
270 #endif // _LIBCPP___TYPE_TRAITS_INVOKE_H