diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-28 00:26:37 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-28 00:26:37 +0000 |
commit | e8bfc31d8cbd65213951163ffc28bc6615f71670 (patch) | |
tree | 831ef87394344cc07d1035efd402c4c8a37786d7 /base | |
parent | 2d62310296eb3c7eb6f92d7eb699dafa5450dac8 (diff) | |
download | chromium_src-e8bfc31d8cbd65213951163ffc28bc6615f71670.zip chromium_src-e8bfc31d8cbd65213951163ffc28bc6615f71670.tar.gz chromium_src-e8bfc31d8cbd65213951163ffc28bc6615f71670.tar.bz2 |
Add new helper that can adapt Callbacks with return values for Closures.
This is helpful for reducing manually written static functions that just effectively just invoke the function and ignore the return value.
BUG=87287
TEST=new unittest
Review URL: http://codereview.chromium.org/8048008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103060 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/bind_helpers.h | 30 | ||||
-rw-r--r-- | base/bind_unittest.cc | 15 |
2 files changed, 36 insertions, 9 deletions
diff --git a/base/bind_helpers.h b/base/bind_helpers.h index e4b4a17..7eb5716 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 << endl; } +// Callback<int(void)> cb = Bind(&DoSomething, 1); +// Closure c = IgnoreReturn(cb); // Prints "1" +// 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_ diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc index 8718d1d..dde2226 100644 --- a/base/bind_unittest.cc +++ b/base/bind_unittest.cc @@ -4,12 +4,6 @@ #include "base/bind.h" -#if defined(BASE_CALLBACK_H_) -// We explicitly do not want to include callback.h so people are not tempted -// to use bind.h in a headerfile for getting the Callback types. -#error "base/bind.h should avoid pulling in callback.h by default." -#endif - #include "base/callback.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -317,6 +311,15 @@ TEST_F(BindTest, ReturnValues) { EXPECT_EQ(51337, const_method_const_obj_cb.Run()); } +// IgnoreReturn adapter test. +// - Function with return value, and no params can be converted to Closure. +TEST_F(BindTest, IgnoreReturn) { + EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); + Callback<int(void)> normal_cb = Bind(&IntFunc0); + Closure c = IgnoreReturn(normal_cb); + c.Run(); +} + // Argument binding tests. // - Argument binding to primitive. // - Argument binding to primitive pointer. |