ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_BIND_INTERNAL_H_ |
| 6 | #define BASE_BIND_INTERNAL_H_ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 7 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
tzik | f09a370 | 2016-06-03 16:25:20 +0900 | [diff] [blame] | 10 | #include <tuple> |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 11 | #include <type_traits> |
| 12 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 13 | #include "base/bind_helpers.h" |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 14 | #include "base/callback_internal.h" |
ajwong@chromium.org | 27e5685 | 2011-10-01 15:31:41 +0900 | [diff] [blame] | 15 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 17 | #include "base/template_util.h" |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 18 | #include "base/tuple.h" |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 19 | #include "build/build_config.h" |
| 20 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 21 | namespace base { |
| 22 | namespace internal { |
| 23 | |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 24 | // See base/callback.h for user documentation. |
| 25 | // |
| 26 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 27 | // CONCEPTS: |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 28 | // Functor -- A movable type representing something that should be called. |
| 29 | // All function pointers and Callback<> are functors even if the |
| 30 | // invocation syntax differs. |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 31 | // RunType -- A function type (as opposed to function _pointer_ type) for |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 32 | // a Callback<>::Run(). Usually just a convenience typedef. |
tzik | b063222 | 2015-12-15 15:41:49 +0900 | [diff] [blame] | 33 | // (Bound)Args -- A set of types that stores the arguments. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 34 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 35 | // Types: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 36 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 37 | // equivalent forms with a "void" return type. |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 38 | // FunctorTraits<> -- Type traits used to determine the correct RunType and |
| 39 | // invocation manner for a Functor. This is where function |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 40 | // signature adapters are applied. |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 41 | // InvokeHelper<> -- Take a Functor + arguments and actully invokes it. |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 42 | // Handle the differing syntaxes needed for WeakPtr<> |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 43 | // support. This is separate from Invoker to avoid creating |
| 44 | // multiple version of Invoker<>. |
| 45 | // Invoker<> -- Unwraps the curried parameters and executes the Functor. |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 46 | // BindState<> -- Stores the curried parameters, and is the main entry point |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 47 | // into the Bind() system. |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 48 | |
tzik | 8df08a5 | 2014-11-26 16:54:58 +0900 | [diff] [blame] | 49 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 50 | // pointer to a RefCounted type. |
| 51 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 52 | // Non-zero-arity cases should be handled by the specialization below. |
| 53 | template <typename... Args> |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 54 | struct HasRefCountedTypeAsRawPtr : std::false_type {}; |
tzik | 8df08a5 | 2014-11-26 16:54:58 +0900 | [diff] [blame] | 55 | |
| 56 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 57 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 58 | // parameters recursively. |
| 59 | template <typename T, typename... Args> |
| 60 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 61 | : std::conditional<NeedsScopedRefptrButGetsRawPtr<T>::value, |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 62 | std::true_type, |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 63 | HasRefCountedTypeAsRawPtr<Args...>>::type {}; |
tzik | 8df08a5 | 2014-11-26 16:54:58 +0900 | [diff] [blame] | 64 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 65 | // ForceVoidReturn<> |
| 66 | // |
| 67 | // Set of templates that support forcing the function return type to void. |
| 68 | template <typename Sig> |
| 69 | struct ForceVoidReturn; |
| 70 | |
tzik | 012481a | 2014-11-20 19:09:45 +0900 | [diff] [blame] | 71 | template <typename R, typename... Args> |
| 72 | struct ForceVoidReturn<R(Args...)> { |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 73 | using RunType = void(Args...); |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 76 | // FunctorTraits<> |
| 77 | // |
| 78 | // See description at top of file. |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 79 | template <typename Functor> |
| 80 | struct FunctorTraits; |
| 81 | |
| 82 | // For functions. |
| 83 | template <typename R, typename... Args> |
| 84 | struct FunctorTraits<R (*)(Args...)> { |
| 85 | using RunType = R(Args...); |
| 86 | static constexpr bool is_method = false; |
| 87 | |
| 88 | template <typename... RunArgs> |
| 89 | static R Invoke(R (*function)(Args...), RunArgs&&... args) { |
| 90 | return function(std::forward<RunArgs>(args)...); |
| 91 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 94 | #if defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 95 | |
| 96 | // For functions. |
| 97 | template <typename R, typename... Args> |
| 98 | struct FunctorTraits<R(__stdcall*)(Args...)> { |
| 99 | using RunType = R(Args...); |
| 100 | static constexpr bool is_method = false; |
| 101 | |
| 102 | template <typename... RunArgs> |
| 103 | static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) { |
| 104 | return function(std::forward<RunArgs>(args)...); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // For functions. |
| 109 | template <typename R, typename... Args> |
| 110 | struct FunctorTraits<R(__fastcall*)(Args...)> { |
| 111 | using RunType = R(Args...); |
| 112 | static constexpr bool is_method = false; |
| 113 | |
| 114 | template <typename... RunArgs> |
| 115 | static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) { |
| 116 | return function(std::forward<RunArgs>(args)...); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | #endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 121 | |
| 122 | // For methods. |
| 123 | template <typename R, typename Receiver, typename... Args> |
| 124 | struct FunctorTraits<R (Receiver::*)(Args...)> { |
| 125 | using RunType = R(Receiver*, Args...); |
| 126 | static constexpr bool is_method = true; |
| 127 | |
| 128 | template <typename ReceiverPtr, typename... RunArgs> |
| 129 | static R Invoke(R (Receiver::*method)(Args...), |
| 130 | ReceiverPtr&& receiver_ptr, |
| 131 | RunArgs&&... args) { |
| 132 | // Clang skips CV qualifier check on a method pointer invocation when the |
| 133 | // receiver is a subclass. Store the receiver into a const reference to |
| 134 | // T to ensure the CV check works. |
| 135 | // https://llvm.org/bugs/show_bug.cgi?id=27037 |
| 136 | Receiver& receiver = *receiver_ptr; |
| 137 | return (receiver.*method)(std::forward<RunArgs>(args)...); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | // For const methods. |
| 142 | template <typename R, typename Receiver, typename... Args> |
| 143 | struct FunctorTraits<R (Receiver::*)(Args...) const> { |
| 144 | using RunType = R(const Receiver*, Args...); |
| 145 | static constexpr bool is_method = true; |
| 146 | |
| 147 | template <typename ReceiverPtr, typename... RunArgs> |
| 148 | static R Invoke(R (Receiver::*method)(Args...) const, |
| 149 | ReceiverPtr&& receiver_ptr, |
| 150 | RunArgs&&... args) { |
| 151 | // Clang skips CV qualifier check on a method pointer invocation when the |
| 152 | // receiver is a subclass. Store the receiver into a const reference to |
| 153 | // T to ensure the CV check works. |
| 154 | // https://llvm.org/bugs/show_bug.cgi?id=27037 |
| 155 | const Receiver& receiver = *receiver_ptr; |
| 156 | return (receiver.*method)(std::forward<RunArgs>(args)...); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | // For IgnoreResults. |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 161 | template <typename T> |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 162 | struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 163 | using RunType = |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 164 | typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType; |
| 165 | |
| 166 | template <typename IgnoreResultType, typename... RunArgs> |
| 167 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 168 | RunArgs&&... args) { |
| 169 | FunctorTraits<T>::Invoke(ignore_result_helper.functor_, |
| 170 | std::forward<RunArgs>(args)...); |
| 171 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 172 | }; |
| 173 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 174 | // For Callbacks. |
| 175 | template <typename R, typename... Args, CopyMode copy_mode> |
| 176 | struct FunctorTraits<Callback<R(Args...), copy_mode>> { |
| 177 | using RunType = R(Args...); |
| 178 | static constexpr bool is_method = false; |
| 179 | |
| 180 | template <typename CallbackType, typename... RunArgs> |
| 181 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 182 | DCHECK(!callback.is_null()); |
| 183 | return std::forward<CallbackType>(callback).Run( |
| 184 | std::forward<RunArgs>(args)...); |
| 185 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 186 | }; |
| 187 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 188 | // InvokeHelper<> |
| 189 | // |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 190 | // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 191 | // |
| 192 | // The normal type just calls the underlying runnable. |
| 193 | // |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 194 | // WeakCalls need special syntax that is applied to the first argument to check |
| 195 | // if they should no-op themselves. |
tzik | e1ad6f6 | 2016-06-01 17:22:51 +0900 | [diff] [blame] | 196 | template <bool is_weak_call, typename ReturnType> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 197 | struct InvokeHelper; |
| 198 | |
tzik | e1ad6f6 | 2016-06-01 17:22:51 +0900 | [diff] [blame] | 199 | template <typename ReturnType> |
| 200 | struct InvokeHelper<false, ReturnType> { |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 201 | template <typename Functor, typename... RunArgs> |
| 202 | static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { |
| 203 | using Traits = FunctorTraits<typename std::decay<Functor>::type>; |
| 204 | return Traits::Invoke(std::forward<Functor>(functor), |
| 205 | std::forward<RunArgs>(args)...); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
tzik | e1ad6f6 | 2016-06-01 17:22:51 +0900 | [diff] [blame] | 209 | template <typename ReturnType> |
| 210 | struct InvokeHelper<true, ReturnType> { |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 211 | // WeakCalls are only supported for functions with a void return type. |
| 212 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 213 | // is invalidated. |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 214 | static_assert(std::is_void<ReturnType>::value, |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 215 | "weak_ptrs can only bind to methods without return values"); |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 216 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 217 | template <typename Functor, typename BoundWeakPtr, typename... RunArgs> |
| 218 | static inline void MakeItSo(Functor&& functor, |
| 219 | BoundWeakPtr weak_ptr, |
| 220 | RunArgs&&... args) { |
| 221 | if (!weak_ptr) |
| 222 | return; |
| 223 | using Traits = FunctorTraits<typename std::decay<Functor>::type>; |
| 224 | Traits::Invoke(std::forward<Functor>(functor), |
| 225 | std::forward<BoundWeakPtr>(weak_ptr), |
| 226 | std::forward<RunArgs>(args)...); |
| 227 | } |
| 228 | }; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 229 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 230 | // Invoker<> |
| 231 | // |
| 232 | // See description at the top of the file. |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 233 | template <typename StorageType, typename UnboundRunType> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 234 | struct Invoker; |
| 235 | |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 236 | template <typename StorageType, typename R, typename... UnboundArgs> |
| 237 | struct Invoker<StorageType, R(UnboundArgs...)> { |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 238 | static R Run(BindStateBase* base, UnboundArgs&&... unbound_args) { |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 239 | // Local references to make debugger stepping easier. If in a debugger, |
| 240 | // you really want to warp ahead and step through the |
| 241 | // InvokeHelper<>::MakeItSo() call below. |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 242 | const StorageType* storage = static_cast<StorageType*>(base); |
| 243 | static constexpr size_t num_bound_args = |
| 244 | std::tuple_size<decltype(storage->bound_args_)>::value; |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 245 | return RunImpl(storage->functor_, |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 246 | storage->bound_args_, |
| 247 | MakeIndexSequence<num_bound_args>(), |
| 248 | std::forward<UnboundArgs>(unbound_args)...); |
| 249 | } |
| 250 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 251 | private: |
| 252 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 253 | static inline R RunImpl(Functor&& functor, |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 254 | BoundArgsTuple&& bound, |
| 255 | IndexSequence<indices...>, |
| 256 | UnboundArgs&&... unbound_args) { |
| 257 | static constexpr bool is_method = |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 258 | FunctorTraits<typename std::decay<Functor>::type>::is_method; |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 259 | |
| 260 | using DecayedArgsTuple = typename std::decay<BoundArgsTuple>::type; |
| 261 | static constexpr bool is_weak_call = |
| 262 | IsWeakMethod<is_method, |
| 263 | typename std::tuple_element< |
| 264 | indices, |
| 265 | DecayedArgsTuple>::type...>::value; |
| 266 | |
tzik | e1ad6f6 | 2016-06-01 17:22:51 +0900 | [diff] [blame] | 267 | return InvokeHelper<is_weak_call, R>::MakeItSo( |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 268 | std::forward<Functor>(functor), |
| 269 | Unwrap(base::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 270 | std::forward<UnboundArgs>(unbound_args)...); |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 271 | } |
| 272 | }; |
| 273 | |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 274 | // Used to implement MakeUnboundRunType. |
| 275 | template <typename Functor, typename... BoundArgs> |
| 276 | struct MakeUnboundRunTypeImpl { |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 277 | using RunType = |
| 278 | typename FunctorTraits<typename std::decay<Functor>::type>::RunType; |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 279 | using ReturnType = ExtractReturnType<RunType>; |
| 280 | using Args = ExtractArgs<RunType>; |
| 281 | using UnboundArgs = DropTypeListItem<sizeof...(BoundArgs), Args>; |
| 282 | using Type = MakeFunctionType<ReturnType, UnboundArgs>; |
| 283 | }; |
| 284 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 285 | // BindState<> |
| 286 | // |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 287 | // This stores all the state passed into Bind(). |
| 288 | template <typename Functor, typename... BoundArgs> |
| 289 | struct BindState final : BindStateBase { |
| 290 | template <typename... ForwardBoundArgs> |
| 291 | explicit BindState(Functor functor, ForwardBoundArgs&&... bound_args) |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 292 | : BindStateBase(&Destroy), |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 293 | functor_(std::move(functor)), |
| 294 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 295 | DCHECK(functor_); |
| 296 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 297 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 298 | Functor functor_; |
| 299 | std::tuple<BoundArgs...> bound_args_; |
dmichael | 2f7bc20 | 2014-12-19 07:30:11 +0900 | [diff] [blame] | 300 | |
| 301 | private: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 302 | ~BindState() {} |
| 303 | |
| 304 | static void Destroy(BindStateBase* self) { |
| 305 | delete static_cast<BindState*>(self); |
| 306 | } |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 307 | }; |
| 308 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame^] | 309 | // Used to implement MakeBindStateType. |
| 310 | template <bool is_method, typename Functor, typename... BoundArgs> |
| 311 | struct MakeBindStateTypeImpl; |
| 312 | |
| 313 | template <typename Functor, typename... BoundArgs> |
| 314 | struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { |
| 315 | static_assert(!HasRefCountedTypeAsRawPtr<BoundArgs...>::value, |
| 316 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 317 | using Type = BindState<typename std::decay<Functor>::type, |
| 318 | typename std::decay<BoundArgs>::type...>; |
| 319 | }; |
| 320 | |
| 321 | template <typename Functor> |
| 322 | struct MakeBindStateTypeImpl<true, Functor> { |
| 323 | using Type = BindState<typename std::decay<Functor>::type>; |
| 324 | }; |
| 325 | |
| 326 | template <typename Functor, typename Receiver, typename... BoundArgs> |
| 327 | struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { |
| 328 | static_assert( |
| 329 | !std::is_array<typename std::remove_reference<Receiver>::type>::value, |
| 330 | "First bound argument to a method cannot be an array."); |
| 331 | static_assert(!HasRefCountedTypeAsRawPtr<BoundArgs...>::value, |
| 332 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 333 | |
| 334 | private: |
| 335 | using DecayedReceiver = typename std::decay<Receiver>::type; |
| 336 | |
| 337 | public: |
| 338 | using Type = BindState< |
| 339 | typename std::decay<Functor>::type, |
| 340 | typename std::conditional< |
| 341 | std::is_pointer<DecayedReceiver>::value, |
| 342 | scoped_refptr<typename std::remove_pointer<DecayedReceiver>::type>, |
| 343 | DecayedReceiver>::type, |
| 344 | typename std::decay<BoundArgs>::type...>; |
| 345 | }; |
| 346 | |
| 347 | template <typename Functor, typename... BoundArgs> |
| 348 | using MakeBindStateType = typename MakeBindStateTypeImpl< |
| 349 | FunctorTraits<typename std::decay<Functor>::type>::is_method, |
| 350 | Functor, |
| 351 | BoundArgs...>::Type; |
| 352 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 353 | } // namespace internal |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 354 | |
| 355 | // Returns a RunType of bound functor. |
| 356 | // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 357 | template <typename Functor, typename... BoundArgs> |
| 358 | using MakeUnboundRunType = |
| 359 | typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; |
| 360 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 361 | } // namespace base |
| 362 | |
| 363 | #endif // BASE_BIND_INTERNAL_H_ |