Chromium Code Reviews| Index: base/bind_helpers.h |
| diff --git a/base/bind_helpers.h b/base/bind_helpers.h |
| index e4b4a174dcba980af63b664f52a8df5ff98d87d0..207c9e6d085f092cc299cbb9f6105f04a5d4139d 100644 |
| --- a/base/bind_helpers.h |
| +++ b/base/bind_helpers.h |
| @@ -6,17 +6,22 @@ |
| // can be used specify the refcounting and reference semantics of arguments |
| // that are bound by the Bind() function in base/bind.h. |
| // |
| -// The public functions are base::Unretained() and base::ConstRef(). |
| +// The public functions are base::Unretained(), base::ConstRef(), and |
| +// base::IgnoreReturn(). |
| +// |
| // Unretained() allows Bind() to bind a non-refcounted class. |
| // ConstRef() allows binding a constant reference to an argument rather |
| // than a copy. |
| +// IgnoreReturn() is used to adapt a 0-argument Callback with a return type to |
| +// a Closure. This is useful if you need to PostTask with a function that has |
| +// a return value that you don't care about. |
| // |
| // |
| // EXAMPLE OF Unretained(): |
| // |
| // class Foo { |
| // public: |
| -// void func() { cout << "Foo:f" << endl; |
| +// void func() { cout << "Foo:f" << endl; } |
| // }; |
| // |
| // // In some function somewhere. |
| @@ -29,7 +34,7 @@ |
| // to compile because Foo does not support the AddRef() and Release() methods. |
| // |
| // |
| -// EXAMPLE OF ConstRef(); |
| +// EXAMPLE OF ConstRef(): |
| // void foo(int arg) { cout << arg << endl } |
| // |
| // int n = 1; |
| @@ -46,12 +51,21 @@ |
| // Note that because ConstRef() takes a reference on |n|, |n| must outlive all |
| // its bound callbacks. |
| // |
| +// |
| +// EXAMPLE OF IgnoreReturn(): |
| +// int DoSomething(int arg) { cout << arg << end; } |
|
darin (slow to review)
2011/09/27 16:48:01
nit: "end" -> "endl"
awong
2011/09/27 21:35:00
Done.
|
| +// Callback <int(void)> cb = Bind(&DoSomething, 1); |
|
darin (slow to review)
2011/09/27 16:48:01
nit: no space before "<"
awong
2011/09/27 21:35:00
Done.
|
| +// Closure c = IgnoreReturn(cb); // Prints "1" |
|
darin (slow to review)
2011/09/27 16:48:01
All of the use cases I can imagine would basically
|
| +// or |
| +// ml->PostTask(FROM_HERE, IgnoreReturn(cb)); // Prints "1" on |ml| |
| #ifndef BASE_BIND_HELPERS_H_ |
| #define BASE_BIND_HELPERS_H_ |
| #pragma once |
| #include "base/basictypes.h" |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/template_util.h" |
| @@ -255,6 +269,11 @@ struct MaybeRefcount<base::true_type, WeakPtr<T> > { |
| static void Release(const WeakPtr<T>&) {} |
| }; |
| +template <typename R> |
| +void VoidReturnAdapter(Callback<R(void)> callback) { |
| + callback.Run(); |
| +} |
| + |
| } // namespace internal |
| template <typename T> |
| @@ -267,6 +286,11 @@ inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
| return internal::ConstRefWrapper<T>(o); |
| } |
| +template <typename R> |
| +Closure IgnoreReturn(Callback<R(void)> callback) { |
| + return Bind(&internal::VoidReturnAdapter<R>, callback); |
| +} |
| + |
| } // namespace base |
| #endif // BASE_BIND_HELPERS_H_ |