OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_BIND_INTERNAL_H_ | 5 #ifndef BASE_BIND_INTERNAL_H_ |
6 #define BASE_BIND_INTERNAL_H_ | 6 #define BASE_BIND_INTERNAL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <tuple> | 10 #include <tuple> |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type | 385 typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type |
386 IsNull(const Functor&) { | 386 IsNull(const Functor&) { |
387 return false; | 387 return false; |
388 } | 388 } |
389 | 389 |
390 template <typename Functor, typename... BoundArgs> | 390 template <typename Functor, typename... BoundArgs> |
391 struct BindState; | 391 struct BindState; |
392 | 392 |
393 template <typename BindStateType, typename SFINAE = void> | 393 template <typename BindStateType, typename SFINAE = void> |
394 struct CancellationChecker { | 394 struct CancellationChecker { |
| 395 static constexpr bool is_cancellable = false; |
395 static bool Run(const BindStateBase*) { | 396 static bool Run(const BindStateBase*) { |
396 return false; | 397 return false; |
397 } | 398 } |
398 }; | 399 }; |
399 | 400 |
400 template <typename Functor, typename... BoundArgs> | 401 template <typename Functor, typename... BoundArgs> |
401 struct CancellationChecker< | 402 struct CancellationChecker< |
402 BindState<Functor, BoundArgs...>, | 403 BindState<Functor, BoundArgs...>, |
403 typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method, | 404 typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method, |
404 BoundArgs...>::value>::type> { | 405 BoundArgs...>::value>::type> { |
| 406 static constexpr bool is_cancellable = true; |
405 static bool Run(const BindStateBase* base) { | 407 static bool Run(const BindStateBase* base) { |
406 using BindStateType = BindState<Functor, BoundArgs...>; | 408 using BindStateType = BindState<Functor, BoundArgs...>; |
407 const BindStateType* bind_state = static_cast<const BindStateType*>(base); | 409 const BindStateType* bind_state = static_cast<const BindStateType*>(base); |
408 return !base::get<0>(bind_state->bound_args_); | 410 return !base::get<0>(bind_state->bound_args_); |
409 } | 411 } |
410 }; | 412 }; |
411 | 413 |
412 template <typename Signature, typename... BoundArgs> | 414 template <typename Signature, typename... BoundArgs> |
413 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { | 415 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { |
| 416 static constexpr bool is_cancellable = true; |
414 static bool Run(const BindStateBase* base) { | 417 static bool Run(const BindStateBase* base) { |
415 using Functor = Callback<Signature>; | 418 using Functor = Callback<Signature>; |
416 using BindStateType = BindState<Functor, BoundArgs...>; | 419 using BindStateType = BindState<Functor, BoundArgs...>; |
417 const BindStateType* bind_state = static_cast<const BindStateType*>(base); | 420 const BindStateType* bind_state = static_cast<const BindStateType*>(base); |
418 return bind_state->functor_.IsCancelled(); | 421 return bind_state->functor_.IsCancelled(); |
419 } | 422 } |
420 }; | 423 }; |
421 | 424 |
422 // BindState<> | 425 // BindState<> |
423 // | 426 // |
424 // This stores all the state passed into Bind(). | 427 // This stores all the state passed into Bind(). |
425 template <typename Functor, typename... BoundArgs> | 428 template <typename Functor, typename... BoundArgs> |
426 struct BindState final : BindStateBase { | 429 struct BindState final : BindStateBase { |
| 430 using IsCancellable = std::integral_constant< |
| 431 bool, CancellationChecker<BindState>::is_cancellable>; |
| 432 |
427 template <typename ForwardFunctor, typename... ForwardBoundArgs> | 433 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
428 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, | 434 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
429 ForwardFunctor&& functor, | 435 ForwardFunctor&& functor, |
430 ForwardBoundArgs&&... bound_args) | 436 ForwardBoundArgs&&... bound_args) |
| 437 // IsCancellable is std::false_type if the CancellationChecker<>::Run |
| 438 // returns always false. Otherwise, it's std::true_type. |
| 439 : BindState(IsCancellable{}, |
| 440 invoke_func, |
| 441 std::forward<ForwardFunctor>(functor), |
| 442 std::forward<ForwardBoundArgs>(bound_args)...) {} |
| 443 |
| 444 Functor functor_; |
| 445 std::tuple<BoundArgs...> bound_args_; |
| 446 |
| 447 private: |
| 448 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 449 explicit BindState(std::true_type, |
| 450 BindStateBase::InvokeFuncStorage invoke_func, |
| 451 ForwardFunctor&& functor, |
| 452 ForwardBoundArgs&&... bound_args) |
431 : BindStateBase(invoke_func, &Destroy, | 453 : BindStateBase(invoke_func, &Destroy, |
432 &CancellationChecker<BindState>::Run), | 454 &CancellationChecker<BindState>::Run), |
433 functor_(std::forward<ForwardFunctor>(functor)), | 455 functor_(std::forward<ForwardFunctor>(functor)), |
434 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { | 456 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
435 DCHECK(!IsNull(functor_)); | 457 DCHECK(!IsNull(functor_)); |
436 } | 458 } |
437 | 459 |
438 Functor functor_; | 460 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
439 std::tuple<BoundArgs...> bound_args_; | 461 explicit BindState(std::false_type, |
| 462 BindStateBase::InvokeFuncStorage invoke_func, |
| 463 ForwardFunctor&& functor, |
| 464 ForwardBoundArgs&&... bound_args) |
| 465 : BindStateBase(invoke_func, &Destroy), |
| 466 functor_(std::forward<ForwardFunctor>(functor)), |
| 467 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 468 DCHECK(!IsNull(functor_)); |
| 469 } |
440 | 470 |
441 private: | |
442 ~BindState() {} | 471 ~BindState() {} |
443 | 472 |
444 static void Destroy(BindStateBase* self) { | 473 static void Destroy(BindStateBase* self) { |
445 delete static_cast<BindState*>(self); | 474 delete static_cast<BindState*>(self); |
446 } | 475 } |
447 }; | 476 }; |
448 | 477 |
449 // Used to implement MakeBindStateType. | 478 // Used to implement MakeBindStateType. |
450 template <bool is_method, typename Functor, typename... BoundArgs> | 479 template <bool is_method, typename Functor, typename... BoundArgs> |
451 struct MakeBindStateTypeImpl; | 480 struct MakeBindStateTypeImpl; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 | 523 |
495 // Returns a RunType of bound functor. | 524 // Returns a RunType of bound functor. |
496 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). | 525 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
497 template <typename Functor, typename... BoundArgs> | 526 template <typename Functor, typename... BoundArgs> |
498 using MakeUnboundRunType = | 527 using MakeUnboundRunType = |
499 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; | 528 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; |
500 | 529 |
501 } // namespace base | 530 } // namespace base |
502 | 531 |
503 #endif // BASE_BIND_INTERNAL_H_ | 532 #endif // BASE_BIND_INTERNAL_H_ |
OLD | NEW |