Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Unified Diff: base/bind_internal.h

Issue 2487493004: Support external task cancellation mechanisms in base::Callback::IsCancelled (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/bind_internal.h
diff --git a/base/bind_internal.h b/base/bind_internal.h
index 88e764547f81677ac6c3f72b8a026642c67681d2..18fbfc52d80f78a78ce7b44752a72a237faef4ac 100644
--- a/base/bind_internal.h
+++ b/base/bind_internal.h
@@ -130,7 +130,7 @@ struct ForceVoidReturn<R(Args...)> {
// FunctorTraits<>
//
// See description at top of file.
-template <typename Functor, typename SFINAE = void>
+template <typename Functor, typename SFINAE>
struct FunctorTraits;
// For a callable type that is convertible to the corresponding function type.
@@ -387,43 +387,24 @@ IsNull(const Functor&) {
return false;
}
-template <typename Functor, typename... BoundArgs>
-struct BindState;
-
-template <typename BindStateType, typename SFINAE = void>
-struct CancellationChecker {
- static constexpr bool is_cancellable = false;
- static bool Run(const BindStateBase*) {
- return false;
- }
-};
-
-template <typename Functor, typename... BoundArgs>
-struct CancellationChecker<
- BindState<Functor, BoundArgs...>,
- typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method,
- BoundArgs...>::value>::type> {
- static constexpr bool is_cancellable = true;
- static bool Run(const BindStateBase* base) {
- using BindStateType = BindState<Functor, BoundArgs...>;
- const BindStateType* bind_state = static_cast<const BindStateType*>(base);
- return !base::get<0>(bind_state->bound_args_);
- }
-};
+// Used by ApplyCancellationChecker below.
+template <typename Functor, typename BoundArgsTuple, size_t... indices>
+bool ApplyCancellationCheckerImpl(const Functor& functor,
+ const BoundArgsTuple& bound_args,
+ IndexSequence<indices...>) {
+ return BindCancellationChecker<Functor, BoundArgsTuple>::Run(
+ functor, base::get<indices>(bound_args)...);
+}
-template <typename Signature,
- typename... BoundArgs,
- CopyMode copy_mode,
- RepeatMode repeat_mode>
-struct CancellationChecker<
- BindState<Callback<Signature, copy_mode, repeat_mode>, BoundArgs...>> {
- static constexpr bool is_cancellable = true;
- static bool Run(const BindStateBase* base) {
- using Functor = Callback<Signature, copy_mode, repeat_mode>;
- using BindStateType = BindState<Functor, BoundArgs...>;
- const BindStateType* bind_state = static_cast<const BindStateType*>(base);
- return bind_state->functor_.IsCancelled();
- }
+// Relays |base| to corresponding BindCancellationChecker<>::Run(). Returns true
+// if the callback |base| represents is canceled.
+template <typename BindStateType>
+bool ApplyCancellationChecker(const BindStateBase* base) {
+ const BindStateType* storage = static_cast<const BindStateType*>(base);
+ static constexpr size_t num_bound_args =
+ std::tuple_size<decltype(storage->bound_args_)>::value;
+ return ApplyCancellationCheckerImpl(storage->functor_, storage->bound_args_,
+ MakeIndexSequence<num_bound_args>());
};
// Template helpers to detect using Bind() on a base::Callback without any
@@ -449,13 +430,15 @@ struct BindingCallbackWithNoArgs<Callback<Signature, copy_mode, repeat_mode>,
template <typename Functor, typename... BoundArgs>
struct BindState final : BindStateBase {
using IsCancellable = std::integral_constant<
- bool, CancellationChecker<BindState>::is_cancellable>;
+ bool,
+ BindCancellationChecker<Functor,
+ std::tuple<BoundArgs...>>::is_cancellable>;
template <typename ForwardFunctor, typename... ForwardBoundArgs>
explicit BindState(BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
- // IsCancellable is std::false_type if the CancellationChecker<>::Run
+ // IsCancellable is std::false_type if the BindCancellationChecker<>::Run
// returns always false. Otherwise, it's std::true_type.
: BindState(IsCancellable{},
invoke_func,
@@ -476,8 +459,9 @@ struct BindState final : BindStateBase {
BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
- : BindStateBase(invoke_func, &Destroy,
- &CancellationChecker<BindState>::Run),
+ : BindStateBase(invoke_func,
+ &Destroy,
+ &ApplyCancellationChecker<BindState>),
functor_(std::forward<ForwardFunctor>(functor)),
bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
DCHECK(!IsNull(functor_));

Powered by Google App Engine
This is Rietveld 408576698