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