[email protected] | b38d357 | 2011-02-15 01:27:38 | [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_ |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 10 | #include <type_traits> |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 11 | #include <utility> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 12 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 13 | #include "base/bind_helpers.h" |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 14 | #include "base/callback_internal.h" |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 15 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 17 | #include "base/template_util.h" |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 18 | #include "build/build_config.h" |
| 19 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 20 | namespace base { |
| 21 | namespace internal { |
| 22 | |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 23 | // See base/callback.h for user documentation. |
| 24 | // |
| 25 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 26 | // CONCEPTS: |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 27 | // Functor -- A movable type representing something that should be called. |
| 28 | // All function pointers and Callback<> are functors even if the |
| 29 | // invocation syntax differs. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 30 | // RunType -- A function type (as opposed to function _pointer_ type) for |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 31 | // a Callback<>::Run(). Usually just a convenience typedef. |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 32 | // (Bound)Args -- A set of types that stores the arguments. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 33 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 34 | // Types: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 35 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 36 | // equivalent forms with a "void" return type. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 37 | // FunctorTraits<> -- Type traits used to determine the correct RunType and |
| 38 | // invocation manner for a Functor. This is where function |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 39 | // signature adapters are applied. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 40 | // InvokeHelper<> -- Take a Functor + arguments and actully invokes it. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 41 | // Handle the differing syntaxes needed for WeakPtr<> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 42 | // support. This is separate from Invoker to avoid creating |
| 43 | // multiple version of Invoker<>. |
| 44 | // Invoker<> -- Unwraps the curried parameters and executes the Functor. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 45 | // BindState<> -- Stores the curried parameters, and is the main entry point |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 46 | // into the Bind() system. |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 47 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 48 | template <typename Callable, |
| 49 | typename Signature = decltype(&Callable::operator())> |
| 50 | struct ExtractCallableRunTypeImpl; |
| 51 | |
| 52 | template <typename Callable, typename R, typename... Args> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 53 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...)> { |
| 54 | using Type = R(Args...); |
| 55 | }; |
| 56 | |
| 57 | template <typename Callable, typename R, typename... Args> |
| 58 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...) const> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 59 | using Type = R(Args...); |
| 60 | }; |
| 61 | |
| 62 | // Evaluated to RunType of the given callable type. |
| 63 | // Example: |
| 64 | // auto f = [](int, char*) { return 0.1; }; |
| 65 | // ExtractCallableRunType<decltype(f)> |
| 66 | // is evaluated to |
| 67 | // double(int, char*); |
| 68 | template <typename Callable> |
| 69 | using ExtractCallableRunType = |
| 70 | typename ExtractCallableRunTypeImpl<Callable>::Type; |
| 71 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 72 | // IsCallableObject<Functor> is std::true_type if |Functor| has operator(). |
| 73 | // Otherwise, it's std::false_type. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 74 | // Example: |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 75 | // IsCallableObject<void(*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 76 | // |
| 77 | // struct Foo {}; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 78 | // IsCallableObject<void(Foo::*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 79 | // |
| 80 | // int i = 0; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 81 | // auto f = [i]() {}; |
| 82 | // IsCallableObject<decltype(f)>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 83 | template <typename Functor, typename SFINAE = void> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 84 | struct IsCallableObject : std::false_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 85 | |
| 86 | template <typename Callable> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 87 | struct IsCallableObject<Callable, void_t<decltype(&Callable::operator())>> |
| 88 | : std::true_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 89 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 90 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 91 | // pointer to a RefCounted type. |
| 92 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 93 | // Non-zero-arity cases should be handled by the specialization below. |
| 94 | template <typename... Args> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 95 | struct HasRefCountedTypeAsRawPtr : std::false_type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 96 | |
| 97 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 98 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 99 | // parameters recursively. |
| 100 | template <typename T, typename... Args> |
| 101 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 102 | : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value, |
| 103 | std::true_type, |
| 104 | HasRefCountedTypeAsRawPtr<Args...>> {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 105 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 106 | // ForceVoidReturn<> |
| 107 | // |
| 108 | // Set of templates that support forcing the function return type to void. |
| 109 | template <typename Sig> |
| 110 | struct ForceVoidReturn; |
| 111 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 112 | template <typename R, typename... Args> |
| 113 | struct ForceVoidReturn<R(Args...)> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 114 | using RunType = void(Args...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 117 | // FunctorTraits<> |
| 118 | // |
| 119 | // See description at top of file. |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 120 | template <typename Functor, typename SFINAE> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 121 | struct FunctorTraits; |
| 122 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 123 | // For empty callable types. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 124 | // This specialization is intended to allow binding captureless lambdas by |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 125 | // base::Bind(), based on the fact that captureless lambdas are empty while |
| 126 | // capturing lambdas are not. This also allows any functors as far as it's an |
| 127 | // empty class. |
| 128 | // Example: |
| 129 | // |
| 130 | // // Captureless lambdas are allowed. |
| 131 | // []() {return 42;}; |
| 132 | // |
| 133 | // // Capturing lambdas are *not* allowed. |
| 134 | // int x; |
| 135 | // [x]() {return x;}; |
| 136 | // |
| 137 | // // Any empty class with operator() is allowed. |
| 138 | // struct Foo { |
| 139 | // void operator()() const {} |
| 140 | // // No non-static member variable and no virtual functions. |
| 141 | // }; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 142 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 143 | struct FunctorTraits<Functor, |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 144 | std::enable_if_t<IsCallableObject<Functor>::value && |
| 145 | std::is_empty<Functor>::value>> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 146 | using RunType = ExtractCallableRunType<Functor>; |
| 147 | static constexpr bool is_method = false; |
| 148 | static constexpr bool is_nullable = false; |
| 149 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 150 | template <typename RunFunctor, typename... RunArgs> |
| 151 | static ExtractReturnType<RunType> Invoke(RunFunctor&& functor, |
| 152 | RunArgs&&... args) { |
| 153 | return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...); |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 154 | } |
| 155 | }; |
| 156 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 157 | // For functions. |
| 158 | template <typename R, typename... Args> |
| 159 | struct FunctorTraits<R (*)(Args...)> { |
| 160 | using RunType = R(Args...); |
| 161 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 162 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 163 | |
| 164 | template <typename... RunArgs> |
| 165 | static R Invoke(R (*function)(Args...), RunArgs&&... args) { |
| 166 | return function(std::forward<RunArgs>(args)...); |
| 167 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 168 | }; |
| 169 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 170 | #if defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 171 | |
| 172 | // For functions. |
| 173 | template <typename R, typename... Args> |
| 174 | struct FunctorTraits<R(__stdcall*)(Args...)> { |
| 175 | using RunType = R(Args...); |
| 176 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 177 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 178 | |
| 179 | template <typename... RunArgs> |
| 180 | static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) { |
| 181 | return function(std::forward<RunArgs>(args)...); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | // For functions. |
| 186 | template <typename R, typename... Args> |
| 187 | struct FunctorTraits<R(__fastcall*)(Args...)> { |
| 188 | using RunType = R(Args...); |
| 189 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 190 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 191 | |
| 192 | template <typename... RunArgs> |
| 193 | static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) { |
| 194 | return function(std::forward<RunArgs>(args)...); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | #endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 199 | |
| 200 | // For methods. |
| 201 | template <typename R, typename Receiver, typename... Args> |
| 202 | struct FunctorTraits<R (Receiver::*)(Args...)> { |
| 203 | using RunType = R(Receiver*, Args...); |
| 204 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 205 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 206 | |
| 207 | template <typename ReceiverPtr, typename... RunArgs> |
| 208 | static R Invoke(R (Receiver::*method)(Args...), |
| 209 | ReceiverPtr&& receiver_ptr, |
| 210 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 211 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 212 | } |
| 213 | }; |
| 214 | |
| 215 | // For const methods. |
| 216 | template <typename R, typename Receiver, typename... Args> |
| 217 | struct FunctorTraits<R (Receiver::*)(Args...) const> { |
| 218 | using RunType = R(const Receiver*, Args...); |
| 219 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 220 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 221 | |
| 222 | template <typename ReceiverPtr, typename... RunArgs> |
| 223 | static R Invoke(R (Receiver::*method)(Args...) const, |
| 224 | ReceiverPtr&& receiver_ptr, |
| 225 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 226 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| 230 | // For IgnoreResults. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 231 | template <typename T> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 232 | struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 233 | using RunType = |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 234 | typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType; |
| 235 | |
| 236 | template <typename IgnoreResultType, typename... RunArgs> |
| 237 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 238 | RunArgs&&... args) { |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 239 | FunctorTraits<T>::Invoke( |
| 240 | std::forward<IgnoreResultType>(ignore_result_helper).functor_, |
| 241 | std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 242 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 243 | }; |
| 244 | |
tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 245 | // For OnceCallbacks. |
| 246 | template <typename R, typename... Args> |
| 247 | struct FunctorTraits<OnceCallback<R(Args...)>> { |
| 248 | using RunType = R(Args...); |
| 249 | static constexpr bool is_method = false; |
| 250 | static constexpr bool is_nullable = true; |
| 251 | |
| 252 | template <typename CallbackType, typename... RunArgs> |
| 253 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 254 | DCHECK(!callback.is_null()); |
| 255 | return std::forward<CallbackType>(callback).Run( |
| 256 | std::forward<RunArgs>(args)...); |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | // For RepeatingCallbacks. |
| 261 | template <typename R, typename... Args> |
| 262 | struct FunctorTraits<RepeatingCallback<R(Args...)>> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 263 | using RunType = R(Args...); |
| 264 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 265 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 266 | |
| 267 | template <typename CallbackType, typename... RunArgs> |
| 268 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 269 | DCHECK(!callback.is_null()); |
| 270 | return std::forward<CallbackType>(callback).Run( |
| 271 | std::forward<RunArgs>(args)...); |
| 272 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 273 | }; |
| 274 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 275 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 276 | using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 277 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 278 | // InvokeHelper<> |
| 279 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 280 | // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 281 | // |
| 282 | // The normal type just calls the underlying runnable. |
| 283 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 284 | // WeakCalls need special syntax that is applied to the first argument to check |
| 285 | // if they should no-op themselves. |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 286 | template <bool is_weak_call, typename ReturnType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 287 | struct InvokeHelper; |
| 288 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 289 | template <typename ReturnType> |
| 290 | struct InvokeHelper<false, ReturnType> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 291 | template <typename Functor, typename... RunArgs> |
| 292 | static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 293 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 294 | return Traits::Invoke(std::forward<Functor>(functor), |
| 295 | std::forward<RunArgs>(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 296 | } |
| 297 | }; |
| 298 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 299 | template <typename ReturnType> |
| 300 | struct InvokeHelper<true, ReturnType> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 301 | // WeakCalls are only supported for functions with a void return type. |
| 302 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 303 | // is invalidated. |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 304 | static_assert(std::is_void<ReturnType>::value, |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 305 | "weak_ptrs can only bind to methods without return values"); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 306 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 307 | template <typename Functor, typename BoundWeakPtr, typename... RunArgs> |
| 308 | static inline void MakeItSo(Functor&& functor, |
tzik | 33871d8 | 2016-07-14 12:12:06 | [diff] [blame] | 309 | BoundWeakPtr&& weak_ptr, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 310 | RunArgs&&... args) { |
| 311 | if (!weak_ptr) |
| 312 | return; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 313 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 314 | Traits::Invoke(std::forward<Functor>(functor), |
| 315 | std::forward<BoundWeakPtr>(weak_ptr), |
| 316 | std::forward<RunArgs>(args)...); |
| 317 | } |
| 318 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 319 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 320 | // Invoker<> |
| 321 | // |
| 322 | // See description at the top of the file. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 323 | template <typename StorageType, typename UnboundRunType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 324 | struct Invoker; |
| 325 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 326 | template <typename StorageType, typename R, typename... UnboundArgs> |
| 327 | struct Invoker<StorageType, R(UnboundArgs...)> { |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 328 | static R RunOnce(BindStateBase* base, |
| 329 | PassingTraitsType<UnboundArgs>... unbound_args) { |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 330 | // Local references to make debugger stepping easier. If in a debugger, |
| 331 | // you really want to warp ahead and step through the |
| 332 | // InvokeHelper<>::MakeItSo() call below. |
| 333 | StorageType* storage = static_cast<StorageType*>(base); |
| 334 | static constexpr size_t num_bound_args = |
| 335 | std::tuple_size<decltype(storage->bound_args_)>::value; |
| 336 | return RunImpl(std::move(storage->functor_), |
| 337 | std::move(storage->bound_args_), |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 338 | std::make_index_sequence<num_bound_args>(), |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 339 | std::forward<UnboundArgs>(unbound_args)...); |
| 340 | } |
| 341 | |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 342 | static R Run(BindStateBase* base, |
| 343 | PassingTraitsType<UnboundArgs>... unbound_args) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 344 | // Local references to make debugger stepping easier. If in a debugger, |
| 345 | // you really want to warp ahead and step through the |
| 346 | // InvokeHelper<>::MakeItSo() call below. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 347 | const StorageType* storage = static_cast<StorageType*>(base); |
| 348 | static constexpr size_t num_bound_args = |
| 349 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 350 | return RunImpl(storage->functor_, storage->bound_args_, |
| 351 | std::make_index_sequence<num_bound_args>(), |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 352 | std::forward<UnboundArgs>(unbound_args)...); |
| 353 | } |
| 354 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 355 | private: |
| 356 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 357 | static inline R RunImpl(Functor&& functor, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 358 | BoundArgsTuple&& bound, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 359 | std::index_sequence<indices...>, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 360 | UnboundArgs&&... unbound_args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 361 | static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 362 | |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 363 | using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 364 | static constexpr bool is_weak_call = |
| 365 | IsWeakMethod<is_method, |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 366 | std::tuple_element_t<indices, DecayedArgsTuple>...>(); |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 367 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 368 | return InvokeHelper<is_weak_call, R>::MakeItSo( |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 369 | std::forward<Functor>(functor), |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 370 | Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 371 | std::forward<UnboundArgs>(unbound_args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 372 | } |
| 373 | }; |
| 374 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 375 | // Extracts necessary type info from Functor and BoundArgs. |
| 376 | // Used to implement MakeUnboundRunType, BindOnce and BindRepeating. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 377 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 378 | struct BindTypeHelper { |
| 379 | static constexpr size_t num_bounds = sizeof...(BoundArgs); |
| 380 | using FunctorTraits = MakeFunctorTraits<Functor>; |
| 381 | |
| 382 | // Example: |
| 383 | // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs |
| 384 | // is a template pack of `Foo*` and `int16_t`: |
| 385 | // - RunType is `double(Foo*, int, const std::string&)`, |
| 386 | // - ReturnType is `double`, |
| 387 | // - RunParamsList is `TypeList<Foo*, int, const std::string&>`, |
| 388 | // - BoundParamsList is `TypeList<Foo*, int>`, |
| 389 | // - UnboundParamsList is `TypeList<const std::string&>`, |
| 390 | // - BoundArgsList is `TypeList<Foo*, int16_t>`, |
| 391 | // - UnboundRunType is `double(const std::string&)`. |
| 392 | using RunType = typename FunctorTraits::RunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 393 | using ReturnType = ExtractReturnType<RunType>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 394 | |
| 395 | using RunParamsList = ExtractArgs<RunType>; |
| 396 | using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>; |
| 397 | using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>; |
| 398 | |
| 399 | using BoundArgsList = TypeList<BoundArgs...>; |
| 400 | |
| 401 | using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 402 | }; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 403 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 404 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 405 | std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 406 | const Functor& functor) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 407 | return !functor; |
| 408 | } |
| 409 | |
| 410 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 411 | std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 412 | const Functor&) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 413 | return false; |
| 414 | } |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 415 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 416 | // Used by ApplyCancellationTraits below. |
| 417 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 418 | bool ApplyCancellationTraitsImpl(const Functor& functor, |
| 419 | const BoundArgsTuple& bound_args, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 420 | std::index_sequence<indices...>) { |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 421 | return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled( |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 422 | functor, std::get<indices>(bound_args)...); |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 423 | } |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 424 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 425 | // Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns |
| 426 | // true if the callback |base| represents is canceled. |
| 427 | template <typename BindStateType> |
| 428 | bool ApplyCancellationTraits(const BindStateBase* base) { |
| 429 | const BindStateType* storage = static_cast<const BindStateType*>(base); |
| 430 | static constexpr size_t num_bound_args = |
| 431 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 432 | return ApplyCancellationTraitsImpl( |
| 433 | storage->functor_, storage->bound_args_, |
| 434 | std::make_index_sequence<num_bound_args>()); |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 435 | }; |
| 436 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 437 | // BindState<> |
| 438 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 439 | // This stores all the state passed into Bind(). |
| 440 | template <typename Functor, typename... BoundArgs> |
| 441 | struct BindState final : BindStateBase { |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 442 | using IsCancellable = std::integral_constant< |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 443 | bool, |
| 444 | CallbackCancellationTraits<Functor, |
| 445 | std::tuple<BoundArgs...>>::is_cancellable>; |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 446 | |
tzik | bfe6612 | 2016-07-08 14:14:01 | [diff] [blame] | 447 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
tzik | 1886c27 | 2016-09-08 05:45:38 | [diff] [blame] | 448 | explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 449 | ForwardFunctor&& functor, |
| 450 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 451 | // IsCancellable is std::false_type if |
| 452 | // CallbackCancellationTraits<>::IsCancelled returns always false. |
| 453 | // Otherwise, it's std::true_type. |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 454 | : BindState(IsCancellable{}, |
| 455 | invoke_func, |
| 456 | std::forward<ForwardFunctor>(functor), |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 457 | std::forward<ForwardBoundArgs>(bound_args)...) {} |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 458 | |
| 459 | Functor functor_; |
| 460 | std::tuple<BoundArgs...> bound_args_; |
| 461 | |
| 462 | private: |
| 463 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 464 | explicit BindState(std::true_type, |
| 465 | BindStateBase::InvokeFuncStorage invoke_func, |
| 466 | ForwardFunctor&& functor, |
| 467 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 468 | : BindStateBase(invoke_func, |
| 469 | &Destroy, |
| 470 | &ApplyCancellationTraits<BindState>), |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 471 | functor_(std::forward<ForwardFunctor>(functor)), |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 472 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 473 | DCHECK(!IsNull(functor_)); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 474 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 475 | |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 476 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 477 | explicit BindState(std::false_type, |
| 478 | BindStateBase::InvokeFuncStorage invoke_func, |
| 479 | ForwardFunctor&& functor, |
| 480 | ForwardBoundArgs&&... bound_args) |
| 481 | : BindStateBase(invoke_func, &Destroy), |
| 482 | functor_(std::forward<ForwardFunctor>(functor)), |
| 483 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 484 | DCHECK(!IsNull(functor_)); |
| 485 | } |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 486 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 487 | ~BindState() = default; |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 488 | |
tzik | 30e0c31 | 2016-09-21 08:06:54 | [diff] [blame] | 489 | static void Destroy(const BindStateBase* self) { |
| 490 | delete static_cast<const BindState*>(self); |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 491 | } |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 492 | }; |
| 493 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 494 | // Used to implement MakeBindStateType. |
| 495 | template <bool is_method, typename Functor, typename... BoundArgs> |
| 496 | struct MakeBindStateTypeImpl; |
| 497 | |
| 498 | template <typename Functor, typename... BoundArgs> |
| 499 | struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 500 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 501 | "A parameter is a refcounted type and needs scoped_refptr."); |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 502 | using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | template <typename Functor> |
| 506 | struct MakeBindStateTypeImpl<true, Functor> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 507 | using Type = BindState<std::decay_t<Functor>>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | template <typename Functor, typename Receiver, typename... BoundArgs> |
| 511 | struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 512 | static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value, |
| 513 | "First bound argument to a method cannot be an array."); |
| 514 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 515 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 516 | |
| 517 | private: |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 518 | using DecayedReceiver = std::decay_t<Receiver>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 519 | |
| 520 | public: |
| 521 | using Type = BindState< |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 522 | std::decay_t<Functor>, |
| 523 | std::conditional_t<std::is_pointer<DecayedReceiver>::value, |
| 524 | scoped_refptr<std::remove_pointer_t<DecayedReceiver>>, |
| 525 | DecayedReceiver>, |
| 526 | std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 530 | using MakeBindStateType = |
| 531 | typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method, |
| 532 | Functor, |
| 533 | BoundArgs...>::Type; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 534 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 535 | } // namespace internal |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 536 | |
| 537 | // Returns a RunType of bound functor. |
| 538 | // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 539 | template <typename Functor, typename... BoundArgs> |
| 540 | using MakeUnboundRunType = |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 541 | typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 542 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 543 | } // namespace base |
| 544 | |
| 545 | #endif // BASE_BIND_INTERNAL_H_ |