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/compiler_specific.h" |
12 #include "base/mac/scoped_block.h" | 13 #include "base/mac/scoped_block.h" |
13 | 14 |
14 // BindBlock builds a callback from an Objective-C block. Example usages: | 15 // BindBlock builds a callback from an Objective-C block. Example usages: |
15 // | 16 // |
16 // Closure closure = BindBlock(^{DoSomething();}); | 17 // Closure closure = BindBlock(^{DoSomething();}); |
17 // | 18 // |
18 // Callback<int(void)> callback = BindBlock(^{return 42;}); | 19 // Callback<int(void)> callback = BindBlock(^{return 42;}); |
19 // | 20 // |
20 // Callback<void(const std::string&, const std::string&)> callback = | 21 // Callback<void(const std::string&, const std::string&)> callback = |
21 // BindBlock(^(const std::string& arg0, const std::string& arg1) { | 22 // BindBlock(^(const std::string& arg0, const std::string& arg1) { |
22 // ... | 23 // ... |
23 // }); | 24 // }); |
24 // | 25 // |
25 // These variadic templates will accommodate any number of arguments, however | 26 // 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 // 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 // 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 // arguments, so functionally the templates are limited to binding blocks with |
29 // zero through six arguments. | 30 // zero through six arguments. |
| 31 // |
| 32 // For code compiled with ARC (automatic reference counting), use BindBlockArc. |
| 33 // This is because the method has a different implementation (to avoid over- |
| 34 // retaining the block) and need to have a different name not to break the ODR |
| 35 // (one definition rule). Another subtle difference is that the implementation |
| 36 // will call a different version of ScopedBlock constructor thus the linker must |
| 37 // not merge both functions. |
30 | 38 |
31 namespace base { | 39 namespace base { |
32 | 40 |
33 namespace internal { | 41 namespace internal { |
34 | 42 |
35 // Helper function to run the block contained in the parameter. | 43 // Helper function to run the block contained in the parameter. |
36 template<typename R, typename... Args> | 44 template<typename R, typename... Args> |
37 R RunBlock(base::mac::ScopedBlock<R(^)(Args...)> block, Args... args) { | 45 R RunBlock(base::mac::ScopedBlock<R(^)(Args...)> block, Args... args) { |
38 R(^extracted_block)(Args...) = block.get(); | 46 R(^extracted_block)(Args...) = block.get(); |
39 return extracted_block(args...); | 47 return extracted_block(args...); |
40 } | 48 } |
41 | 49 |
42 } // namespace internal | 50 } // namespace internal |
43 | 51 |
| 52 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 53 |
44 // Construct a callback from an objective-C block with up to six arguments (see | 54 // Construct a callback from an objective-C block with up to six arguments (see |
45 // note above). | 55 // note above). |
46 template<typename R, typename... Args> | 56 template<typename R, typename... Args> |
47 base::Callback<R(Args...)> BindBlock(R(^block)(Args...)) { | 57 base::Callback<R(Args...)> BindBlock(R(^block)(Args...)) { |
48 return base::Bind( | 58 return base::Bind( |
49 &base::internal::RunBlock<R, Args...>, | 59 &base::internal::RunBlock<R, Args...>, |
50 base::mac::ScopedBlock<R (^)(Args...)>( | 60 base::mac::ScopedBlock<R (^)(Args...)>( |
51 base::mac::internal::ScopedBlockTraits<R (^)(Args...)>::Retain( | 61 base::mac::internal::ScopedBlockTraits<R (^)(Args...)>::Retain( |
52 block))); | 62 block))); |
53 } | 63 } |
54 | 64 |
| 65 #else |
| 66 |
| 67 // Construct a callback from an objective-C block with up to six arguments (see |
| 68 // note above). |
| 69 template <typename R, typename... Args> |
| 70 base::Callback<R(Args...)> BindBlockArc(R (^block)(Args...)) { |
| 71 return base::Bind(&base::internal::RunBlock<R, Args...>, |
| 72 base::mac::ScopedBlock<R (^)(Args...)>(block)); |
| 73 } |
| 74 |
| 75 #endif |
| 76 |
55 } // namespace base | 77 } // namespace base |
56 | 78 |
57 #endif // BASE_MAC_BIND_OBJC_BLOCK_H_ | 79 #endif // BASE_MAC_BIND_OBJC_BLOCK_H_ |
OLD | NEW |