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 // The public functions are base::Unretained(), base::Owned(), bass::Passed(), | 9 // It also defines a set of simple functions and utilities that people want |
| 10 // when using Callback<> and Bind(). |
| 11 // |
| 12 // |
| 13 // ARGUMENT BINDING WRAPPERS |
| 14 // |
| 15 // The wrapper functions are base::Unretained(), base::Owned(), bass::Passed(), |
10 // base::ConstRef(), and base::IgnoreResult(). | 16 // base::ConstRef(), and base::IgnoreResult(). |
11 // | 17 // |
12 // Unretained() allows Bind() to bind a non-refcounted class, and to disable | 18 // Unretained() allows Bind() to bind a non-refcounted class, and to disable |
13 // refcounting on arguments that are refcounted objects. | 19 // refcounting on arguments that are refcounted objects. |
14 // | 20 // |
15 // Owned() transfers ownership of an object to the Callback resulting from | 21 // Owned() transfers ownership of an object to the Callback resulting from |
16 // bind; the object will be deleted when the Callback is deleted. | 22 // bind; the object will be deleted when the Callback is deleted. |
17 // | 23 // |
18 // 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) |
19 // through a Callback. Logically, this signifies a destructive transfer of | 25 // through a Callback. Logically, this signifies a destructive transfer of |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // | 123 // |
118 // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| | 124 // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| |
119 // // no longer owns Foo() and, if reset, would not delete Foo(). | 125 // // no longer owns Foo() and, if reset, would not delete Foo(). |
120 // cb.Run(); // Foo() is now transferred to |arg| and deleted. | 126 // cb.Run(); // Foo() is now transferred to |arg| and deleted. |
121 // cb.Run(); // This CHECK()s since Foo() already been used once. | 127 // cb.Run(); // This CHECK()s since Foo() already been used once. |
122 // | 128 // |
123 // Passed() is particularly useful with PostTask() when you are transferring | 129 // Passed() is particularly useful with PostTask() when you are transferring |
124 // 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 |
125 // 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 |
126 // or if it is posted to a MessageLoopProxy. | 132 // or if it is posted to a MessageLoopProxy. |
| 133 // |
| 134 // |
| 135 // SIMPLE FUNCTIONS AND UTILITIES. |
| 136 // |
| 137 // DoNothing() - Useful for creating a Closure that does nothing when called. |
| 138 // DeletePointer<T>() - Useful for creating a Closure that will delete a |
| 139 // pointer when invoked. Only use this when necessary. |
| 140 // In most cases MessageLoop::DeleteSoon() is a better |
| 141 // fit. |
| 142 // ScopedClosureRunner - Scoper object that runs the wrapped closure when it |
| 143 // goes out of scope. It's conceptually similar to |
| 144 // scoped_ptr<> but calls Run() instead of deleting |
| 145 // the pointer. |
127 | 146 |
128 #ifndef BASE_BIND_HELPERS_H_ | 147 #ifndef BASE_BIND_HELPERS_H_ |
129 #define BASE_BIND_HELPERS_H_ | 148 #define BASE_BIND_HELPERS_H_ |
130 #pragma once | 149 #pragma once |
131 | 150 |
132 #include "base/basictypes.h" | 151 #include "base/basictypes.h" |
133 #include "base/callback.h" | 152 #include "base/callback.h" |
134 #include "base/memory/weak_ptr.h" | 153 #include "base/memory/weak_ptr.h" |
135 #include "base/template_util.h" | 154 #include "base/template_util.h" |
136 | 155 |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { | 529 static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { |
511 return internal::IgnoreResultHelper<T>(data); | 530 return internal::IgnoreResultHelper<T>(data); |
512 } | 531 } |
513 | 532 |
514 template <typename T> | 533 template <typename T> |
515 static inline internal::IgnoreResultHelper<Callback<T> > | 534 static inline internal::IgnoreResultHelper<Callback<T> > |
516 IgnoreResult(const Callback<T>& data) { | 535 IgnoreResult(const Callback<T>& data) { |
517 return internal::IgnoreResultHelper<Callback<T> >(data); | 536 return internal::IgnoreResultHelper<Callback<T> >(data); |
518 } | 537 } |
519 | 538 |
| 539 BASE_EXPORT void DoNothing(); |
| 540 |
| 541 template<typename T> |
| 542 void DeletePointer(T* obj) { |
| 543 delete obj; |
| 544 } |
| 545 |
| 546 // ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the |
| 547 // Closure is executed and deleted no matter how the current scope exits. |
| 548 class BASE_EXPORT ScopedClosureRunner { |
| 549 public: |
| 550 explicit ScopedClosureRunner(const Closure& closure); |
| 551 ~ScopedClosureRunner(); |
| 552 |
| 553 Closure Release(); |
| 554 |
| 555 private: |
| 556 Closure closure_; |
| 557 |
| 558 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedClosureRunner); |
| 559 }; |
| 560 |
520 } // namespace base | 561 } // namespace base |
521 | 562 |
522 #endif // BASE_BIND_HELPERS_H_ | 563 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |