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 16 matching lines...) Expand all Loading... | |
| 27 // Callback::Run() twice on a Callback that was created with a Passed() | 27 // Callback::Run() twice on a Callback that was created with a Passed() |
| 28 // argument will CHECK() because the first invocation would have already | 28 // argument will CHECK() because the first invocation would have already |
| 29 // transferred ownership to the target function. | 29 // transferred ownership to the target function. |
| 30 // | 30 // |
| 31 // ConstRef() allows binding a constant reference to an argument rather | 31 // ConstRef() allows binding a constant reference to an argument rather |
| 32 // than a copy. | 32 // than a copy. |
| 33 // | 33 // |
| 34 // IgnoreResult() is used to adapt a function or Callback with a return type to | 34 // IgnoreResult() is used to adapt a function or Callback with a return type to |
| 35 // one with a void return. This is most useful if you have a function with, | 35 // one with a void return. This is most useful if you have a function with, |
| 36 // say, a pesky ignorable bool return that you want to use with PostTask or | 36 // say, a pesky ignorable bool return that you want to use with PostTask or |
| 37 // something else that expect a Callback with a void return. | 37 // something else that expect a Callback with a void return. |
|
Nico
2016/03/18 14:16:36
explain this new qualifier here...
vmpstr
2016/03/18 18:02:46
Done.
| |
| 38 // | 38 // |
| 39 // EXAMPLE OF Unretained(): | 39 // EXAMPLE OF Unretained(): |
| 40 // | 40 // |
| 41 // class Foo { | 41 // class Foo { |
| 42 // public: | 42 // public: |
| 43 // void func() { cout << "Foo:f" << endl; } | 43 // void func() { cout << "Foo:f" << endl; } |
| 44 // }; | 44 // }; |
| 45 // | 45 // |
| 46 // // In some function somewhere. | 46 // // In some function somewhere. |
| 47 // Foo foo; | 47 // Foo foo; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 // | 123 // |
| 124 // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| | 124 // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| |
| 125 // // no longer owns Foo() and, if reset, would not delete Foo(). | 125 // // no longer owns Foo() and, if reset, would not delete Foo(). |
| 126 // cb.Run(); // Foo() is now transferred to |arg| and deleted. | 126 // cb.Run(); // Foo() is now transferred to |arg| and deleted. |
| 127 // cb.Run(); // This CHECK()s since Foo() already been used once. | 127 // cb.Run(); // This CHECK()s since Foo() already been used once. |
| 128 // | 128 // |
| 129 // Passed() is particularly useful with PostTask() when you are transferring | 129 // Passed() is particularly useful with PostTask() when you are transferring |
| 130 // ownership of an argument into a task, but don't necessarily know if the | 130 // ownership of an argument into a task, but don't necessarily know if the |
| 131 // task will always be executed. This can happen if the task is cancellable | 131 // task will always be executed. This can happen if the task is cancellable |
| 132 // or if it is posted to a TaskRunner. | 132 // or if it is posted to a TaskRunner. |
| 133 // | 133 // |
|
Nico
2016/03/18 14:16:36
...and here
vmpstr
2016/03/18 18:02:46
Done.
| |
| 134 // | 134 // |
| 135 // SIMPLE FUNCTIONS AND UTILITIES. | 135 // SIMPLE FUNCTIONS AND UTILITIES. |
| 136 // | 136 // |
| 137 // DoNothing() - Useful for creating a Closure that does nothing when called. | 137 // DoNothing() - Useful for creating a Closure that does nothing when called. |
| 138 // DeletePointer<T>() - Useful for creating a Closure that will delete a | 138 // DeletePointer<T>() - Useful for creating a Closure that will delete a |
| 139 // pointer when invoked. Only use this when necessary. | 139 // pointer when invoked. Only use this when necessary. |
| 140 // In most cases MessageLoop::DeleteSoon() is a better | 140 // In most cases MessageLoop::DeleteSoon() is a better |
| 141 // fit. | 141 // fit. |
| 142 | 142 |
| 143 #ifndef BASE_BIND_HELPERS_H_ | 143 #ifndef BASE_BIND_HELPERS_H_ |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 template <typename T> | 305 template <typename T> |
| 306 class ConstRefWrapper { | 306 class ConstRefWrapper { |
| 307 public: | 307 public: |
| 308 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} | 308 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
| 309 const T& get() const { return *ptr_; } | 309 const T& get() const { return *ptr_; } |
| 310 private: | 310 private: |
| 311 const T* ptr_; | 311 const T* ptr_; |
| 312 }; | 312 }; |
| 313 | 313 |
| 314 template <typename T> | 314 template <typename T> |
| 315 class RetainedRefWrapper { | |
| 316 public: | |
| 317 explicit RetainedRefWrapper(T* o) : ptr_(o) {} | |
| 318 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} | |
| 319 T* get() const { return ptr_.get(); } | |
| 320 private: | |
| 321 scoped_refptr<T> ptr_; | |
| 322 }; | |
| 323 | |
| 324 template <typename T> | |
| 315 struct IgnoreResultHelper { | 325 struct IgnoreResultHelper { |
| 316 explicit IgnoreResultHelper(T functor) : functor_(functor) {} | 326 explicit IgnoreResultHelper(T functor) : functor_(functor) {} |
| 317 | 327 |
| 318 T functor_; | 328 T functor_; |
| 319 }; | 329 }; |
| 320 | 330 |
| 321 template <typename T> | 331 template <typename T> |
| 322 struct IgnoreResultHelper<Callback<T> > { | 332 struct IgnoreResultHelper<Callback<T> > { |
| 323 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} | 333 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} |
| 324 | 334 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 const T& Unwrap(ConstRefWrapper<T> const_ref) { | 413 const T& Unwrap(ConstRefWrapper<T> const_ref) { |
| 404 return const_ref.get(); | 414 return const_ref.get(); |
| 405 } | 415 } |
| 406 | 416 |
| 407 template <typename T> | 417 template <typename T> |
| 408 T* Unwrap(const scoped_refptr<T>& o) { | 418 T* Unwrap(const scoped_refptr<T>& o) { |
| 409 return o.get(); | 419 return o.get(); |
| 410 } | 420 } |
| 411 | 421 |
| 412 template <typename T> | 422 template <typename T> |
| 423 T* Unwrap(const RetainedRefWrapper<T>& o) { | |
| 424 return o.get(); | |
| 425 } | |
| 426 | |
| 427 template <typename T> | |
| 413 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { | 428 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { |
| 414 return o; | 429 return o; |
| 415 } | 430 } |
| 416 | 431 |
| 417 template <typename T> | 432 template <typename T> |
| 418 T* Unwrap(const OwnedWrapper<T>& o) { | 433 T* Unwrap(const OwnedWrapper<T>& o) { |
| 419 return o.get(); | 434 return o.get(); |
| 420 } | 435 } |
| 421 | 436 |
| 422 template <typename T> | 437 template <typename T> |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 using ExtractArgs = typename ExtractArgsImpl<Signature>::Type; | 553 using ExtractArgs = typename ExtractArgsImpl<Signature>::Type; |
| 539 | 554 |
| 540 } // namespace internal | 555 } // namespace internal |
| 541 | 556 |
| 542 template <typename T> | 557 template <typename T> |
| 543 static inline internal::UnretainedWrapper<T> Unretained(T* o) { | 558 static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
| 544 return internal::UnretainedWrapper<T>(o); | 559 return internal::UnretainedWrapper<T>(o); |
| 545 } | 560 } |
| 546 | 561 |
| 547 template <typename T> | 562 template <typename T> |
| 563 static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) { | |
| 564 return internal::RetainedRefWrapper<T>(o); | |
| 565 } | |
| 566 | |
| 567 template <typename T> | |
| 568 static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) { | |
| 569 return internal::RetainedRefWrapper<T>(std::move(o)); | |
| 570 } | |
| 571 | |
| 572 template <typename T> | |
| 548 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { | 573 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
| 549 return internal::ConstRefWrapper<T>(o); | 574 return internal::ConstRefWrapper<T>(o); |
| 550 } | 575 } |
| 551 | 576 |
| 552 template <typename T> | 577 template <typename T> |
| 553 static inline internal::OwnedWrapper<T> Owned(T* o) { | 578 static inline internal::OwnedWrapper<T> Owned(T* o) { |
| 554 return internal::OwnedWrapper<T>(o); | 579 return internal::OwnedWrapper<T>(o); |
| 555 } | 580 } |
| 556 | 581 |
| 557 // We offer 2 syntaxes for calling Passed(). The first takes an rvalue and | 582 // We offer 2 syntaxes for calling Passed(). The first takes an rvalue and |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 586 BASE_EXPORT void DoNothing(); | 611 BASE_EXPORT void DoNothing(); |
| 587 | 612 |
| 588 template<typename T> | 613 template<typename T> |
| 589 void DeletePointer(T* obj) { | 614 void DeletePointer(T* obj) { |
| 590 delete obj; | 615 delete obj; |
| 591 } | 616 } |
| 592 | 617 |
| 593 } // namespace base | 618 } // namespace base |
| 594 | 619 |
| 595 #endif // BASE_BIND_HELPERS_H_ | 620 #endif // BASE_BIND_HELPERS_H_ |
| OLD | NEW |