[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] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 13 | #include "base/callback_internal.h" |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 14 | #include "base/compiler_specific.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 | |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 20 | #if defined(OS_MACOSX) && !HAS_FEATURE(objc_arc) |
| 21 | #include "base/mac/scoped_block.h" |
| 22 | #endif |
| 23 | |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 24 | // See base/callback.h for user documentation. |
| 25 | // |
| 26 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 27 | // CONCEPTS: |
tzik | 99de02b | 2016-07-01 05:54:12 | [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. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 31 | // RunType -- A function type (as opposed to function _pointer_ type) for |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 32 | // a Callback<>::Run(). Usually just a convenience typedef. |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 33 | // (Bound)Args -- A set of types that stores the arguments. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 34 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 35 | // Types: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 36 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 37 | // equivalent forms with a "void" return type. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 38 | // FunctorTraits<> -- Type traits used to determine the correct RunType and |
| 39 | // invocation manner for a Functor. This is where function |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 40 | // signature adapters are applied. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 41 | // InvokeHelper<> -- Take a Functor + arguments and actully invokes it. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 42 | // Handle the differing syntaxes needed for WeakPtr<> |
tzik | 99de02b | 2016-07-01 05:54:12 | [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. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 46 | // BindState<> -- Stores the curried parameters, and is the main entry point |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 47 | // into the Bind() system. |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 48 | |
tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame^] | 49 | #if defined(OS_WIN) |
| 50 | namespace Microsoft { |
| 51 | namespace WRL { |
| 52 | template <typename> |
| 53 | class ComPtr; |
| 54 | } // namespace WRL |
| 55 | } // namespace Microsoft |
| 56 | #endif |
| 57 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 58 | namespace base { |
| 59 | |
| 60 | template <typename T> |
| 61 | struct IsWeakReceiver; |
| 62 | |
| 63 | template <typename> |
| 64 | struct BindUnwrapTraits; |
| 65 | |
| 66 | template <typename Functor, typename BoundArgsTuple, typename SFINAE = void> |
| 67 | struct CallbackCancellationTraits; |
| 68 | |
| 69 | namespace internal { |
| 70 | |
| 71 | template <typename Functor, typename SFINAE = void> |
| 72 | struct FunctorTraits; |
| 73 | |
| 74 | template <typename T> |
| 75 | class UnretainedWrapper { |
| 76 | public: |
| 77 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 78 | T* get() const { return ptr_; } |
| 79 | |
| 80 | private: |
| 81 | T* ptr_; |
| 82 | }; |
| 83 | |
| 84 | template <typename T> |
| 85 | class ConstRefWrapper { |
| 86 | public: |
| 87 | explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
| 88 | const T& get() const { return *ptr_; } |
| 89 | |
| 90 | private: |
| 91 | const T* ptr_; |
| 92 | }; |
| 93 | |
| 94 | template <typename T> |
| 95 | class RetainedRefWrapper { |
| 96 | public: |
| 97 | explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 98 | explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 99 | T* get() const { return ptr_.get(); } |
| 100 | |
| 101 | private: |
| 102 | scoped_refptr<T> ptr_; |
| 103 | }; |
| 104 | |
| 105 | template <typename T> |
| 106 | struct IgnoreResultHelper { |
| 107 | explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
| 108 | explicit operator bool() const { return !!functor_; } |
| 109 | |
| 110 | T functor_; |
| 111 | }; |
| 112 | |
| 113 | // An alternate implementation is to avoid the destructive copy, and instead |
| 114 | // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
| 115 | // a class that is essentially a std::unique_ptr<>. |
| 116 | // |
| 117 | // The current implementation has the benefit though of leaving ParamTraits<> |
| 118 | // fully in callback_internal.h as well as avoiding type conversions during |
| 119 | // storage. |
| 120 | template <typename T> |
| 121 | class OwnedWrapper { |
| 122 | public: |
| 123 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| 124 | ~OwnedWrapper() { delete ptr_; } |
| 125 | T* get() const { return ptr_; } |
| 126 | OwnedWrapper(OwnedWrapper&& other) { |
| 127 | ptr_ = other.ptr_; |
| 128 | other.ptr_ = NULL; |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | mutable T* ptr_; |
| 133 | }; |
| 134 | |
| 135 | // PassedWrapper is a copyable adapter for a scoper that ignores const. |
| 136 | // |
| 137 | // It is needed to get around the fact that Bind() takes a const reference to |
| 138 | // all its arguments. Because Bind() takes a const reference to avoid |
| 139 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| 140 | // types; doing a destructive "move" of the type into Bind() would violate |
| 141 | // the const correctness. |
| 142 | // |
| 143 | // This conundrum cannot be solved without either C++11 rvalue references or |
| 144 | // a O(2^n) blowup of Bind() templates to handle each combination of regular |
| 145 | // types and movable-but-not-copyable types. Thus we introduce a wrapper type |
| 146 | // that is copyable to transmit the correct type information down into |
| 147 | // BindState<>. Ignoring const in this type makes sense because it is only |
| 148 | // created when we are explicitly trying to do a destructive move. |
| 149 | // |
| 150 | // Two notes: |
| 151 | // 1) PassedWrapper supports any type that has a move constructor, however |
| 152 | // the type will need to be specifically whitelisted in order for it to be |
| 153 | // bound to a Callback. We guard this explicitly at the call of Passed() |
| 154 | // to make for clear errors. Things not given to Passed() will be forwarded |
| 155 | // and stored by value which will not work for general move-only types. |
| 156 | // 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL" |
| 157 | // scoper to a Callback and allow the Callback to execute once. |
| 158 | template <typename T> |
| 159 | class PassedWrapper { |
| 160 | public: |
| 161 | explicit PassedWrapper(T&& scoper) |
| 162 | : is_valid_(true), scoper_(std::move(scoper)) {} |
| 163 | PassedWrapper(PassedWrapper&& other) |
| 164 | : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {} |
| 165 | T Take() const { |
| 166 | CHECK(is_valid_); |
| 167 | is_valid_ = false; |
| 168 | return std::move(scoper_); |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | mutable bool is_valid_; |
| 173 | mutable T scoper_; |
| 174 | }; |
| 175 | |
| 176 | template <typename T> |
| 177 | using Unwrapper = BindUnwrapTraits<std::decay_t<T>>; |
| 178 | |
| 179 | template <typename T> |
Peter Kasting | c2f8749bf | 2018-03-31 03:32:37 | [diff] [blame] | 180 | decltype(auto) Unwrap(T&& o) { |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 181 | return Unwrapper<T>::Unwrap(std::forward<T>(o)); |
| 182 | } |
| 183 | |
| 184 | // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a |
| 185 | // method. It is used internally by Bind() to select the correct |
| 186 | // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
| 187 | // the target object is invalidated. |
| 188 | // |
| 189 | // The first argument should be the type of the object that will be received by |
| 190 | // the method. |
| 191 | template <bool is_method, typename... Args> |
| 192 | struct IsWeakMethod : std::false_type {}; |
| 193 | |
| 194 | template <typename T, typename... Args> |
| 195 | struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {}; |
| 196 | |
| 197 | // Packs a list of types to hold them in a single type. |
| 198 | template <typename... Types> |
| 199 | struct TypeList {}; |
| 200 | |
| 201 | // Used for DropTypeListItem implementation. |
| 202 | template <size_t n, typename List> |
| 203 | struct DropTypeListItemImpl; |
| 204 | |
| 205 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 206 | template <size_t n, typename T, typename... List> |
| 207 | struct DropTypeListItemImpl<n, TypeList<T, List...>> |
| 208 | : DropTypeListItemImpl<n - 1, TypeList<List...>> {}; |
| 209 | |
| 210 | template <typename T, typename... List> |
| 211 | struct DropTypeListItemImpl<0, TypeList<T, List...>> { |
| 212 | using Type = TypeList<T, List...>; |
| 213 | }; |
| 214 | |
| 215 | template <> |
| 216 | struct DropTypeListItemImpl<0, TypeList<>> { |
| 217 | using Type = TypeList<>; |
| 218 | }; |
| 219 | |
| 220 | // A type-level function that drops |n| list item from given TypeList. |
| 221 | template <size_t n, typename List> |
| 222 | using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type; |
| 223 | |
| 224 | // Used for TakeTypeListItem implementation. |
| 225 | template <size_t n, typename List, typename... Accum> |
| 226 | struct TakeTypeListItemImpl; |
| 227 | |
| 228 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 229 | template <size_t n, typename T, typename... List, typename... Accum> |
| 230 | struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...> |
| 231 | : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {}; |
| 232 | |
| 233 | template <typename T, typename... List, typename... Accum> |
| 234 | struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> { |
| 235 | using Type = TypeList<Accum...>; |
| 236 | }; |
| 237 | |
| 238 | template <typename... Accum> |
| 239 | struct TakeTypeListItemImpl<0, TypeList<>, Accum...> { |
| 240 | using Type = TypeList<Accum...>; |
| 241 | }; |
| 242 | |
| 243 | // A type-level function that takes first |n| list item from given TypeList. |
| 244 | // E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to |
| 245 | // TypeList<A, B, C>. |
| 246 | template <size_t n, typename List> |
| 247 | using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type; |
| 248 | |
| 249 | // Used for ConcatTypeLists implementation. |
| 250 | template <typename List1, typename List2> |
| 251 | struct ConcatTypeListsImpl; |
| 252 | |
| 253 | template <typename... Types1, typename... Types2> |
| 254 | struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> { |
| 255 | using Type = TypeList<Types1..., Types2...>; |
| 256 | }; |
| 257 | |
| 258 | // A type-level function that concats two TypeLists. |
| 259 | template <typename List1, typename List2> |
| 260 | using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type; |
| 261 | |
| 262 | // Used for MakeFunctionType implementation. |
| 263 | template <typename R, typename ArgList> |
| 264 | struct MakeFunctionTypeImpl; |
| 265 | |
| 266 | template <typename R, typename... Args> |
| 267 | struct MakeFunctionTypeImpl<R, TypeList<Args...>> { |
| 268 | // MSVC 2013 doesn't support Type Alias of function types. |
| 269 | // Revisit this after we update it to newer version. |
| 270 | typedef R Type(Args...); |
| 271 | }; |
| 272 | |
| 273 | // A type-level function that constructs a function type that has |R| as its |
| 274 | // return type and has TypeLists items as its arguments. |
| 275 | template <typename R, typename ArgList> |
| 276 | using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type; |
| 277 | |
| 278 | // Used for ExtractArgs and ExtractReturnType. |
| 279 | template <typename Signature> |
| 280 | struct ExtractArgsImpl; |
| 281 | |
| 282 | template <typename R, typename... Args> |
| 283 | struct ExtractArgsImpl<R(Args...)> { |
| 284 | using ReturnType = R; |
| 285 | using ArgsList = TypeList<Args...>; |
| 286 | }; |
| 287 | |
| 288 | // A type-level function that extracts function arguments into a TypeList. |
| 289 | // E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>. |
| 290 | template <typename Signature> |
| 291 | using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList; |
| 292 | |
| 293 | // A type-level function that extracts the return type of a function. |
| 294 | // E.g. ExtractReturnType<R(A, B, C)> is evaluated to R. |
| 295 | template <typename Signature> |
| 296 | using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType; |
| 297 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 298 | template <typename Callable, |
| 299 | typename Signature = decltype(&Callable::operator())> |
| 300 | struct ExtractCallableRunTypeImpl; |
| 301 | |
| 302 | template <typename Callable, typename R, typename... Args> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 303 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...)> { |
| 304 | using Type = R(Args...); |
| 305 | }; |
| 306 | |
| 307 | template <typename Callable, typename R, typename... Args> |
| 308 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...) const> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 309 | using Type = R(Args...); |
| 310 | }; |
| 311 | |
| 312 | // Evaluated to RunType of the given callable type. |
| 313 | // Example: |
| 314 | // auto f = [](int, char*) { return 0.1; }; |
| 315 | // ExtractCallableRunType<decltype(f)> |
| 316 | // is evaluated to |
| 317 | // double(int, char*); |
| 318 | template <typename Callable> |
| 319 | using ExtractCallableRunType = |
| 320 | typename ExtractCallableRunTypeImpl<Callable>::Type; |
| 321 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 322 | // IsCallableObject<Functor> is std::true_type if |Functor| has operator(). |
| 323 | // Otherwise, it's std::false_type. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 324 | // Example: |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 325 | // IsCallableObject<void(*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 326 | // |
| 327 | // struct Foo {}; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 328 | // IsCallableObject<void(Foo::*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 329 | // |
| 330 | // int i = 0; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 331 | // auto f = [i]() {}; |
| 332 | // IsCallableObject<decltype(f)>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 333 | template <typename Functor, typename SFINAE = void> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 334 | struct IsCallableObject : std::false_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 335 | |
| 336 | template <typename Callable> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 337 | struct IsCallableObject<Callable, void_t<decltype(&Callable::operator())>> |
| 338 | : std::true_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 339 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 340 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 341 | // pointer to a RefCounted type. |
| 342 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 343 | // Non-zero-arity cases should be handled by the specialization below. |
| 344 | template <typename... Args> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 345 | struct HasRefCountedTypeAsRawPtr : std::false_type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 346 | |
| 347 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 348 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 349 | // parameters recursively. |
| 350 | template <typename T, typename... Args> |
| 351 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 352 | : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value, |
| 353 | std::true_type, |
| 354 | HasRefCountedTypeAsRawPtr<Args...>> {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 355 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 356 | // ForceVoidReturn<> |
| 357 | // |
| 358 | // Set of templates that support forcing the function return type to void. |
| 359 | template <typename Sig> |
| 360 | struct ForceVoidReturn; |
| 361 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 362 | template <typename R, typename... Args> |
| 363 | struct ForceVoidReturn<R(Args...)> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 364 | using RunType = void(Args...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 365 | }; |
| 366 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 367 | // FunctorTraits<> |
| 368 | // |
| 369 | // See description at top of file. |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 370 | template <typename Functor, typename SFINAE> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 371 | struct FunctorTraits; |
| 372 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 373 | // For empty callable types. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 374 | // This specialization is intended to allow binding captureless lambdas by |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 375 | // base::Bind(), based on the fact that captureless lambdas are empty while |
| 376 | // capturing lambdas are not. This also allows any functors as far as it's an |
| 377 | // empty class. |
| 378 | // Example: |
| 379 | // |
| 380 | // // Captureless lambdas are allowed. |
| 381 | // []() {return 42;}; |
| 382 | // |
| 383 | // // Capturing lambdas are *not* allowed. |
| 384 | // int x; |
| 385 | // [x]() {return x;}; |
| 386 | // |
| 387 | // // Any empty class with operator() is allowed. |
| 388 | // struct Foo { |
| 389 | // void operator()() const {} |
| 390 | // // No non-static member variable and no virtual functions. |
| 391 | // }; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 392 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 393 | struct FunctorTraits<Functor, |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 394 | std::enable_if_t<IsCallableObject<Functor>::value && |
| 395 | std::is_empty<Functor>::value>> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 396 | using RunType = ExtractCallableRunType<Functor>; |
| 397 | static constexpr bool is_method = false; |
| 398 | static constexpr bool is_nullable = false; |
| 399 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 400 | template <typename RunFunctor, typename... RunArgs> |
| 401 | static ExtractReturnType<RunType> Invoke(RunFunctor&& functor, |
| 402 | RunArgs&&... args) { |
| 403 | return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...); |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 404 | } |
| 405 | }; |
| 406 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 407 | // For functions. |
| 408 | template <typename R, typename... Args> |
| 409 | struct FunctorTraits<R (*)(Args...)> { |
| 410 | using RunType = R(Args...); |
| 411 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 412 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 413 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 414 | template <typename Function, typename... RunArgs> |
| 415 | static R Invoke(Function&& function, RunArgs&&... args) { |
| 416 | return std::forward<Function>(function)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 417 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 418 | }; |
| 419 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 420 | #if defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 421 | |
| 422 | // For functions. |
| 423 | template <typename R, typename... Args> |
| 424 | struct FunctorTraits<R(__stdcall*)(Args...)> { |
| 425 | using RunType = R(Args...); |
| 426 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 427 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 428 | |
| 429 | template <typename... RunArgs> |
| 430 | static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) { |
| 431 | return function(std::forward<RunArgs>(args)...); |
| 432 | } |
| 433 | }; |
| 434 | |
| 435 | // For functions. |
| 436 | template <typename R, typename... Args> |
| 437 | struct FunctorTraits<R(__fastcall*)(Args...)> { |
| 438 | using RunType = R(Args...); |
| 439 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 440 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 441 | |
| 442 | template <typename... RunArgs> |
| 443 | static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) { |
| 444 | return function(std::forward<RunArgs>(args)...); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | #endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 449 | |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 450 | #if defined(OS_MACOSX) |
| 451 | |
| 452 | // Support for Objective-C blocks. There are two implementation depending |
| 453 | // on whether Automated Reference Counting (ARC) is enabled. When ARC is |
| 454 | // enabled, then the block itself can be bound as the compiler will ensure |
| 455 | // its lifetime will be correctly managed. Otherwise, require the block to |
| 456 | // be wrapped in a base::mac::ScopedBlock (via base::RetainBlock) that will |
| 457 | // correctly manage the block lifetime. |
| 458 | // |
| 459 | // The two implementation ensure that the One Definition Rule (ODR) is not |
| 460 | // broken (it is not possible to write a template base::RetainBlock that would |
| 461 | // work correctly both with ARC enabled and disabled). |
| 462 | |
| 463 | #if HAS_FEATURE(objc_arc) |
| 464 | |
| 465 | template <typename R, typename... Args> |
| 466 | struct FunctorTraits<R (^)(Args...)> { |
| 467 | using RunType = R(Args...); |
| 468 | static constexpr bool is_method = false; |
| 469 | static constexpr bool is_nullable = true; |
| 470 | |
| 471 | template <typename BlockType, typename... RunArgs> |
| 472 | static R Invoke(BlockType&& block, RunArgs&&... args) { |
| 473 | // According to LLVM documentation (§ 6.3), "local variables of automatic |
| 474 | // storage duration do not have precise lifetime." Use objc_precise_lifetime |
| 475 | // to ensure that the Objective-C block is not deallocated until it has |
| 476 | // finished executing even if the Callback<> is destroyed during the block |
| 477 | // execution. |
| 478 | // https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics |
| 479 | __attribute__((objc_precise_lifetime)) R (^scoped_block)(Args...) = block; |
| 480 | return scoped_block(std::forward<RunArgs>(args)...); |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | #else // HAS_FEATURE(objc_arc) |
| 485 | |
| 486 | template <typename R, typename... Args> |
| 487 | struct FunctorTraits<base::mac::ScopedBlock<R (^)(Args...)>> { |
| 488 | using RunType = R(Args...); |
| 489 | static constexpr bool is_method = false; |
| 490 | static constexpr bool is_nullable = true; |
| 491 | |
| 492 | template <typename BlockType, typename... RunArgs> |
| 493 | static R Invoke(BlockType&& block, RunArgs&&... args) { |
| 494 | // Copy the block to ensure that the Objective-C block is not deallocated |
| 495 | // until it has finished executing even if the Callback<> is destroyed |
| 496 | // during the block execution. |
| 497 | base::mac::ScopedBlock<R (^)(Args...)> scoped_block(block); |
| 498 | return scoped_block.get()(std::forward<RunArgs>(args)...); |
| 499 | } |
| 500 | }; |
| 501 | |
| 502 | #endif // HAS_FEATURE(objc_arc) |
| 503 | #endif // defined(OS_MACOSX) |
| 504 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 505 | // For methods. |
| 506 | template <typename R, typename Receiver, typename... Args> |
| 507 | struct FunctorTraits<R (Receiver::*)(Args...)> { |
| 508 | using RunType = R(Receiver*, Args...); |
| 509 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 510 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 511 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 512 | template <typename Method, typename ReceiverPtr, typename... RunArgs> |
| 513 | static R Invoke(Method method, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 514 | ReceiverPtr&& receiver_ptr, |
| 515 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 516 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 517 | } |
| 518 | }; |
| 519 | |
| 520 | // For const methods. |
| 521 | template <typename R, typename Receiver, typename... Args> |
| 522 | struct FunctorTraits<R (Receiver::*)(Args...) const> { |
| 523 | using RunType = R(const Receiver*, Args...); |
| 524 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 525 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 526 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 527 | template <typename Method, typename ReceiverPtr, typename... RunArgs> |
| 528 | static R Invoke(Method method, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 529 | ReceiverPtr&& receiver_ptr, |
| 530 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 531 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 532 | } |
| 533 | }; |
| 534 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 535 | #ifdef __cpp_noexcept_function_type |
| 536 | // noexcept makes a distinct function type in C++17. |
| 537 | // I.e. `void(*)()` and `void(*)() noexcept` are same in pre-C++17, and |
| 538 | // different in C++17. |
| 539 | template <typename R, typename... Args> |
| 540 | struct FunctorTraits<R (*)(Args...) noexcept> : FunctorTraits<R (*)(Args...)> { |
| 541 | }; |
| 542 | |
| 543 | template <typename R, typename Receiver, typename... Args> |
| 544 | struct FunctorTraits<R (Receiver::*)(Args...) noexcept> |
| 545 | : FunctorTraits<R (Receiver::*)(Args...)> {}; |
| 546 | |
| 547 | template <typename R, typename Receiver, typename... Args> |
| 548 | struct FunctorTraits<R (Receiver::*)(Args...) const noexcept> |
| 549 | : FunctorTraits<R (Receiver::*)(Args...) const> {}; |
| 550 | #endif |
| 551 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 552 | // For IgnoreResults. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 553 | template <typename T> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 554 | struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 555 | using RunType = |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 556 | typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType; |
| 557 | |
| 558 | template <typename IgnoreResultType, typename... RunArgs> |
| 559 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 560 | RunArgs&&... args) { |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 561 | FunctorTraits<T>::Invoke( |
| 562 | std::forward<IgnoreResultType>(ignore_result_helper).functor_, |
| 563 | std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 564 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 565 | }; |
| 566 | |
tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 567 | // For OnceCallbacks. |
| 568 | template <typename R, typename... Args> |
| 569 | struct FunctorTraits<OnceCallback<R(Args...)>> { |
| 570 | using RunType = R(Args...); |
| 571 | static constexpr bool is_method = false; |
| 572 | static constexpr bool is_nullable = true; |
| 573 | |
| 574 | template <typename CallbackType, typename... RunArgs> |
| 575 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 576 | DCHECK(!callback.is_null()); |
| 577 | return std::forward<CallbackType>(callback).Run( |
| 578 | std::forward<RunArgs>(args)...); |
| 579 | } |
| 580 | }; |
| 581 | |
| 582 | // For RepeatingCallbacks. |
| 583 | template <typename R, typename... Args> |
| 584 | struct FunctorTraits<RepeatingCallback<R(Args...)>> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 585 | using RunType = R(Args...); |
| 586 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 587 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 588 | |
| 589 | template <typename CallbackType, typename... RunArgs> |
| 590 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 591 | DCHECK(!callback.is_null()); |
| 592 | return std::forward<CallbackType>(callback).Run( |
| 593 | std::forward<RunArgs>(args)...); |
| 594 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 595 | }; |
| 596 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 597 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 598 | using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 599 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 600 | // InvokeHelper<> |
| 601 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 602 | // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 603 | // |
| 604 | // The normal type just calls the underlying runnable. |
| 605 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 606 | // WeakCalls need special syntax that is applied to the first argument to check |
| 607 | // if they should no-op themselves. |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 608 | template <bool is_weak_call, typename ReturnType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 609 | struct InvokeHelper; |
| 610 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 611 | template <typename ReturnType> |
| 612 | struct InvokeHelper<false, ReturnType> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 613 | template <typename Functor, typename... RunArgs> |
| 614 | static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 615 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 616 | return Traits::Invoke(std::forward<Functor>(functor), |
| 617 | std::forward<RunArgs>(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 618 | } |
| 619 | }; |
| 620 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 621 | template <typename ReturnType> |
| 622 | struct InvokeHelper<true, ReturnType> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 623 | // WeakCalls are only supported for functions with a void return type. |
| 624 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 625 | // is invalidated. |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 626 | static_assert(std::is_void<ReturnType>::value, |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 627 | "weak_ptrs can only bind to methods without return values"); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 628 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 629 | template <typename Functor, typename BoundWeakPtr, typename... RunArgs> |
| 630 | static inline void MakeItSo(Functor&& functor, |
tzik | 33871d8 | 2016-07-14 12:12:06 | [diff] [blame] | 631 | BoundWeakPtr&& weak_ptr, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 632 | RunArgs&&... args) { |
| 633 | if (!weak_ptr) |
| 634 | return; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 635 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 636 | Traits::Invoke(std::forward<Functor>(functor), |
| 637 | std::forward<BoundWeakPtr>(weak_ptr), |
| 638 | std::forward<RunArgs>(args)...); |
| 639 | } |
| 640 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 641 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 642 | // Invoker<> |
| 643 | // |
| 644 | // See description at the top of the file. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 645 | template <typename StorageType, typename UnboundRunType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 646 | struct Invoker; |
| 647 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 648 | template <typename StorageType, typename R, typename... UnboundArgs> |
| 649 | struct Invoker<StorageType, R(UnboundArgs...)> { |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 650 | static R RunOnce(BindStateBase* base, |
tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 651 | PassingType<UnboundArgs>... unbound_args) { |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 652 | // Local references to make debugger stepping easier. If in a debugger, |
| 653 | // you really want to warp ahead and step through the |
| 654 | // InvokeHelper<>::MakeItSo() call below. |
| 655 | StorageType* storage = static_cast<StorageType*>(base); |
| 656 | static constexpr size_t num_bound_args = |
| 657 | std::tuple_size<decltype(storage->bound_args_)>::value; |
| 658 | return RunImpl(std::move(storage->functor_), |
| 659 | std::move(storage->bound_args_), |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 660 | std::make_index_sequence<num_bound_args>(), |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 661 | std::forward<UnboundArgs>(unbound_args)...); |
| 662 | } |
| 663 | |
tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 664 | static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 665 | // Local references to make debugger stepping easier. If in a debugger, |
| 666 | // you really want to warp ahead and step through the |
| 667 | // InvokeHelper<>::MakeItSo() call below. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 668 | const StorageType* storage = static_cast<StorageType*>(base); |
| 669 | static constexpr size_t num_bound_args = |
| 670 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 671 | return RunImpl(storage->functor_, storage->bound_args_, |
| 672 | std::make_index_sequence<num_bound_args>(), |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 673 | std::forward<UnboundArgs>(unbound_args)...); |
| 674 | } |
| 675 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 676 | private: |
| 677 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 678 | static inline R RunImpl(Functor&& functor, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 679 | BoundArgsTuple&& bound, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 680 | std::index_sequence<indices...>, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 681 | UnboundArgs&&... unbound_args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 682 | static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 683 | |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 684 | using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 685 | static constexpr bool is_weak_call = |
| 686 | IsWeakMethod<is_method, |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 687 | std::tuple_element_t<indices, DecayedArgsTuple>...>(); |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 688 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 689 | return InvokeHelper<is_weak_call, R>::MakeItSo( |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 690 | std::forward<Functor>(functor), |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 691 | Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 692 | std::forward<UnboundArgs>(unbound_args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 693 | } |
| 694 | }; |
| 695 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 696 | // Extracts necessary type info from Functor and BoundArgs. |
| 697 | // Used to implement MakeUnboundRunType, BindOnce and BindRepeating. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 698 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 699 | struct BindTypeHelper { |
| 700 | static constexpr size_t num_bounds = sizeof...(BoundArgs); |
| 701 | using FunctorTraits = MakeFunctorTraits<Functor>; |
| 702 | |
| 703 | // Example: |
| 704 | // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs |
| 705 | // is a template pack of `Foo*` and `int16_t`: |
| 706 | // - RunType is `double(Foo*, int, const std::string&)`, |
| 707 | // - ReturnType is `double`, |
| 708 | // - RunParamsList is `TypeList<Foo*, int, const std::string&>`, |
| 709 | // - BoundParamsList is `TypeList<Foo*, int>`, |
| 710 | // - UnboundParamsList is `TypeList<const std::string&>`, |
| 711 | // - BoundArgsList is `TypeList<Foo*, int16_t>`, |
| 712 | // - UnboundRunType is `double(const std::string&)`. |
| 713 | using RunType = typename FunctorTraits::RunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 714 | using ReturnType = ExtractReturnType<RunType>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 715 | |
| 716 | using RunParamsList = ExtractArgs<RunType>; |
| 717 | using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>; |
| 718 | using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>; |
| 719 | |
| 720 | using BoundArgsList = TypeList<BoundArgs...>; |
| 721 | |
| 722 | using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 723 | }; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 724 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 725 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 726 | std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 727 | const Functor& functor) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 728 | return !functor; |
| 729 | } |
| 730 | |
| 731 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 732 | std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 733 | const Functor&) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 734 | return false; |
| 735 | } |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 736 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 737 | // Used by ApplyCancellationTraits below. |
| 738 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 739 | bool ApplyCancellationTraitsImpl(const Functor& functor, |
| 740 | const BoundArgsTuple& bound_args, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 741 | std::index_sequence<indices...>) { |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 742 | return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled( |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 743 | functor, std::get<indices>(bound_args)...); |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 744 | } |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 745 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 746 | // Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns |
| 747 | // true if the callback |base| represents is canceled. |
| 748 | template <typename BindStateType> |
| 749 | bool ApplyCancellationTraits(const BindStateBase* base) { |
| 750 | const BindStateType* storage = static_cast<const BindStateType*>(base); |
| 751 | static constexpr size_t num_bound_args = |
| 752 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 753 | return ApplyCancellationTraitsImpl( |
| 754 | storage->functor_, storage->bound_args_, |
| 755 | std::make_index_sequence<num_bound_args>()); |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 756 | }; |
| 757 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 758 | // BindState<> |
| 759 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 760 | // This stores all the state passed into Bind(). |
| 761 | template <typename Functor, typename... BoundArgs> |
| 762 | struct BindState final : BindStateBase { |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 763 | using IsCancellable = std::integral_constant< |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 764 | bool, |
| 765 | CallbackCancellationTraits<Functor, |
| 766 | std::tuple<BoundArgs...>>::is_cancellable>; |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 767 | |
tzik | bfe6612 | 2016-07-08 14:14:01 | [diff] [blame] | 768 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
tzik | 1886c27 | 2016-09-08 05:45:38 | [diff] [blame] | 769 | explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 770 | ForwardFunctor&& functor, |
| 771 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 772 | // IsCancellable is std::false_type if |
| 773 | // CallbackCancellationTraits<>::IsCancelled returns always false. |
| 774 | // Otherwise, it's std::true_type. |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 775 | : BindState(IsCancellable{}, |
| 776 | invoke_func, |
| 777 | std::forward<ForwardFunctor>(functor), |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 778 | std::forward<ForwardBoundArgs>(bound_args)...) {} |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 779 | |
| 780 | Functor functor_; |
| 781 | std::tuple<BoundArgs...> bound_args_; |
| 782 | |
| 783 | private: |
| 784 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 785 | explicit BindState(std::true_type, |
| 786 | BindStateBase::InvokeFuncStorage invoke_func, |
| 787 | ForwardFunctor&& functor, |
| 788 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 789 | : BindStateBase(invoke_func, |
| 790 | &Destroy, |
| 791 | &ApplyCancellationTraits<BindState>), |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 792 | functor_(std::forward<ForwardFunctor>(functor)), |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 793 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 794 | DCHECK(!IsNull(functor_)); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 795 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 796 | |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 797 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 798 | explicit BindState(std::false_type, |
| 799 | BindStateBase::InvokeFuncStorage invoke_func, |
| 800 | ForwardFunctor&& functor, |
| 801 | ForwardBoundArgs&&... bound_args) |
| 802 | : BindStateBase(invoke_func, &Destroy), |
| 803 | functor_(std::forward<ForwardFunctor>(functor)), |
| 804 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 805 | DCHECK(!IsNull(functor_)); |
| 806 | } |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 807 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 808 | ~BindState() = default; |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 809 | |
tzik | 30e0c31 | 2016-09-21 08:06:54 | [diff] [blame] | 810 | static void Destroy(const BindStateBase* self) { |
| 811 | delete static_cast<const BindState*>(self); |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 812 | } |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 813 | }; |
| 814 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 815 | // Used to implement MakeBindStateType. |
| 816 | template <bool is_method, typename Functor, typename... BoundArgs> |
| 817 | struct MakeBindStateTypeImpl; |
| 818 | |
| 819 | template <typename Functor, typename... BoundArgs> |
| 820 | struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 821 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 822 | "A parameter is a refcounted type and needs scoped_refptr."); |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 823 | using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 824 | }; |
| 825 | |
| 826 | template <typename Functor> |
| 827 | struct MakeBindStateTypeImpl<true, Functor> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 828 | using Type = BindState<std::decay_t<Functor>>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 829 | }; |
| 830 | |
| 831 | template <typename Functor, typename Receiver, typename... BoundArgs> |
| 832 | struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 833 | private: |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 834 | using DecayedReceiver = std::decay_t<Receiver>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 835 | |
tzik | 4625ac61 | 2018-02-28 09:43:55 | [diff] [blame] | 836 | static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value, |
| 837 | "First bound argument to a method cannot be an array."); |
| 838 | static_assert( |
| 839 | !std::is_pointer<DecayedReceiver>::value || |
| 840 | IsRefCountedType<std::remove_pointer_t<DecayedReceiver>>::value, |
| 841 | "Receivers may not be raw pointers. If using a raw pointer here is safe" |
| 842 | " and has no lifetime concerns, use base::Unretained() and document why" |
| 843 | " it's safe."); |
| 844 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
| 845 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 846 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 847 | public: |
| 848 | using Type = BindState< |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 849 | std::decay_t<Functor>, |
| 850 | std::conditional_t<std::is_pointer<DecayedReceiver>::value, |
| 851 | scoped_refptr<std::remove_pointer_t<DecayedReceiver>>, |
| 852 | DecayedReceiver>, |
| 853 | std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 857 | using MakeBindStateType = |
| 858 | typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method, |
| 859 | Functor, |
| 860 | BoundArgs...>::Type; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 861 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 862 | } // namespace internal |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 863 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 864 | // An injection point to control |this| pointer behavior on a method invocation. |
| 865 | // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 866 | // method, base::Bind cancels the method invocation if the receiver is tested as |
| 867 | // false. |
| 868 | // E.g. Foo::bar() is not called: |
| 869 | // struct Foo : base::SupportsWeakPtr<Foo> { |
| 870 | // void bar() {} |
| 871 | // }; |
| 872 | // |
| 873 | // WeakPtr<Foo> oo = nullptr; |
| 874 | // base::Bind(&Foo::bar, oo).Run(); |
| 875 | template <typename T> |
| 876 | struct IsWeakReceiver : std::false_type {}; |
| 877 | |
| 878 | template <typename T> |
| 879 | struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
| 880 | |
| 881 | template <typename T> |
| 882 | struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 883 | |
| 884 | // An injection point to control how bound objects passed to the target |
| 885 | // function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right |
| 886 | // before the target function is invoked. |
| 887 | template <typename> |
| 888 | struct BindUnwrapTraits { |
| 889 | template <typename T> |
| 890 | static T&& Unwrap(T&& o) { |
| 891 | return std::forward<T>(o); |
| 892 | } |
| 893 | }; |
| 894 | |
| 895 | template <typename T> |
| 896 | struct BindUnwrapTraits<internal::UnretainedWrapper<T>> { |
| 897 | static T* Unwrap(const internal::UnretainedWrapper<T>& o) { return o.get(); } |
| 898 | }; |
| 899 | |
| 900 | template <typename T> |
| 901 | struct BindUnwrapTraits<internal::ConstRefWrapper<T>> { |
| 902 | static const T& Unwrap(const internal::ConstRefWrapper<T>& o) { |
| 903 | return o.get(); |
| 904 | } |
| 905 | }; |
| 906 | |
| 907 | template <typename T> |
| 908 | struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> { |
| 909 | static T* Unwrap(const internal::RetainedRefWrapper<T>& o) { return o.get(); } |
| 910 | }; |
| 911 | |
| 912 | template <typename T> |
| 913 | struct BindUnwrapTraits<internal::OwnedWrapper<T>> { |
| 914 | static T* Unwrap(const internal::OwnedWrapper<T>& o) { return o.get(); } |
| 915 | }; |
| 916 | |
| 917 | template <typename T> |
| 918 | struct BindUnwrapTraits<internal::PassedWrapper<T>> { |
| 919 | static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); } |
| 920 | }; |
| 921 | |
tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame^] | 922 | #if defined(OS_WIN) |
| 923 | template <typename T> |
| 924 | struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> { |
| 925 | static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); } |
| 926 | }; |
| 927 | #endif |
| 928 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 929 | // CallbackCancellationTraits allows customization of Callback's cancellation |
| 930 | // semantics. By default, callbacks are not cancellable. A specialization should |
| 931 | // set is_cancellable = true and implement an IsCancelled() that returns if the |
| 932 | // callback should be cancelled. |
| 933 | template <typename Functor, typename BoundArgsTuple, typename SFINAE> |
| 934 | struct CallbackCancellationTraits { |
| 935 | static constexpr bool is_cancellable = false; |
| 936 | }; |
| 937 | |
| 938 | // Specialization for method bound to weak pointer receiver. |
| 939 | template <typename Functor, typename... BoundArgs> |
| 940 | struct CallbackCancellationTraits< |
| 941 | Functor, |
| 942 | std::tuple<BoundArgs...>, |
| 943 | std::enable_if_t< |
| 944 | internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method, |
| 945 | BoundArgs...>::value>> { |
| 946 | static constexpr bool is_cancellable = true; |
| 947 | |
| 948 | template <typename Receiver, typename... Args> |
| 949 | static bool IsCancelled(const Functor&, |
| 950 | const Receiver& receiver, |
| 951 | const Args&...) { |
| 952 | return !receiver; |
| 953 | } |
| 954 | }; |
| 955 | |
| 956 | // Specialization for a nested bind. |
| 957 | template <typename Signature, typename... BoundArgs> |
| 958 | struct CallbackCancellationTraits<OnceCallback<Signature>, |
| 959 | std::tuple<BoundArgs...>> { |
| 960 | static constexpr bool is_cancellable = true; |
| 961 | |
| 962 | template <typename Functor> |
| 963 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 964 | return functor.IsCancelled(); |
| 965 | } |
| 966 | }; |
| 967 | |
| 968 | template <typename Signature, typename... BoundArgs> |
| 969 | struct CallbackCancellationTraits<RepeatingCallback<Signature>, |
| 970 | std::tuple<BoundArgs...>> { |
| 971 | static constexpr bool is_cancellable = true; |
| 972 | |
| 973 | template <typename Functor> |
| 974 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 975 | return functor.IsCancelled(); |
| 976 | } |
| 977 | }; |
| 978 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 979 | // Returns a RunType of bound functor. |
| 980 | // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 981 | template <typename Functor, typename... BoundArgs> |
| 982 | using MakeUnboundRunType = |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 983 | typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 984 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 985 | } // namespace base |
| 986 | |
| 987 | #endif // BASE_BIND_INTERNAL_H_ |