summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjoaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-28 21:11:15 +0000
committerjoaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-28 21:11:15 +0000
commitd15483914b628725d66f1fedbaadd90e18313de3 (patch)
tree0ba95627e8d244bffe03fa14dc4573254eca9fa2
parent70938c03b7fad9d26ba12d75ac25ecb97c6b5064 (diff)
downloadchromium_src-d15483914b628725d66f1fedbaadd90e18313de3.zip
chromium_src-d15483914b628725d66f1fedbaadd90e18313de3.tar.gz
chromium_src-d15483914b628725d66f1fedbaadd90e18313de3.tar.bz2
Add support for two arguments to base::BindBlock.
BUG=None Review URL: https://codereview.chromium.org/148263010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247502 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/mac/bind_objc_block.h19
-rw-r--r--base/mac/bind_objc_block_unittest.mm13
2 files changed, 32 insertions, 0 deletions
diff --git a/base/mac/bind_objc_block.h b/base/mac/bind_objc_block.h
index 75da437..9deb2d2 100644
--- a/base/mac/bind_objc_block.h
+++ b/base/mac/bind_objc_block.h
@@ -14,7 +14,13 @@
// BindBlock builds a callback from an Objective-C block. Example usages:
//
// Closure closure = BindBlock(^{DoSomething();});
+//
// Callback<int(void)> callback = BindBlock(^{return 42;});
+//
+// Callback<void(const std::string&, const std::string&)> callback =
+// BindBlock(^(const std::string& arg0, const std::string& arg1) {
+// ...
+// });
namespace base {
@@ -33,6 +39,12 @@ R RunBlock(base::mac::ScopedBlock<R(^)(A1)> block, A1 a) {
return extracted_block(a);
}
+template<typename R, typename A1, typename A2>
+R RunBlock(base::mac::ScopedBlock<R(^)(A1, A2)> block, A1 a, A2 b) {
+ R(^extracted_block)(A1, A2) = block.get();
+ return extracted_block(a, b);
+}
+
} // namespace internal
// Construct a callback with no argument from an objective-C block.
@@ -49,6 +61,13 @@ base::Callback<R(A1)> BindBlock(R(^block)(A1)) {
base::mac::ScopedBlock<R(^)(A1)>(Block_copy(block)));
}
+// Construct a callback with two arguments from an objective-C block.
+template<typename R, typename A1, typename A2>
+base::Callback<R(A1, A2)> BindBlock(R(^block)(A1, A2)) {
+ return base::Bind(&base::internal::RunBlock<R, A1, A2>,
+ base::mac::ScopedBlock<R(^)(A1, A2)>(Block_copy(block)));
+}
+
} // namespace base
#endif // BASE_MAC_BIND_OBJC_BLOCK_H_
diff --git a/base/mac/bind_objc_block_unittest.mm b/base/mac/bind_objc_block_unittest.mm
index a4bcd76..c72fd4a 100644
--- a/base/mac/bind_objc_block_unittest.mm
+++ b/base/mac/bind_objc_block_unittest.mm
@@ -4,6 +4,8 @@
#import "base/mac/bind_objc_block.h"
+#include <string>
+
#include "base/callback.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
@@ -51,4 +53,15 @@ TEST(BindObjcBlockTest, TestArgument) {
EXPECT_EQ(kArgument + 1, c.Run(kArgument));
}
+TEST(BindObjcBlockTest, TestTwoArguments) {
+ std::string result;
+ std::string* ptr = &result;
+ base::Callback<void(const std::string&, const std::string&)> c =
+ base::BindBlock(^(const std::string& a, const std::string& b) {
+ *ptr = a + b;
+ });
+ c.Run("forty", "two");
+ EXPECT_EQ(result, "fortytwo");
+}
+
} // namespace