// Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // absl::base_internal::Invoke(f, args...) is an implementation of // INVOKE(f, args...) from section [func.require] of the C++ standard. // // [func.require] // Define INVOKE (f, t1, t2, ..., tN) as follows: // 1. (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T // and is_base_of_v> is true; // 2. (t1.get().*f)(t2, ..., tN) when f is a pointer to a member function of a // class T and remove_cvref_t is a specialization of // reference_wrapper; // 3. ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a // class T and t1 does not satisfy the previous two items; // 4. t1.*f when N == 1 and f is a pointer to data member of a class T and // is_base_of_v> is true; // 5. t1.get().*f when N == 1 and f is a pointer to data member of a class T and // remove_cvref_t is a specialization of reference_wrapper; // 6. (*t1).*f when N == 1 and f is a pointer to data member of a class T and t1 // does not satisfy the previous two items; // 7. f(t1, t2, ..., tN) in all other cases. // // The implementation is SFINAE-friendly: substitution failure within Invoke() // isn't an error. #ifndef ABSL_BASE_INTERNAL_INVOKE_H_ #define ABSL_BASE_INTERNAL_INVOKE_H_ #include #include #include #include "absl/meta/type_traits.h" // The following code is internal implementation detail. See the comment at the // top of this file for the API documentation. namespace absl { ABSL_NAMESPACE_BEGIN namespace base_internal { template struct IsReferenceWrapper : std::false_type {}; template struct IsReferenceWrapper> : std::true_type {}; template using RemoveCvrefT = typename std::remove_cv::type>::type; // The seven classes below each implement one of the clauses from the definition // of INVOKE. The inner class template Accept checks whether the // clause is applicable; static function template Invoke(f, args...) does the // invocation. // // By separating the clause selection logic from invocation we make sure that // Invoke() does exactly what the standard says. template struct StrippedAccept { template struct Accept : Derived::template AcceptImpl::type>::type...> {}; }; // (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T // and t1 is an object of type T or a reference to an object of type T or a // reference to an object of a type derived from T. struct MemFunAndRef : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, std::is_base_of< C, typename std::remove_reference::type>::value && absl::is_function::value> {}; template static decltype((std::declval().* std::declval())(std::declval()...)) Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) { return (std::forward(obj).* std::forward(mem_fun))(std::forward(args)...); } }; // (t1.get().*f)(t2, ..., tN) when f is a pointer to a member function of a // class T and remove_cvref_t is a specialization of // reference_wrapper; struct MemFunAndRefWrap : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, IsReferenceWrapper>::value && absl::is_function::value> {}; template static decltype((std::declval().get().* std::declval())(std::declval()...)) Invoke(MemFun&& mem_fun, RefWrap&& ref_wrap, Args&&... args) { return (std::forward(ref_wrap).get().* std::forward(mem_fun))(std::forward(args)...); } }; // ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a // class T and t1 does not satisfy the previous two items; struct MemFunAndPtr : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, !std::is_base_of< C, typename std::remove_reference::type>::value && !IsReferenceWrapper>::value && absl::is_function::value> {}; template static decltype(((*std::declval()).* std::declval())(std::declval()...)) Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) { return ((*std::forward(ptr)).* std::forward(mem_fun))(std::forward(args)...); } }; // t1.*f when N == 1 and f is a pointer to data member of a class T and // is_base_of_v> is true; struct DataMemAndRef : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, std::is_base_of< C, typename std::remove_reference::type>::value && !absl::is_function::value> {}; template static decltype(std::declval().*std::declval()) Invoke( DataMem&& data_mem, Ref&& ref) { return std::forward(ref).*std::forward(data_mem); } }; // t1.get().*f when N == 1 and f is a pointer to data member of a class T and // remove_cvref_t is a specialization of reference_wrapper; struct DataMemAndRefWrap : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, IsReferenceWrapper>::value && !absl::is_function::value> {}; template static decltype(std::declval().get().*std::declval()) Invoke(DataMem&& data_mem, RefWrap&& ref_wrap) { return std::forward(ref_wrap).get().* std::forward(data_mem); } }; // (*t1).*f when N == 1 and f is a pointer to data member of a class T and t1 // does not satisfy the previous two items; struct DataMemAndPtr : StrippedAccept { template struct AcceptImpl : std::false_type {}; template struct AcceptImpl : std::integral_constant< bool, !std::is_base_of< C, typename std::remove_reference::type>::value && !IsReferenceWrapper>::value && !absl::is_function::value> {}; template static decltype((*std::declval()).*std::declval()) Invoke( DataMem&& data_mem, Ptr&& ptr) { return (*std::forward(ptr)).*std::forward(data_mem); } }; // f(t1, t2, ..., tN) in all other cases. struct Callable { // Callable doesn't have Accept because it's the last clause that gets picked // when none of the previous clauses are applicable. template static decltype(std::declval()(std::declval()...)) Invoke( F&& f, Args&&... args) { return std::forward(f)(std::forward(args)...); } }; // Resolves to the first matching clause. template struct Invoker { typedef typename std::conditional< MemFunAndRef::Accept::value, MemFunAndRef, typename std::conditional< MemFunAndRefWrap::Accept::value, MemFunAndRefWrap, typename std::conditional< MemFunAndPtr::Accept::value, MemFunAndPtr, typename std::conditional< DataMemAndRef::Accept::value, DataMemAndRef, typename std::conditional< DataMemAndRefWrap::Accept::value, DataMemAndRefWrap, typename std::conditional< DataMemAndPtr::Accept::value, DataMemAndPtr, Callable>::type>::type>::type>::type>::type>::type type; }; // The result type of Invoke. template using InvokeT = decltype(Invoker::type::Invoke( std::declval(), std::declval()...)); // Invoke(f, args...) is an implementation of INVOKE(f, args...) from section // [func.require] of the C++ standard. template InvokeT Invoke(F&& f, Args&&... args) { return Invoker::type::Invoke(std::forward(f), std::forward(args)...); } } // namespace base_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_BASE_INTERNAL_INVOKE_H_