OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MAC_BIND_OBJC_BLOCK_H_ | 5 #ifndef BASE_MAC_BIND_OBJC_BLOCK_H_ |
6 #define BASE_MAC_BIND_OBJC_BLOCK_H_ | 6 #define BASE_MAC_BIND_OBJC_BLOCK_H_ |
7 | 7 |
8 #include <Block.h> | 8 #include <Block.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/mac/scoped_block.h" | 12 #include "base/mac/scoped_block.h" |
13 | 13 |
14 // BindBlock builds a callback from an Objective-C block. Example usages: | 14 // BindBlock builds a callback from an Objective-C block. Example usages: |
15 // | 15 // |
16 // Closure closure = BindBlock(^{DoSomething();}); | 16 // Closure closure = BindBlock(^{DoSomething();}); |
17 // | 17 // |
18 // Callback<int(void)> callback = BindBlock(^{return 42;}); | 18 // Callback<int(void)> callback = BindBlock(^{return 42;}); |
19 // | 19 // |
20 // Callback<void(const std::string&, const std::string&)> callback = | 20 // Callback<void(const std::string&, const std::string&)> callback = |
21 // BindBlock(^(const std::string& arg0, const std::string& arg1) { | 21 // BindBlock(^(const std::string& arg0, const std::string& arg1) { |
22 // ... | 22 // ... |
23 // }); | 23 // }); |
| 24 // |
| 25 // These variadic templates will accommodate any number of arguments, however |
| 26 // the underlying templates in bind_internal.h and callback.h are limited to |
| 27 // seven total arguments, and the bound block itself is used as one of these |
| 28 // arguments, so functionally the templates are limited to binding blocks with |
| 29 // zero through six arguments. |
24 | 30 |
25 namespace base { | 31 namespace base { |
26 | 32 |
27 namespace internal { | 33 namespace internal { |
28 | 34 |
29 // Helper functions to run the block contained in the parameter. | 35 // Helper function to run the block contained in the parameter. |
30 template<typename R> | 36 template<typename R, typename... Args> |
31 R RunBlock(base::mac::ScopedBlock<R(^)()> block) { | 37 R RunBlock(base::mac::ScopedBlock<R(^)(Args...)> block, Args... args) { |
32 R(^extracted_block)() = block.get(); | 38 R(^extracted_block)(Args...) = block.get(); |
33 return extracted_block(); | 39 return extracted_block(args...); |
34 } | |
35 | |
36 template<typename R, typename A1> | |
37 R RunBlock(base::mac::ScopedBlock<R(^)(A1)> block, A1 a) { | |
38 R(^extracted_block)(A1) = block.get(); | |
39 return extracted_block(a); | |
40 } | |
41 | |
42 template<typename R, typename A1, typename A2> | |
43 R RunBlock(base::mac::ScopedBlock<R(^)(A1, A2)> block, A1 a, A2 b) { | |
44 R(^extracted_block)(A1, A2) = block.get(); | |
45 return extracted_block(a, b); | |
46 } | 40 } |
47 | 41 |
48 } // namespace internal | 42 } // namespace internal |
49 | 43 |
50 // Construct a callback with no argument from an objective-C block. | 44 // Construct a callback from an objective-C block with up to six arguments (see |
51 template<typename R> | 45 // note above). |
52 base::Callback<R(void)> BindBlock(R(^block)()) { | 46 template<typename R, typename... Args> |
53 return base::Bind(&base::internal::RunBlock<R>, | 47 base::Callback<R(Args...)> BindBlock(R(^block)(Args...)) { |
54 base::mac::ScopedBlock<R(^)()>(Block_copy(block))); | 48 return base::Bind(&base::internal::RunBlock<R, Args...>, |
55 } | 49 base::mac::ScopedBlock<R(^)(Args...)>(Block_copy(block))); |
56 | |
57 // Construct a callback with one argument from an objective-C block. | |
58 template<typename R, typename A1> | |
59 base::Callback<R(A1)> BindBlock(R(^block)(A1)) { | |
60 return base::Bind(&base::internal::RunBlock<R, A1>, | |
61 base::mac::ScopedBlock<R(^)(A1)>(Block_copy(block))); | |
62 } | |
63 | |
64 // Construct a callback with two arguments from an objective-C block. | |
65 template<typename R, typename A1, typename A2> | |
66 base::Callback<R(A1, A2)> BindBlock(R(^block)(A1, A2)) { | |
67 return base::Bind(&base::internal::RunBlock<R, A1, A2>, | |
68 base::mac::ScopedBlock<R(^)(A1, A2)>(Block_copy(block))); | |
69 } | 50 } |
70 | 51 |
71 } // namespace base | 52 } // namespace base |
72 | 53 |
73 #endif // BASE_MAC_BIND_OBJC_BLOCK_H_ | 54 #endif // BASE_MAC_BIND_OBJC_BLOCK_H_ |
OLD | NEW |