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 10 matching lines...) Expand all Loading... |
21 // Owned() transfers ownership of an object to the Callback resulting from | 21 // Owned() transfers ownership of an object to the Callback resulting from |
22 // bind; the object will be deleted when the Callback is deleted. | 22 // bind; the object will be deleted when the Callback is deleted. |
23 // | 23 // |
24 // Passed() is for transferring movable-but-not-copyable types (eg. scoped_ptr) | 24 // Passed() is for transferring movable-but-not-copyable types (eg. scoped_ptr) |
25 // through a Callback. Logically, this signifies a destructive transfer of | 25 // through a Callback. Logically, this signifies a destructive transfer of |
26 // the state of the argument into the target function. Invoking | 26 // the state of the argument into the target function. Invoking |
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 // RetainedRef() accepts a ref counted object and retains a reference to it. |
| 32 // When the callback is called, the object is passed as a raw pointer. |
| 33 // |
31 // ConstRef() allows binding a constant reference to an argument rather | 34 // ConstRef() allows binding a constant reference to an argument rather |
32 // than a copy. | 35 // than a copy. |
33 // | 36 // |
34 // IgnoreResult() is used to adapt a function or Callback with a return type to | 37 // 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, | 38 // 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 | 39 // 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. | 40 // something else that expect a Callback with a void return. |
38 // | 41 // |
39 // EXAMPLE OF Unretained(): | 42 // EXAMPLE OF Unretained(): |
40 // | 43 // |
(...skipping 23 matching lines...) Expand all Loading... |
64 // foo_callback.Run(); // Prints "1" | 67 // foo_callback.Run(); // Prints "1" |
65 // *n = 2; | 68 // *n = 2; |
66 // foo_callback.Run(); // Prints "2" | 69 // foo_callback.Run(); // Prints "2" |
67 // | 70 // |
68 // foo_callback.Reset(); // |pn| is deleted. Also will happen when | 71 // foo_callback.Reset(); // |pn| is deleted. Also will happen when |
69 // // |foo_callback| goes out of scope. | 72 // // |foo_callback| goes out of scope. |
70 // | 73 // |
71 // Without Owned(), someone would have to know to delete |pn| when the last | 74 // Without Owned(), someone would have to know to delete |pn| when the last |
72 // reference to the Callback is deleted. | 75 // reference to the Callback is deleted. |
73 // | 76 // |
| 77 // EXAMPLE OF RetainedRef(): |
| 78 // |
| 79 // void foo(RefCountedBytes* bytes) {} |
| 80 // |
| 81 // scoped_refptr<RefCountedBytes> bytes = ...; |
| 82 // Closure callback = Bind(&foo, base::RetainedRef(bytes)); |
| 83 // callback.Run(); |
| 84 // |
| 85 // Without RetainedRef, the scoped_refptr would try to implicitly convert to |
| 86 // a raw pointer and fail compilation: |
| 87 // |
| 88 // Closure callback = Bind(&foo, bytes); // ERROR! |
| 89 // |
74 // | 90 // |
75 // EXAMPLE OF ConstRef(): | 91 // EXAMPLE OF ConstRef(): |
76 // | 92 // |
77 // void foo(int arg) { cout << arg << endl } | 93 // void foo(int arg) { cout << arg << endl } |
78 // | 94 // |
79 // int n = 1; | 95 // int n = 1; |
80 // Closure no_ref = Bind(&foo, n); | 96 // Closure no_ref = Bind(&foo, n); |
81 // Closure has_ref = Bind(&foo, ConstRef(n)); | 97 // Closure has_ref = Bind(&foo, ConstRef(n)); |
82 // | 98 // |
83 // no_ref.Run(); // Prints "1" | 99 // no_ref.Run(); // Prints "1" |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 template <typename T> | 321 template <typename T> |
306 class ConstRefWrapper { | 322 class ConstRefWrapper { |
307 public: | 323 public: |
308 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} | 324 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
309 const T& get() const { return *ptr_; } | 325 const T& get() const { return *ptr_; } |
310 private: | 326 private: |
311 const T* ptr_; | 327 const T* ptr_; |
312 }; | 328 }; |
313 | 329 |
314 template <typename T> | 330 template <typename T> |
| 331 class RetainedRefWrapper { |
| 332 public: |
| 333 explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 334 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 335 T* get() const { return ptr_.get(); } |
| 336 private: |
| 337 scoped_refptr<T> ptr_; |
| 338 }; |
| 339 |
| 340 template <typename T> |
315 struct IgnoreResultHelper { | 341 struct IgnoreResultHelper { |
316 explicit IgnoreResultHelper(T functor) : functor_(functor) {} | 342 explicit IgnoreResultHelper(T functor) : functor_(functor) {} |
317 | 343 |
318 T functor_; | 344 T functor_; |
319 }; | 345 }; |
320 | 346 |
321 template <typename T> | 347 template <typename T> |
322 struct IgnoreResultHelper<Callback<T> > { | 348 struct IgnoreResultHelper<Callback<T> > { |
323 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} | 349 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} |
324 | 350 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 const T& Unwrap(ConstRefWrapper<T> const_ref) { | 429 const T& Unwrap(ConstRefWrapper<T> const_ref) { |
404 return const_ref.get(); | 430 return const_ref.get(); |
405 } | 431 } |
406 | 432 |
407 template <typename T> | 433 template <typename T> |
408 T* Unwrap(const scoped_refptr<T>& o) { | 434 T* Unwrap(const scoped_refptr<T>& o) { |
409 return o.get(); | 435 return o.get(); |
410 } | 436 } |
411 | 437 |
412 template <typename T> | 438 template <typename T> |
| 439 T* Unwrap(const RetainedRefWrapper<T>& o) { |
| 440 return o.get(); |
| 441 } |
| 442 |
| 443 template <typename T> |
413 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { | 444 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { |
414 return o; | 445 return o; |
415 } | 446 } |
416 | 447 |
417 template <typename T> | 448 template <typename T> |
418 T* Unwrap(const OwnedWrapper<T>& o) { | 449 T* Unwrap(const OwnedWrapper<T>& o) { |
419 return o.get(); | 450 return o.get(); |
420 } | 451 } |
421 | 452 |
422 template <typename T> | 453 template <typename T> |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 using ExtractArgs = typename ExtractArgsImpl<Signature>::Type; | 569 using ExtractArgs = typename ExtractArgsImpl<Signature>::Type; |
539 | 570 |
540 } // namespace internal | 571 } // namespace internal |
541 | 572 |
542 template <typename T> | 573 template <typename T> |
543 static inline internal::UnretainedWrapper<T> Unretained(T* o) { | 574 static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
544 return internal::UnretainedWrapper<T>(o); | 575 return internal::UnretainedWrapper<T>(o); |
545 } | 576 } |
546 | 577 |
547 template <typename T> | 578 template <typename T> |
| 579 static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) { |
| 580 return internal::RetainedRefWrapper<T>(o); |
| 581 } |
| 582 |
| 583 template <typename T> |
| 584 static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) { |
| 585 return internal::RetainedRefWrapper<T>(std::move(o)); |
| 586 } |
| 587 |
| 588 template <typename T> |
548 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { | 589 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
549 return internal::ConstRefWrapper<T>(o); | 590 return internal::ConstRefWrapper<T>(o); |
550 } | 591 } |
551 | 592 |
552 template <typename T> | 593 template <typename T> |
553 static inline internal::OwnedWrapper<T> Owned(T* o) { | 594 static inline internal::OwnedWrapper<T> Owned(T* o) { |
554 return internal::OwnedWrapper<T>(o); | 595 return internal::OwnedWrapper<T>(o); |
555 } | 596 } |
556 | 597 |
557 // We offer 2 syntaxes for calling Passed(). The first takes an rvalue and | 598 // 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(); | 627 BASE_EXPORT void DoNothing(); |
587 | 628 |
588 template<typename T> | 629 template<typename T> |
589 void DeletePointer(T* obj) { | 630 void DeletePointer(T* obj) { |
590 delete obj; | 631 delete obj; |
591 } | 632 } |
592 | 633 |
593 } // namespace base | 634 } // namespace base |
594 | 635 |
595 #endif // BASE_BIND_HELPERS_H_ | 636 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |