1 // Copyright 2017 The Abseil Authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // absl::base_internal::Invoke(f, args...) is an implementation of
16 // INVOKE(f, args...) from section [func.require] of the C++ standard.
19 // Define INVOKE (f, t1, t2, ..., tN) as follows:
20 // 1. (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
21 // and t1 is an object of type T or a reference to an object of type T or a
22 // reference to an object of a type derived from T;
23 // 2. ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
24 // class T and t1 is not one of the types described in the previous item;
25 // 3. t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
26 // an object of type T or a reference to an object of type T or a reference
27 // to an object of a type derived from T;
28 // 4. (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
29 // is not one of the types described in the previous item;
30 // 5. f(t1, t2, ..., tN) in all other cases.
32 // The implementation is SFINAE-friendly: substitution failure within Invoke()
35 #ifndef ABSL_BASE_INTERNAL_INVOKE_H_
36 #define ABSL_BASE_INTERNAL_INVOKE_H_
39 #include <type_traits>
42 // The following code is internal implementation detail. See the comment at the
43 // top of this file for the API documentation.
46 namespace base_internal {
48 // The five classes below each implement one of the clauses from the definition
49 // of INVOKE. The inner class template Accept<F, Args...> checks whether the
50 // clause is applicable; static function template Invoke(f, args...) does the
53 // By separating the clause selection logic from invocation we make sure that
54 // Invoke() does exactly what the standard says.
56 template <typename Derived>
57 struct StrippedAccept {
58 template <typename... Args>
59 struct Accept : Derived::template AcceptImpl<typename std::remove_cv<
60 typename std::remove_reference<Args>::type>::type...> {};
63 // (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
64 // and t1 is an object of type T or a reference to an object of type T or a
65 // reference to an object of a type derived from T.
66 struct MemFunAndRef : StrippedAccept<MemFunAndRef> {
67 template <typename... Args>
68 struct AcceptImpl : std::false_type {};
70 template <typename R, typename C, typename... Params, typename Obj,
72 struct AcceptImpl<R (C::*)(Params...), Obj, Args...>
73 : std::is_base_of<C, Obj> {};
75 template <typename R, typename C, typename... Params, typename Obj,
77 struct AcceptImpl<R (C::*)(Params...) const, Obj, Args...>
78 : std::is_base_of<C, Obj> {};
80 template <typename MemFun, typename Obj, typename... Args>
81 static decltype((std::declval<Obj>().*
82 std::declval<MemFun>())(std::declval<Args>()...))
83 Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) {
84 return (std::forward<Obj>(obj).*
85 std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
89 // ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
90 // class T and t1 is not one of the types described in the previous item.
91 struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> {
92 template <typename... Args>
93 struct AcceptImpl : std::false_type {};
95 template <typename R, typename C, typename... Params, typename Ptr,
97 struct AcceptImpl<R (C::*)(Params...), Ptr, Args...>
98 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
100 template <typename R, typename C, typename... Params, typename Ptr,
102 struct AcceptImpl<R (C::*)(Params...) const, Ptr, Args...>
103 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
105 template <typename MemFun, typename Ptr, typename... Args>
106 static decltype(((*std::declval<Ptr>()).*
107 std::declval<MemFun>())(std::declval<Args>()...))
108 Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) {
109 return ((*std::forward<Ptr>(ptr)).*
110 std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
114 // t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
115 // an object of type T or a reference to an object of type T or a reference
116 // to an object of a type derived from T.
117 struct DataMemAndRef : StrippedAccept<DataMemAndRef> {
118 template <typename... Args>
119 struct AcceptImpl : std::false_type {};
121 template <typename R, typename C, typename Obj>
122 struct AcceptImpl<R C::*, Obj> : std::is_base_of<C, Obj> {};
124 template <typename DataMem, typename Ref>
125 static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke(
126 DataMem&& data_mem, Ref&& ref) {
127 return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem);
131 // (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
132 // is not one of the types described in the previous item.
133 struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> {
134 template <typename... Args>
135 struct AcceptImpl : std::false_type {};
137 template <typename R, typename C, typename Ptr>
138 struct AcceptImpl<R C::*, Ptr>
139 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
141 template <typename DataMem, typename Ptr>
142 static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke(
143 DataMem&& data_mem, Ptr&& ptr) {
144 return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem);
148 // f(t1, t2, ..., tN) in all other cases.
150 // Callable doesn't have Accept because it's the last clause that gets picked
151 // when none of the previous clauses are applicable.
152 template <typename F, typename... Args>
153 static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke(
154 F&& f, Args&&... args) {
155 return std::forward<F>(f)(std::forward<Args>(args)...);
159 // Resolves to the first matching clause.
160 template <typename... Args>
162 typedef typename std::conditional<
163 MemFunAndRef::Accept<Args...>::value, MemFunAndRef,
164 typename std::conditional<
165 MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr,
166 typename std::conditional<
167 DataMemAndRef::Accept<Args...>::value, DataMemAndRef,
168 typename std::conditional<DataMemAndPtr::Accept<Args...>::value,
169 DataMemAndPtr, Callable>::type>::type>::
173 // The result type of Invoke<F, Args...>.
174 template <typename F, typename... Args>
175 using InvokeT = decltype(Invoker<F, Args...>::type::Invoke(
176 std::declval<F>(), std::declval<Args>()...));
178 // Invoke(f, args...) is an implementation of INVOKE(f, args...) from section
179 // [func.require] of the C++ standard.
180 template <typename F, typename... Args>
181 InvokeT<F, Args...> Invoke(F&& f, Args&&... args) {
182 return Invoker<F, Args...>::type::Invoke(std::forward<F>(f),
183 std::forward<Args>(args)...);
185 } // namespace base_internal
188 #endif // ABSL_BASE_INTERNAL_INVOKE_H_