summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-30 09:09:34 +0000
committerajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-30 09:09:34 +0000
commitcea20fe4e5ee8e0b81c9d4f0ff7750cb8ef64aef (patch)
tree48ce1068ad3c94af442bdb9b373329ff3300ef5f
parentf3f0da43dfff66771f894b48c45c8c567062244f (diff)
downloadchromium_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
-rw-r--r--base/bind.h46
-rw-r--r--base/bind.h.pump20
-rw-r--r--base/bind_internal.h41
-rw-r--r--base/bind_internal.h.pump17
-rw-r--r--base/bind_unittest.cc35
5 files changed, 159 insertions, 0 deletions
diff --git a/base/bind.h b/base/bind.h
index cd9eb19..f023308e 100644
--- a/base/bind.h
+++ b/base/bind.h
@@ -3,6 +3,7 @@
// DO NOT EDIT BY HAND!!!
+
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -94,6 +95,51 @@ Bind(Sig f, const P1& p1, const P2& p2, const P3& p3, const P4& p4,
f, p1, p2, p3, p4, p5, p6));
}
+// Specializations to allow binding all the free arguments in a
+// pre-existing base::Callback<>. This does not give full support for
+// currying, but is significantly simpler and addresses the use case
+// where a base::Callback<> needs to be invoked on another context/thread.
+template <typename Sig, typename P1>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1) {
+ return base::Bind(&internal::BindMoreFunc1<Sig, P1>, callback, p1);
+}
+
+template <typename Sig, typename P1, typename P2>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2) {
+ return base::Bind(&internal::BindMoreFunc2<Sig, P1, P2>, callback, p1, p2);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3) {
+ return base::Bind(&internal::BindMoreFunc3<Sig, P1, P2, P3>, callback, p1,
+ p2, p3);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4) {
+ return base::Bind(&internal::BindMoreFunc4<Sig, P1, P2, P3, P4>, callback,
+ p1, p2, p3, p4);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4,
+ typename P5>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4, const P5& p5) {
+ return base::Bind(&internal::BindMoreFunc5<Sig, P1, P2, P3, P4, P5>,
+ callback, p1, p2, p3, p4, p5);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4,
+ typename P5, typename P6>
+base::Closure Bind(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6) {
+ return base::Bind(&internal::BindMoreFunc6<Sig, P1, P2, P3, P4, P5, P6>,
+ callback, p1, p2, p3, p4, p5, p6);
+}
+
} // namespace base
#endif // BASE_BIND_H_
diff --git a/base/bind.h.pump b/base/bind.h.pump
index 62b313f..eca00cd 100644
--- a/base/bind.h.pump
+++ b/base/bind.h.pump
@@ -63,6 +63,26 @@ $for BOUND_ARG , [[P$(BOUND_ARG)]]>(
}
]]
+]] $$ for BOUND
+
+// Specializations to allow binding all the free arguments in a
+// pre-existing base::Callback<>. This does not give full support for
+// currying, but is significantly simpler and addresses the use case
+// where a base::Callback<> needs to be invoked on another context/thread.
+$for BOUND [[
+$range BOUND_ARG 1..BOUND
+$if BOUND != 0 [[
+
+template <typename Sig, $for BOUND_ARG , [[typename P$(BOUND_ARG)]]>
+base::Closure Bind(const base::Callback<Sig>& callback, [[]]
+$for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) {
+ return base::Bind([[]]
+&internal::BindMoreFunc$(BOUND)<Sig, $for BOUND_ARG , [[P$(BOUND_ARG)]]>, [[]]
+callback, [[]]
+$for BOUND_ARG , [[p$(BOUND_ARG)]]);
+}
+
+]]
]] $$ for BOUND
diff --git a/base/bind_internal.h b/base/bind_internal.h
index 17ce9c8..404b795 100644
--- a/base/bind_internal.h
+++ b/base/bind_internal.h
@@ -1721,6 +1721,47 @@ struct Invoker6<true, StorageType, void(T::*)(X1, X2, X3, X4, X5)> {
}
};
+// BindMoreFuncN<>
+//
+// This set of functions help in fully binding the free parameters in a
+// Callback<>.
+template <typename Sig, typename P1>
+void BindMoreFunc1(const base::Callback<Sig>& callback, const P1& p1) {
+ callback.Run(p1);
+}
+
+template <typename Sig, typename P1, typename P2>
+void BindMoreFunc2(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2) {
+ callback.Run(p1, p2);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3>
+void BindMoreFunc3(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3) {
+ callback.Run(p1, p2, p3);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4>
+void BindMoreFunc4(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4) {
+ callback.Run(p1, p2, p3, p4);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4,
+ typename P5>
+void BindMoreFunc5(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4, const P5& p5) {
+ callback.Run(p1, p2, p3, p4, p5);
+}
+
+template <typename Sig, typename P1, typename P2, typename P3, typename P4,
+ typename P5, typename P6>
+void BindMoreFunc6(const base::Callback<Sig>& callback, const P1& p1,
+ const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6) {
+ callback.Run(p1, p2, p3, p4, p5, p6);
+}
+
// InvokerStorageN<>
//
// These are the actual storage classes for the Invokers.
diff --git a/base/bind_internal.h.pump b/base/bind_internal.h.pump
index 31988c5..532fe3d 100644
--- a/base/bind_internal.h.pump
+++ b/base/bind_internal.h.pump
@@ -266,6 +266,23 @@ $for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]);
]] $$ for ARITY
]] $$ for BOUND
+// BindMoreFuncN<>
+//
+// This set of functions help in fully binding the free parameters in a
+// Callback<>.
+$for BOUND [[
+$range BOUND_ARG 1..BOUND
+$if BOUND != 0 [[
+
+template <typename Sig, $for BOUND_ARG , [[typename P$(BOUND_ARG)]]>
+void BindMoreFunc$(BOUND)(const base::Callback<Sig>& callback, [[]]
+$for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) {
+ callback.Run($for BOUND_ARG , [[p$(BOUND_ARG)]]);
+}
+
+]] $$ if BOUND
+]] $$ for BOUND
+
// InvokerStorageN<>
//
// These are the actual storage classes for the Invokers.
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.