Lift the invoke helper in Function.h
[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 #if __cpp_lib_is_invocable >= 201703 || _MSC_VER
65
66 namespace folly {
67
68 /* using override */ using std::invoke_result;
69 /* using override */ using std::invoke_result_t;
70 /* using override */ using std::is_invocable;
71 /* using override */ using std::is_invocable_r;
72 /* using override */ using std::is_nothrow_invocable;
73 /* using override */ using std::is_nothrow_invocable_r;
74
75 }
76
77 #else
78
79 namespace folly {
80
81 namespace detail {
82
83 template <typename F, typename... Args>
84 using invoke_result_ =
85     decltype(invoke(std::declval<F>(), std::declval<Args>()...));
86
87 template <typename F, typename... Args>
88 using invoke_nothrow_ = std::integral_constant<
89     bool,
90     noexcept(invoke(std::declval<F>(), std::declval<Args>()...))>;
91
92 //  from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
93
94 template <typename Void, typename F, typename... Args>
95 struct invoke_result {};
96
97 template <typename F, typename... Args>
98 struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> {
99   using type = invoke_result_<F, Args...>;
100 };
101
102 template <typename Void, typename F, typename... Args>
103 struct is_invocable : std::false_type {};
104
105 template <typename F, typename... Args>
106 struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
107     : std::true_type {};
108
109 template <typename Void, typename R, typename F, typename... Args>
110 struct is_invocable_r : std::false_type {};
111
112 template <typename R, typename F, typename... Args>
113 struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
114     : std::is_convertible<invoke_result_<F, Args...>, R> {};
115
116 template <typename Void, typename F, typename... Args>
117 struct is_nothrow_invocable : std::false_type {};
118
119 template <typename F, typename... Args>
120 struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...>
121     : invoke_nothrow_<F, Args...> {};
122
123 template <typename Void, typename R, typename F, typename... Args>
124 struct is_nothrow_invocable_r : std::false_type {};
125
126 template <typename R, typename F, typename... Args>
127 struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...>
128     : StrictConjunction<
129         std::is_convertible<invoke_result_<F, Args...>, R>,
130         invoke_nothrow_<F, Args...>> {};
131
132 } // namespace detail
133
134 //  mimic: std::invoke_result, C++17
135 template <typename F, typename... Args>
136 struct invoke_result : detail::invoke_result<void, F, Args...> {};
137
138 //  mimic: std::invoke_result_t, C++17
139 template <typename F, typename... Args>
140 using invoke_result_t = typename invoke_result<F, Args...>::type;
141
142 //  mimic: std::is_invocable, C++17
143 template <typename F, typename... Args>
144 struct is_invocable : detail::is_invocable<void, F, Args...> {};
145
146 //  mimic: std::is_invocable_r, C++17
147 template <typename R, typename F, typename... Args>
148 struct is_invocable_r : detail::is_invocable_r<void, R, F, Args...> {};
149
150 //  mimic: std::is_nothrow_invocable, C++17
151 template <typename F, typename... Args>
152 struct is_nothrow_invocable : detail::is_nothrow_invocable<void, F, Args...> {};
153
154 //  mimic: std::is_nothrow_invocable_r, C++17
155 template <typename R, typename F, typename... Args>
156 struct is_nothrow_invocable_r
157     : detail::is_nothrow_invocable_r<void, R, F, Args...> {};
158
159 } // namespace folly
160
161 #endif