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