Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 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 |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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. |
| 14 | // |
| 15 | // absl::base_internal::Invoke(f, args...) is an implementation of |
| 16 | // INVOKE(f, args...) from section [func.require] of the C++ standard. |
| 17 | // |
| 18 | // [func.require] |
| 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. |
| 31 | // |
| 32 | // The implementation is SFINAE-friendly: substitution failure within Invoke() |
| 33 | // isn't an error. |
| 34 | |
| 35 | #ifndef ABSL_BASE_INTERNAL_INVOKE_H_ |
| 36 | #define ABSL_BASE_INTERNAL_INVOKE_H_ |
| 37 | |
| 38 | #include <algorithm> |
| 39 | #include <type_traits> |
| 40 | #include <utility> |
| 41 | |
| 42 | #include "absl/meta/type_traits.h" |
| 43 | |
| 44 | // The following code is internal implementation detail. See the comment at the |
| 45 | // top of this file for the API documentation. |
| 46 | |
| 47 | namespace absl { |
| 48 | namespace base_internal { |
| 49 | |
| 50 | // The five classes below each implement one of the clauses from the definition |
| 51 | // of INVOKE. The inner class template Accept<F, Args...> checks whether the |
| 52 | // clause is applicable; static function template Invoke(f, args...) does the |
| 53 | // invocation. |
| 54 | // |
| 55 | // By separating the clause selection logic from invocation we make sure that |
| 56 | // Invoke() does exactly what the standard says. |
| 57 | |
| 58 | template <typename Derived> |
| 59 | struct StrippedAccept { |
| 60 | template <typename... Args> |
| 61 | struct Accept : Derived::template AcceptImpl<typename std::remove_cv< |
| 62 | typename std::remove_reference<Args>::type>::type...> {}; |
| 63 | }; |
| 64 | |
| 65 | // (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T |
| 66 | // and t1 is an object of type T or a reference to an object of type T or a |
| 67 | // reference to an object of a type derived from T. |
| 68 | struct MemFunAndRef : StrippedAccept<MemFunAndRef> { |
| 69 | template <typename... Args> |
| 70 | struct AcceptImpl : std::false_type {}; |
| 71 | |
| 72 | template <typename MemFunType, typename C, typename Obj, typename... Args> |
| 73 | struct AcceptImpl<MemFunType C::*, Obj, Args...> |
| 74 | : std::integral_constant<bool, std::is_base_of<C, Obj>::value && |
| 75 | absl::is_function<MemFunType>::value> { |
| 76 | }; |
| 77 | |
| 78 | template <typename MemFun, typename Obj, typename... Args> |
| 79 | static decltype((std::declval<Obj>().* |
| 80 | std::declval<MemFun>())(std::declval<Args>()...)) |
| 81 | Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) { |
| 82 | return (std::forward<Obj>(obj).* |
| 83 | std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | // ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a |
| 88 | // class T and t1 is not one of the types described in the previous item. |
| 89 | struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> { |
| 90 | template <typename... Args> |
| 91 | struct AcceptImpl : std::false_type {}; |
| 92 | |
| 93 | template <typename MemFunType, typename C, typename Ptr, typename... Args> |
| 94 | struct AcceptImpl<MemFunType C::*, Ptr, Args...> |
| 95 | : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value && |
| 96 | absl::is_function<MemFunType>::value> { |
| 97 | }; |
| 98 | |
| 99 | template <typename MemFun, typename Ptr, typename... Args> |
| 100 | static decltype(((*std::declval<Ptr>()).* |
| 101 | std::declval<MemFun>())(std::declval<Args>()...)) |
| 102 | Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) { |
| 103 | return ((*std::forward<Ptr>(ptr)).* |
| 104 | std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is |
| 109 | // an object of type T or a reference to an object of type T or a reference |
| 110 | // to an object of a type derived from T. |
| 111 | struct DataMemAndRef : StrippedAccept<DataMemAndRef> { |
| 112 | template <typename... Args> |
| 113 | struct AcceptImpl : std::false_type {}; |
| 114 | |
| 115 | template <typename R, typename C, typename Obj> |
| 116 | struct AcceptImpl<R C::*, Obj> |
| 117 | : std::integral_constant<bool, std::is_base_of<C, Obj>::value && |
| 118 | !absl::is_function<R>::value> {}; |
| 119 | |
| 120 | template <typename DataMem, typename Ref> |
| 121 | static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke( |
| 122 | DataMem&& data_mem, Ref&& ref) { |
| 123 | return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 |
| 128 | // is not one of the types described in the previous item. |
| 129 | struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> { |
| 130 | template <typename... Args> |
| 131 | struct AcceptImpl : std::false_type {}; |
| 132 | |
| 133 | template <typename R, typename C, typename Ptr> |
| 134 | struct AcceptImpl<R C::*, Ptr> |
| 135 | : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value && |
| 136 | !absl::is_function<R>::value> {}; |
| 137 | |
| 138 | template <typename DataMem, typename Ptr> |
| 139 | static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke( |
| 140 | DataMem&& data_mem, Ptr&& ptr) { |
| 141 | return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | // f(t1, t2, ..., tN) in all other cases. |
| 146 | struct Callable { |
| 147 | // Callable doesn't have Accept because it's the last clause that gets picked |
| 148 | // when none of the previous clauses are applicable. |
| 149 | template <typename F, typename... Args> |
| 150 | static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke( |
| 151 | F&& f, Args&&... args) { |
| 152 | return std::forward<F>(f)(std::forward<Args>(args)...); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | // Resolves to the first matching clause. |
| 157 | template <typename... Args> |
| 158 | struct Invoker { |
| 159 | typedef typename std::conditional< |
| 160 | MemFunAndRef::Accept<Args...>::value, MemFunAndRef, |
| 161 | typename std::conditional< |
| 162 | MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr, |
| 163 | typename std::conditional< |
| 164 | DataMemAndRef::Accept<Args...>::value, DataMemAndRef, |
| 165 | typename std::conditional<DataMemAndPtr::Accept<Args...>::value, |
| 166 | DataMemAndPtr, Callable>::type>::type>:: |
| 167 | type>::type type; |
| 168 | }; |
| 169 | |
| 170 | // The result type of Invoke<F, Args...>. |
| 171 | template <typename F, typename... Args> |
| 172 | using InvokeT = decltype(Invoker<F, Args...>::type::Invoke( |
| 173 | std::declval<F>(), std::declval<Args>()...)); |
| 174 | |
| 175 | // Invoke(f, args...) is an implementation of INVOKE(f, args...) from section |
| 176 | // [func.require] of the C++ standard. |
| 177 | template <typename F, typename... Args> |
| 178 | InvokeT<F, Args...> Invoke(F&& f, Args&&... args) { |
| 179 | return Invoker<F, Args...>::type::Invoke(std::forward<F>(f), |
| 180 | std::forward<Args>(args)...); |
| 181 | } |
| 182 | } // namespace base_internal |
| 183 | } // namespace absl |
| 184 | |
| 185 | #endif // ABSL_BASE_INTERNAL_INVOKE_H_ |