diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-30 09:09:34 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-30 09:09:34 +0000 |
commit | cea20fe4e5ee8e0b81c9d4f0ff7750cb8ef64aef (patch) | |
tree | 48ce1068ad3c94af442bdb9b373329ff3300ef5f /base/bind_unittest.cc | |
parent | f3f0da43dfff66771f894b48c45c8c567062244f (diff) | |
download | chromium_src-cea20fe4e5ee8e0b81c9d4f0ff7750cb8ef64aef.zip chromium_src-cea20fe4e5ee8e0b81c9d4f0ff7750cb8ef64aef.tar.gz chromium_src-cea20fe4e5ee8e0b81c9d4f0ff7750cb8ef64aef.tar.bz2 |
Allow Bind() to take a Callback<> and bind all its free parameters.
Basically, turns base::Callback<void(...)> to base::Closure.
It turns out there are a number of use caess where an API takes a callback, and then wants to invoke the Callback on another thread. This piece of syntactic sugar removes the need for custom helper functions.
This isn't quite full currying, however it is much simpler to implement.
BUG=87287
TEST=none
Review URL: http://codereview.chromium.org/8073012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103446 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/bind_unittest.cc')
-rw-r--r-- | base/bind_unittest.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc index dde2226..0c24710 100644 --- a/base/bind_unittest.cc +++ b/base/bind_unittest.cc @@ -153,6 +153,10 @@ int Sum(int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; } +void OutputSum(int* output, int a, int b, int c, int d, int e) { + *output = a + b + c + d + e; +} + const char* CStringIdentity(const char* s) { return s; } @@ -243,6 +247,37 @@ TEST_F(BindTest, ArityTest) { EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); } +// Bind should be able to take existing Callbacks and convert to a Closure. +TEST_F(BindTest, CallbackBindMore) { + int output = 0; + Closure c; + + Callback<void(int)> c1 = Bind(&OutputSum, &output, 16, 8, 4, 2); + c = Bind(c1, 10); + c.Run(); + EXPECT_EQ(40, output); + + Callback<void(int,int)> c2 = Bind(&OutputSum, &output, 16, 8, 4); + c = Bind(c2, 10, 9); + c.Run(); + EXPECT_EQ(47, output); + + Callback<void(int,int,int)> c3 = Bind(&OutputSum, &output, 16, 8); + c = Bind(c3, 10, 9, 8); + c.Run(); + EXPECT_EQ(51, output); + + Callback<void(int,int,int,int)> c4 = Bind(&OutputSum, &output, 16); + c = Bind(c4, 10, 9, 8, 7); + c.Run(); + EXPECT_EQ(50, output); + + Callback<void(int,int,int,int,int)> c5 = Bind(&OutputSum, &output); + c = Bind(c5, 10, 9, 8, 7, 6); + c.Run(); + EXPECT_EQ(40, output); +} + // Function type support. // - Normal function. // - Normal function bound with non-refcounted first argument. |