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

Side by Side Diff: base/bind_helpers.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 unified diff | Download patch
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This defines a set of argument wrappers and related factory methods that 5 // This defines a set of argument wrappers and related factory methods that
6 // can be used specify the refcounting and reference semantics of arguments 6 // can be used specify the refcounting and reference semantics of arguments
7 // that are bound by the Bind() function in base/bind.h. 7 // that are bound by the Bind() function in base/bind.h.
8 // 8 //
9 // It also defines a set of simple functions and utilities that people want 9 // It also defines a set of simple functions and utilities that people want
10 // when using Callback<> and Bind(). 10 // when using Callback<> and Bind().
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 namespace base { 172 namespace base {
173 173
174 template <typename T> 174 template <typename T>
175 struct IsWeakReceiver; 175 struct IsWeakReceiver;
176 176
177 template <typename> 177 template <typename>
178 struct BindUnwrapTraits; 178 struct BindUnwrapTraits;
179 179
180 namespace internal { 180 namespace internal {
181 181
182 template <typename Functor, typename SFINAE = void>
183 struct FunctorTraits;
184
182 template <typename T> 185 template <typename T>
183 class UnretainedWrapper { 186 class UnretainedWrapper {
184 public: 187 public:
185 explicit UnretainedWrapper(T* o) : ptr_(o) {} 188 explicit UnretainedWrapper(T* o) : ptr_(o) {}
186 T* get() const { return ptr_; } 189 T* get() const { return ptr_; }
187 private: 190 private:
188 T* ptr_; 191 T* ptr_;
189 }; 192 };
190 193
191 template <typename T> 194 template <typename T>
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 } 517 }
515 }; 518 };
516 519
517 template <typename T> 520 template <typename T>
518 struct BindUnwrapTraits<internal::PassedWrapper<T>> { 521 struct BindUnwrapTraits<internal::PassedWrapper<T>> {
519 static T Unwrap(const internal::PassedWrapper<T>& o) { 522 static T Unwrap(const internal::PassedWrapper<T>& o) {
520 return o.Take(); 523 return o.Take();
521 } 524 }
522 }; 525 };
523 526
527 // CallbackCancellationTraits allows customization of Callback's cancellation
528 // semantics. By default, callbacks are not cancellable. A specialization should
529 // set is_cancellable = true and implement an IsCancelled() that returns if the
530 // callback should be cancelled.
531 template <typename Functor, typename BoundArgsTuple, typename SFINAE = void>
532 struct CallbackCancellationTraits {
533 static constexpr bool is_cancellable = false;
534 };
535
536 // Specialization for method bound to weak pointer receiver.
537 template <typename Functor, typename... BoundArgs>
538 struct CallbackCancellationTraits<
539 Functor,
540 std::tuple<BoundArgs...>,
541 typename std::enable_if<
542 internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method,
543 BoundArgs...>::value>::type> {
544 static constexpr bool is_cancellable = true;
545
546 template <typename Receiver, typename... Args>
547 static bool IsCancelled(const Functor&,
548 const Receiver& receiver,
549 const Args&...) {
550 return !receiver;
551 }
552 };
553
554 // Specialization for a nested bind.
555 template <typename Signature,
556 typename... BoundArgs,
557 internal::CopyMode copy_mode,
558 internal::RepeatMode repeat_mode>
559 struct CallbackCancellationTraits<Callback<Signature, copy_mode, repeat_mode>,
560 std::tuple<BoundArgs...>> {
561 static constexpr bool is_cancellable = true;
562
563 template <typename Functor>
564 static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
565 return functor.IsCancelled();
566 }
567 };
568
524 } // namespace base 569 } // namespace base
525 570
526 #endif // BASE_BIND_HELPERS_H_ 571 #endif // BASE_BIND_HELPERS_H_
OLDNEW
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698