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