Chromium Code Reviews| 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 // 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 Loading... | |
| 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 Loading... | |
| 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 // An injection point to support Callback::IsCancelled in a specific form of | |
| 528 // base::Bind. | |
|
dcheng
2016/11/10 07:36:23
Nit: I'd make this comment mostly descriptive abou
alex clarke (OOO till 29th)
2016/11/10 10:02:21
+1
tzik
2016/11/15 11:40:35
Done.
| |
| 529 // Specialize CallbackCancellationTraits to support Callback::IsCancelled, set | |
| 530 // is_cancellable to true in the specialization, and implement Run() method. | |
| 531 // CallbackCancellationTraits<>::Run() should return true if the resulting | |
| 532 // callback is cancelled. | |
| 533 template <typename Functor, typename BoundArgsTuple, typename SFINAE = void> | |
| 534 struct CallbackCancellationTraits { | |
| 535 static constexpr bool is_cancellable = false; | |
| 536 }; | |
| 537 | |
| 538 // Specialization for method bound to weak pointer receiver. | |
| 539 template <typename Functor, typename... BoundArgs> | |
| 540 struct CallbackCancellationTraits< | |
| 541 Functor, | |
| 542 std::tuple<BoundArgs...>, | |
| 543 typename std::enable_if< | |
| 544 internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method, | |
| 545 BoundArgs...>::value>::type> { | |
| 546 static constexpr bool is_cancellable = true; | |
| 547 | |
| 548 template <typename Receiver, typename... Args> | |
| 549 static bool IsCancelled(const Functor&, | |
| 550 const Receiver& receiver, | |
| 551 const Args&...) { | |
| 552 return !receiver; | |
| 553 } | |
| 554 }; | |
| 555 | |
| 556 // Specialization for a nested bind. | |
| 557 template <typename Signature, | |
| 558 typename... BoundArgs, | |
| 559 internal::CopyMode copy_mode, | |
| 560 internal::RepeatMode repeat_mode> | |
| 561 struct CallbackCancellationTraits<Callback<Signature, copy_mode, repeat_mode>, | |
| 562 std::tuple<BoundArgs...>> { | |
| 563 static constexpr bool is_cancellable = true; | |
| 564 | |
| 565 template <typename Functor> | |
| 566 static bool IsCancelled(const Functor& functor, const BoundArgs&...) { | |
| 567 return functor.IsCancelled(); | |
| 568 } | |
| 569 }; | |
| 570 | |
| 524 } // namespace base | 571 } // namespace base |
| 525 | 572 |
| 526 #endif // BASE_BIND_HELPERS_H_ | 573 #endif // BASE_BIND_HELPERS_H_ |
| OLD | NEW |