OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_SCOPED_BLOCK_H_ | 5 #ifndef BASE_MAC_SCOPED_BLOCK_H_ |
6 #define BASE_MAC_SCOPED_BLOCK_H_ | 6 #define BASE_MAC_SCOPED_BLOCK_H_ |
7 | 7 |
8 #include <Block.h> | 8 #include <Block.h> |
9 | 9 |
10 #include "base/mac/scoped_typeref.h" | 10 #include "base/mac/scoped_typeref.h" |
11 | 11 |
| 12 #if defined(__has_feature) && __has_feature(objc_arc) |
| 13 #define BASE_MAC_BRIDGE_CAST(TYPE, VALUE) (__bridge TYPE)(VALUE) |
| 14 #else |
| 15 #define BASE_MAC_BRIDGE_CAST(TYPE, VALUE) VALUE |
| 16 #endif |
| 17 |
12 namespace base { | 18 namespace base { |
13 namespace mac { | 19 namespace mac { |
14 | 20 |
15 namespace internal { | 21 namespace internal { |
16 | 22 |
17 template <typename B> | 23 template <typename B> |
18 struct ScopedBlockTraits { | 24 struct ScopedBlockTraits { |
19 static B InvalidValue() { return nullptr; } | 25 static B InvalidValue() { return nullptr; } |
20 static B Retain(B block) { return Block_copy(block); } | 26 static B Retain(B block) { |
21 static void Release(B block) { Block_release(block); } | 27 return BASE_MAC_BRIDGE_CAST( |
| 28 B, Block_copy(BASE_MAC_BRIDGE_CAST(const void*, block))); |
| 29 } |
| 30 static void Release(B block) { |
| 31 Block_release(BASE_MAC_BRIDGE_CAST(const void*, block)); |
| 32 } |
22 }; | 33 }; |
23 | 34 |
24 } // namespace internal | 35 } // namespace internal |
25 | 36 |
26 // ScopedBlock<> is patterned after ScopedCFTypeRef<>, but uses Block_copy() and | 37 // ScopedBlock<> is patterned after ScopedCFTypeRef<>, but uses Block_copy() and |
27 // Block_release() instead of CFRetain() and CFRelease(). | 38 // Block_release() instead of CFRetain() and CFRelease(). |
28 | 39 |
29 template <typename B> | 40 template <typename B> |
30 using ScopedBlock = ScopedTypeRef<B, internal::ScopedBlockTraits<B>>; | 41 using ScopedBlock = ScopedTypeRef<B, internal::ScopedBlockTraits<B>>; |
31 | 42 |
32 } // namespace mac | 43 } // namespace mac |
33 } // namespace base | 44 } // namespace base |
34 | 45 |
| 46 #undef BASE_MAC_BRIDGE_CAST |
| 47 |
35 #endif // BASE_MAC_SCOPED_BLOCK_H_ | 48 #endif // BASE_MAC_SCOPED_BLOCK_H_ |
OLD | NEW |