2eab3ff6ba76c81e2103ed2cfe03980bce7c6c89
[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 using invoke_nothrow_ = std::integral_constant<
90     bool,
91     noexcept(invoke(std::declval<F>(), std::declval<Args>()...))>;
92
93 //  from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
94
95 template <typename Void, typename F, typename... Args>
96 struct invoke_result {};
97
98 template <typename F, typename... Args>
99 struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> {
100   using type = invoke_result_<F, Args...>;
101 };
102
103 template <typename Void, typename F, typename... Args>
104 struct is_invocable : std::false_type {};
105
106 template <typename F, typename... Args>
107 struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
108     : std::true_type {};
109
110 template <typename Void, typename R, typename F, typename... Args>
111 struct is_invocable_r : std::false_type {};
112
113 template <typename R, typename F, typename... Args>
114 struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
115     : std::is_convertible<invoke_result_<F, Args...>, R> {};
116
117 template <typename Void, typename F, typename... Args>
118 struct is_nothrow_invocable : std::false_type {};
119
120 template <typename F, typename... Args>
121 struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
122     : invoke_nothrow_<F, Args...> {};
123
124 template <typename Void, typename R, typename F, typename... Args>
125 struct is_nothrow_invocable_r : std::false_type {};
126
127 template <typename R, typename F, typename... Args>
128 struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
129     : StrictConjunction<
130         std::is_convertible<invoke_result_<F, Args...>, R>,
131         invoke_nothrow_<F, Args...>> {};
132
133 } // namespace detail
134
135 //  mimic: std::invoke_result, C++17
136 template <typename F, typename... Args>
137 struct invoke_result : detail::invoke_result<void, F, Args...> {};
138
139 //  mimic: std::invoke_result_t, C++17
140 template <typename F, typename... Args>
141 using invoke_result_t = typename invoke_result<F, Args...>::type;
142
143 //  mimic: std::is_invocable, C++17
144 template <typename F, typename... Args>
145 struct is_invocable : detail::is_invocable<void, F, Args...> {};
146
147 //  mimic: std::is_invocable_r, C++17
148 template <typename R, typename F, typename... Args>
149 struct is_invocable_r : detail::is_invocable_r<void, R, F, Args...> {};
150
151 //  mimic: std::is_nothrow_invocable, C++17
152 template <typename F, typename... Args>
153 struct is_nothrow_invocable : detail::is_nothrow_invocable<void, F, Args...> {};
154
155 //  mimic: std::is_nothrow_invocable_r, C++17
156 template <typename R, typename F, typename... Args>
157 struct is_nothrow_invocable_r
158     : detail::is_nothrow_invocable_r<void, R, F, Args...> {};
159
160 } // namespace folly
161
162 #endif
163
164 /***
165  *  FOLLY_CREATE_MEMBER_INVOKE_TRAITS
166  *
167  *  Used to create traits container, bound to a specific member-invocable name,
168  *  with the following member traits types and aliases:
169  *
170  *  * invoke_result
171  *  * invoke_result_t
172  *  * is_invocable
173  *  * is_invocable_r
174  *  * is_nothrow_invocable
175  *  * is_nothrow_invocable_r
176  *
177  *  The container also has a static member function:
178  *
179  *  * invoke
180  *
181  *  These members have behavior matching the behavior of C++17's corresponding
182  *  invocation traits types, aliases, and functions, but substituting canonical
183  *  invocation with member invocation.
184  *
185  *  Example:
186  *
187  *    FOLLY_CREATE_MEMBER_INVOKE_TRAITS(foo_invoke_traits, foo);
188  *
189  *  The traits container type `foo_invoke_traits` is generated in the current
190  *  namespace and has the listed member types and aliases. They may be used as
191  *  follows:
192  *
193  *    struct CanFoo {
194  *      int foo(Bar const&) { return 1; }
195  *      int foo(Car&&) noexcept { return 2; }
196  *    };
197  *
198  *    using traits = foo_invoke_traits;
199  *
200  *    traits::invoke(CanFoo{}, Bar{}) // 1
201  *
202  *    traits::invoke_result<CanFoo, Bar&&> // has member
203  *    traits::invoke_result_t<CanFoo, Bar&&> // int
204  *    traits::invoke_result<CanFoo, Bar&> // empty
205  *    traits::invoke_result_t<CanFoo, Bar&> // error
206  *
207  *    traits::is_invocable<CanFoo, Bar&&>::value // true
208  *    traits::is_invocable<CanFoo, Bar&>::value // false
209  *
210  *    traits::is_invocable_r<int, CanFoo, Bar&&>::value // true
211  *    traits::is_invocable_r<char*, CanFoo, Bar&&>::value // false
212  *
213  *    traits::is_nothrow_invocable<CanFoo, Bar&&>::value // false
214  *    traits::is_nothrow_invocable<CanFoo, Car&&>::value // true
215  *
216  *    traits::is_nothrow_invocable<int, CanFoo, Bar&&>::value // false
217  *    traits::is_nothrow_invocable<char*, CanFoo, Bar&&>::value // false
218  *    traits::is_nothrow_invocable<int, CanFoo, Car&&>::value // true
219  *    traits::is_nothrow_invocable<char*, CanFoo, Car&&>::value // false
220  */
221 #define FOLLY_CREATE_MEMBER_INVOKE_TRAITS(classname, membername)              \
222   struct classname {                                                          \
223    private:                                                                   \
224     template <typename T>                                                     \
225     using v_ = ::folly::void_t<T>;                                            \
226     template <typename F, typename... Args>                                   \
227     using result_ =                                                           \
228         decltype(::std::declval<F>().membername(::std::declval<Args>()...));  \
229     template <typename F, typename... Args>                                   \
230     using nothrow_ = ::std::integral_constant<                                \
231         bool,                                                                 \
232         noexcept(::std::declval<F>().membername(::std::declval<Args>()...))>; \
233                                                                               \
234     template <typename, typename F, typename... Args>                         \
235     struct invoke_result_ {};                                                 \
236     template <typename F, typename... Args>                                   \
237     struct invoke_result_<v_<result_<F, Args...>>, F, Args...> {              \
238       using type = result_<F, Args...>;                                       \
239     };                                                                        \
240                                                                               \
241     template <typename, typename F, typename... Args>                         \
242     struct is_invocable_ : ::std::false_type {};                              \
243     template <typename F, typename... Args>                                   \
244     struct is_invocable_<v_<result_<F, Args...>>, F, Args...>                 \
245         : ::std::true_type {};                                                \
246                                                                               \
247     template <typename, typename R, typename F, typename... Args>             \
248     struct is_invocable_r_ : ::std::false_type {};                            \
249     template <typename R, typename F, typename... Args>                       \
250     struct is_invocable_r_<v_<result_<F, Args...>>, R, F, Args...>            \
251         : ::std::is_convertible<result_<F, Args...>, R> {};                   \
252                                                                               \
253     template <typename, typename F, typename... Args>                         \
254     struct is_nothrow_invocable_ : ::std::false_type {};                      \
255     template <typename F, typename... Args>                                   \
256     struct is_nothrow_invocable_<v_<result_<F, Args...>>, F, Args...>         \
257         : nothrow_<F, Args...> {};                                            \
258                                                                               \
259     template <typename, typename R, typename F, typename... Args>             \
260     struct is_nothrow_invocable_r_ : ::std::false_type {};                    \
261     template <typename R, typename F, typename... Args>                       \
262     struct is_nothrow_invocable_r_<v_<result_<F, Args...>>, R, F, Args...>    \
263         : ::folly::StrictConjunction<                                         \
264               ::std::is_convertible<result_<F, Args...>, R>,                  \
265               nothrow_<F, Args...>> {};                                       \
266                                                                               \
267    public:                                                                    \
268     template <typename F, typename... Args>                                   \
269     struct invoke_result : invoke_result_<void, F, Args...> {};               \
270     template <typename F, typename... Args>                                   \
271     using invoke_result_t = typename invoke_result<F, Args...>::type;         \
272     template <typename F, typename... Args>                                   \
273     struct is_invocable : is_invocable_<void, F, Args...> {};                 \
274     template <typename R, typename F, typename... Args>                       \
275     struct is_invocable_r : is_invocable_r_<void, R, F, Args...> {};          \
276     template <typename F, typename... Args>                                   \
277     struct is_nothrow_invocable : is_nothrow_invocable_<void, F, Args...> {}; \
278     template <typename R, typename F, typename... Args>                       \
279     struct is_nothrow_invocable_r                                             \
280         : is_nothrow_invocable_r_<void, R, F, Args...> {};                    \
281                                                                               \
282     template <typename F, typename... Args>                                   \
283     static constexpr result_<F, Args...> invoke(                              \
284         F&& f,                                                                \
285         Args&&... args) noexcept(nothrow_<F, Args...>::value) {               \
286       return std::forward<F>(f).membername(std::forward<Args>(args)...);      \
287     }                                                                         \
288   }