| 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 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { | 415 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { |
| 416 static constexpr bool is_cancellable = true; | 416 static constexpr bool is_cancellable = true; |
| 417 static bool Run(const BindStateBase* base) { | 417 static bool Run(const BindStateBase* base) { |
| 418 using Functor = Callback<Signature>; | 418 using Functor = Callback<Signature>; |
| 419 using BindStateType = BindState<Functor, BoundArgs...>; | 419 using BindStateType = BindState<Functor, BoundArgs...>; |
| 420 const BindStateType* bind_state = static_cast<const BindStateType*>(base); | 420 const BindStateType* bind_state = static_cast<const BindStateType*>(base); |
| 421 return bind_state->functor_.IsCancelled(); | 421 return bind_state->functor_.IsCancelled(); |
| 422 } | 422 } |
| 423 }; | 423 }; |
| 424 | 424 |
| 425 // Template helpers to detect using Bind() on a base::Callback without any |
| 426 // additional arguments. In that case, the original base::Callback object should |
| 427 // just be directly used. |
| 428 template <typename Functor, typename... BoundArgs> |
| 429 struct BindingCallbackWithNoArgs { |
| 430 static constexpr bool value = false; |
| 431 }; |
| 432 |
| 433 template <typename Signature, |
| 434 typename... BoundArgs, |
| 435 CopyMode copy_mode, |
| 436 RepeatMode repeat_mode> |
| 437 struct BindingCallbackWithNoArgs<Callback<Signature, copy_mode, repeat_mode>, |
| 438 BoundArgs...> { |
| 439 static constexpr bool value = sizeof...(BoundArgs) == 0; |
| 440 }; |
| 441 |
| 425 // BindState<> | 442 // BindState<> |
| 426 // | 443 // |
| 427 // This stores all the state passed into Bind(). | 444 // This stores all the state passed into Bind(). |
| 428 template <typename Functor, typename... BoundArgs> | 445 template <typename Functor, typename... BoundArgs> |
| 429 struct BindState final : BindStateBase { | 446 struct BindState final : BindStateBase { |
| 430 using IsCancellable = std::integral_constant< | 447 using IsCancellable = std::integral_constant< |
| 431 bool, CancellationChecker<BindState>::is_cancellable>; | 448 bool, CancellationChecker<BindState>::is_cancellable>; |
| 432 | 449 |
| 433 template <typename ForwardFunctor, typename... ForwardBoundArgs> | 450 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 434 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, | 451 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 435 ForwardFunctor&& functor, | 452 ForwardFunctor&& functor, |
| 436 ForwardBoundArgs&&... bound_args) | 453 ForwardBoundArgs&&... bound_args) |
| 437 // IsCancellable is std::false_type if the CancellationChecker<>::Run | 454 // IsCancellable is std::false_type if the CancellationChecker<>::Run |
| 438 // returns always false. Otherwise, it's std::true_type. | 455 // returns always false. Otherwise, it's std::true_type. |
| 439 : BindState(IsCancellable{}, | 456 : BindState(IsCancellable{}, |
| 440 invoke_func, | 457 invoke_func, |
| 441 std::forward<ForwardFunctor>(functor), | 458 std::forward<ForwardFunctor>(functor), |
| 442 std::forward<ForwardBoundArgs>(bound_args)...) {} | 459 std::forward<ForwardBoundArgs>(bound_args)...) { |
| 460 static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value, |
| 461 "Attempting to bind a base::Callback with no additional " |
| 462 "arguments: save a heap allocation and use the original " |
| 463 "base::Callback object"); |
| 464 } |
| 443 | 465 |
| 444 Functor functor_; | 466 Functor functor_; |
| 445 std::tuple<BoundArgs...> bound_args_; | 467 std::tuple<BoundArgs...> bound_args_; |
| 446 | 468 |
| 447 private: | 469 private: |
| 448 template <typename ForwardFunctor, typename... ForwardBoundArgs> | 470 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 449 explicit BindState(std::true_type, | 471 explicit BindState(std::true_type, |
| 450 BindStateBase::InvokeFuncStorage invoke_func, | 472 BindStateBase::InvokeFuncStorage invoke_func, |
| 451 ForwardFunctor&& functor, | 473 ForwardFunctor&& functor, |
| 452 ForwardBoundArgs&&... bound_args) | 474 ForwardBoundArgs&&... bound_args) |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 | 545 |
| 524 // Returns a RunType of bound functor. | 546 // Returns a RunType of bound functor. |
| 525 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). | 547 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 526 template <typename Functor, typename... BoundArgs> | 548 template <typename Functor, typename... BoundArgs> |
| 527 using MakeUnboundRunType = | 549 using MakeUnboundRunType = |
| 528 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; | 550 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; |
| 529 | 551 |
| 530 } // namespace base | 552 } // namespace base |
| 531 | 553 |
| 532 #endif // BASE_BIND_INTERNAL_H_ | 554 #endif // BASE_BIND_INTERNAL_H_ |
| OLD | NEW |