6dce01c0687a2a4a51044a2c7031521a613ebf7b
[folly.git] / folly / functional / Invoke.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <functional>
20 #include <type_traits>
21
22 #include <folly/Traits.h>
23
24 /**
25  *  include or backport:
26  *  * std::invoke
27  *  * std::invoke_result
28  *  * std::invoke_result_t
29  *  * std::is_invocable
30  *  * std::is_invocable_r
31  *  * std::is_nothrow_invocable
32  *  * std::is_nothrow_invocable_r
33  */
34
35 #if __cpp_lib_invoke >= 201411 || _MSC_VER
36
37 namespace folly {
38
39 /* using override */ using std::invoke;
40
41 }
42
43 #else
44
45 namespace folly {
46
47 //  mimic: std::invoke, C++17
48 template <typename F, typename... Args>
49 constexpr auto invoke(F&& f, Args&&... args) noexcept(
50     noexcept(std::forward<F>(f)(std::forward<Args>(args)...)))
51     -> decltype(std::forward<F>(f)(std::forward<Args>(args)...)) {
52   return std::forward<F>(f)(std::forward<Args>(args)...);
53 }
54 template <typename M, typename C, typename... Args>
55 constexpr auto invoke(M(C::*d), Args&&... args)
56     -> decltype(std::mem_fn(d)(std::forward<Args>(args)...)) {
57   return std::mem_fn(d)(std::forward<Args>(args)...);
58 }
59
60 } // namespace folly
61
62 #endif
63
64 // Only available in >= MSVC 2017 15.3
65 #if __cpp_lib_is_invocable >= 201703 || _MSC_VER >= 1911
66
67 namespace folly {
68
69 /* using override */ using std::invoke_result;
70 /* using override */ using std::invoke_result_t;
71 /* using override */ using std::is_invocable;
72 /* using override */ using std::is_invocable_r;
73 /* using override */ using std::is_nothrow_invocable;
74 /* using override */ using std::is_nothrow_invocable_r;
75
76 }
77
78 #else
79
80 namespace folly {
81
82 namespace detail {
83
84 template <typename F, typename... Args>
85 using invoke_result_ =
86     decltype(invoke(std::declval<F>(), std::declval<Args>()...));
87
88 template <typename F, typename... Args>
89 struct invoke_nothrow_
90     : std::integral_constant<
91           bool,
92           noexcept(invoke(std::declval<F>(), std::declval<Args>()...))> {};
93
94 //  from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
95
96 template <typename Void, typename F, typename... Args>
97 struct invoke_result {};
98
99 template <typename F, typename... Args>
100 struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> {
101   using type = invoke_result_<F, Args...>;
102 };
103
104 template <typename Void, typename F, typename... Args>
105 struct is_invocable : std::false_type {};
106
107 template <typename F, typename... Args>
108 struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
109     : std::true_type {};
110
111 template <typename Void, typename R, typename F, typename... Args>
112 struct is_invocable_r : std::false_type {};
113
114 template <typename R, typename F, typename... Args>
115 struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
116     : std::is_convertible<invoke_result_<F, Args...>, R> {};
117
118 template <typename Void, typename F, typename... Args>
119 struct is_nothrow_invocable : std::false_type {};
120
121 template <typename F, typename... Args>
122 struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
123     : invoke_nothrow_<F, Args...> {};
124
125 template <typename Void, typename R, typename F, typename... Args>
126 struct is_nothrow_invocable_r : std::false_type {};
127
128 template <typename R, typename F, typename... Args>
129 struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
130     : StrictConjunction<
131         std::is_convertible<invoke_result_<F, Args...>, R>,
132         invoke_nothrow_<F, Args...>> {};
133
134 } // namespace detail
135
136 //  mimic: std::invoke_result, C++17
137 template <typename F, typename... Args>
138 struct invoke_result : detail::invoke_result<void, F, Args...> {};
139
140 //  mimic: std::invoke_result_t, C++17
141 template <typename F, typename... Args>
142 using invoke_result_t = typename invoke_result<F, Args...>::type;
143
144 //  mimic: std::is_invocable, C++17
145 template <typename F, typename... Args>
146 struct is_invocable : detail::is_invocable<void, F, Args...> {};
147
148 //  mimic: std::is_invocable_r, C++17
149 template <typename R, typename F, typename... Args>
150 struct is_invocable_r : detail::is_invocable_r<void, R, F, Args...> {};
151
152 //  mimic: std::is_nothrow_invocable, C++17
153 template <typename F, typename... Args>
154 struct is_nothrow_invocable : detail::is_nothrow_invocable<void, F, Args...> {};
155
156 //  mimic: std::is_nothrow_invocable_r, C++17
157 template <typename R, typename F, typename... Args>
158 struct is_nothrow_invocable_r
159     : detail::is_nothrow_invocable_r<void, R, F, Args...> {};
160
161 } // namespace folly
162
163 #endif
164
165 /***
166  *  FOLLY_CREATE_MEMBER_INVOKE_TRAITS
167  *
168  *  Used to create traits container, bound to a specific member-invocable name,
169  *  with the following member traits types and aliases:
170  *
171  *  * invoke_result
172  *  * invoke_result_t
173  *  * is_invocable
174  *  * is_invocable_r
175  *  * is_nothrow_invocable
176  *  * is_nothrow_invocable_r
177  *
178  *  The container also has a static member function:
179  *
180  *  * invoke
181  *
182  *  These members have behavior matching the behavior of C++17's corresponding
183  *  invocation traits types, aliases, and functions, but substituting canonical
184  *  invocation with member invocation.
185  *
186  *  Example:
187  *
188  *    FOLLY_CREATE_MEMBER_INVOKE_TRAITS(foo_invoke_traits, foo);
189  *
190  *  The traits container type `foo_invoke_traits` is generated in the current
191  *  namespace and has the listed member types and aliases. They may be used as
192  *  follows:
193  *
194  *    struct CanFoo {
195  *      int foo(Bar const&) { return 1; }
196  *      int foo(Car&&) noexcept { return 2; }
197  *    };
198  *
199  *    using traits = foo_invoke_traits;
200  *
201  *    traits::invoke(CanFoo{}, Bar{}) // 1
202  *
203  *    traits::invoke_result<CanFoo, Bar&&> // has member
204  *    traits::invoke_result_t<CanFoo, Bar&&> // int
205  *    traits::invoke_result<CanFoo, Bar&> // empty
206  *    traits::invoke_result_t<CanFoo, Bar&> // error
207  *
208  *    traits::is_invocable<CanFoo, Bar&&>::value // true
209  *    traits::is_invocable<CanFoo, Bar&>::value // false
210  *
211  *    traits::is_invocable_r<int, CanFoo, Bar&&>::value // true
212  *    traits::is_invocable_r<char*, CanFoo, Bar&&>::value // false
213  *
214  *    traits::is_nothrow_invocable<CanFoo, Bar&&>::value // false
215  *    traits::is_nothrow_invocable<CanFoo, Car&&>::value // true
216  *
217  *    traits::is_nothrow_invocable<int, CanFoo, Bar&&>::value // false
218  *    traits::is_nothrow_invocable<char*, CanFoo, Bar&&>::value // false
219  *    traits::is_nothrow_invocable<int, CanFoo, Car&&>::value // true
220  *    traits::is_nothrow_invocable<char*, CanFoo, Car&&>::value // false
221  */
222 #define FOLLY_CREATE_MEMBER_INVOKE_TRAITS(classname, membername)              \
223   struct classname {                                                          \
224    private:                                                                   \
225     template <typename T>                                                     \
226     using v_ = ::folly::void_t<T>;                                            \
227     template <typename F, typename... Args>                                   \
228     using result_ =                                                           \
229         decltype(::std::declval<F>().membername(::std::declval<Args>()...));  \
230     template <typename F, typename... Args>                                   \
231     struct nothrow_ : std::integral_constant<                                 \
232                           bool,                                               \
233                           noexcept(::std::declval<F>().membername(            \
234                               ::std::declval<Args>()...))> {};                \
235                                                                               \
236     template <typename, typename F, typename... Args>                         \
237     struct invoke_result_ {};                                                 \
238     template <typename F, typename... Args>                                   \
239     struct invoke_result_<v_<result_<F, Args...>>, F, Args...> {              \
240       using type = result_<F, Args...>;                                       \
241     };                                                                        \
242                                                                               \
243     template <typename, typename F, typename... Args>                         \
244     struct is_invocable_ : ::std::false_type {};                              \
245     template <typename F, typename... Args>                                   \
246     struct is_invocable_<v_<result_<F, Args...>>, F, Args...>                 \
247         : ::std::true_type {};                                                \
248                                                                               \
249     template <typename, typename R, typename F, typename... Args>             \
250     struct is_invocable_r_ : ::std::false_type {};                            \
251     template <typename R, typename F, typename... Args>                       \
252     struct is_invocable_r_<v_<result_<F, Args...>>, R, F, Args...>            \
253         : ::std::is_convertible<result_<F, Args...>, R> {};                   \
254                                                                               \
255     template <typename, typename F, typename... Args>                         \
256     struct is_nothrow_invocable_ : ::std::false_type {};                      \
257     template <typename F, typename... Args>                                   \
258     struct is_nothrow_invocable_<v_<result_<F, Args...>>, F, Args...>         \
259         : nothrow_<F, Args...> {};                                            \
260                                                                               \
261     template <typename, typename R, typename F, typename... Args>             \
262     struct is_nothrow_invocable_r_ : ::std::false_type {};                    \
263     template <typename R, typename F, typename... Args>                       \
264     struct is_nothrow_invocable_r_<v_<result_<F, Args...>>, R, F, Args...>    \
265         : ::folly::StrictConjunction<                                         \
266               ::std::is_convertible<result_<F, Args...>, R>,                  \
267               nothrow_<F, Args...>> {};                                       \
268                                                                               \
269    public:                                                                    \
270     template <typename F, typename... Args>                                   \
271     struct invoke_result : invoke_result_<void, F, Args...> {};               \
272     template <typename F, typename... Args>                                   \
273     using invoke_result_t = typename invoke_result<F, Args...>::type;         \
274     template <typename F, typename... Args>                                   \
275     struct is_invocable : is_invocable_<void, F, Args...> {};                 \
276     template <typename R, typename F, typename... Args>                       \
277     struct is_invocable_r : is_invocable_r_<void, R, F, Args...> {};          \
278     template <typename F, typename... Args>                                   \
279     struct is_nothrow_invocable : is_nothrow_invocable_<void, F, Args...> {}; \
280     template <typename R, typename F, typename... Args>                       \
281     struct is_nothrow_invocable_r                                             \
282         : is_nothrow_invocable_r_<void, R, F, Args...> {};                    \
283                                                                               \
284     template <typename F, typename... Args>                                   \
285     static constexpr result_<F, Args...> invoke(                              \
286         F&& f,                                                                \
287         Args&&... args) noexcept(nothrow_<F, Args...>::value) {               \
288       return std::forward<F>(f).membername(std::forward<Args>(args)...);      \
289     }                                                                         \
290   }