diff options
author | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-24 08:56:11 +0000 |
---|---|---|
committer | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-24 08:56:11 +0000 |
commit | defe38f413645cd0e0c39a807607b6c68f7e6114 (patch) | |
tree | dedcf4d2738f07130afaf0e8d2c6b5b688b07c8b /base/mac | |
parent | b39e32e82406efb313998eb705ccd8544c0c8946 (diff) | |
download | chromium_src-defe38f413645cd0e0c39a807607b6c68f7e6114.zip chromium_src-defe38f413645cd0e0c39a807607b6c68f7e6114.tar.gz chromium_src-defe38f413645cd0e0c39a807607b6c68f7e6114.tar.bz2 |
BindBlock supports callbacks with one argument.
BUG=NONE
Review URL: https://chromiumcodereview.appspot.com/14247015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@196092 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac')
-rw-r--r-- | base/mac/bind_objc_block.h | 32 | ||||
-rw-r--r-- | base/mac/bind_objc_block_unittest.mm | 6 |
2 files changed, 28 insertions, 10 deletions
diff --git a/base/mac/bind_objc_block.h b/base/mac/bind_objc_block.h index ee1fe12..75da437 100644 --- a/base/mac/bind_objc_block.h +++ b/base/mac/bind_objc_block.h @@ -20,21 +20,33 @@ namespace base { namespace internal { -// Helper function to run the block contained in the parameter. -template<typename ReturnType> -ReturnType RunBlock(base::mac::ScopedBlock<ReturnType(^)()> block) { - ReturnType(^extracted_block)() = block.get(); +// Helper functions to run the block contained in the parameter. +template<typename R> +R RunBlock(base::mac::ScopedBlock<R(^)()> block) { + R(^extracted_block)() = block.get(); return extracted_block(); } +template<typename R, typename A1> +R RunBlock(base::mac::ScopedBlock<R(^)(A1)> block, A1 a) { + R(^extracted_block)(A1) = block.get(); + return extracted_block(a); +} + } // namespace internal -// Construct a callback from an objective-C block. -template<typename ReturnType> -base::Callback<ReturnType(void)> BindBlock(ReturnType(^block)()) { - return base::Bind(&base::internal::RunBlock<ReturnType>, - base::mac::ScopedBlock<ReturnType(^)()>( - Block_copy(block))); +// Construct a callback with no argument from an objective-C block. +template<typename R> +base::Callback<R(void)> BindBlock(R(^block)()) { + return base::Bind(&base::internal::RunBlock<R>, + base::mac::ScopedBlock<R(^)()>(Block_copy(block))); +} + +// Construct a callback with one argument from an objective-C block. +template<typename R, typename A1> +base::Callback<R(A1)> BindBlock(R(^block)(A1)) { + return base::Bind(&base::internal::RunBlock<R, A1>, + base::mac::ScopedBlock<R(^)(A1)>(Block_copy(block))); } } // namespace base diff --git a/base/mac/bind_objc_block_unittest.mm b/base/mac/bind_objc_block_unittest.mm index a44e4c7..888b3dc 100644 --- a/base/mac/bind_objc_block_unittest.mm +++ b/base/mac/bind_objc_block_unittest.mm @@ -45,4 +45,10 @@ TEST(BindObjcBlockTest, TestReturnValue) { EXPECT_EQ(kReturnValue, c.Run()); } +TEST(BindObjcBlockTest, TestArgument) { + const int kArgument = 42; + base::Callback<int(int)> c = base::BindBlock(^(int a){return a + 1;}); + EXPECT_EQ(kArgument + 1, c.Run(kArgument)); +} + } // namespace |