1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_FOUNDATION_UTIL_H_ 6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_FOUNDATION_UTIL_H_ 7 8 #include <CoreFoundation/CoreFoundation.h> 9 10 namespace partition_alloc::internal::base::mac { 11 12 // CFCast<>() and CFCastStrict<>() cast a basic CFTypeRef to a more 13 // specific CoreFoundation type. The compatibility of the passed 14 // object is found by comparing its opaque type against the 15 // requested type identifier. If the supplied object is not 16 // compatible with the requested return type, CFCast<>() returns 17 // NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer 18 // to either variant results in NULL being returned without 19 // triggering any DCHECK. 20 // 21 // Example usage: 22 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( 23 // CFArrayGetValueAtIndex(array, index)); 24 // 25 // CFTypeRef hello = CFSTR("hello world"); 26 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello); 27 28 template <typename T> 29 T CFCast(const CFTypeRef& cf_val); 30 31 template <typename T> 32 T CFCastStrict(const CFTypeRef& cf_val); 33 34 #define PA_CF_CAST_DECL(TypeCF) \ 35 template <> \ 36 TypeCF##Ref CFCast<TypeCF##Ref>(const CFTypeRef& cf_val); \ 37 \ 38 template <> \ 39 TypeCF##Ref CFCastStrict<TypeCF##Ref>(const CFTypeRef& cf_val) 40 41 PA_CF_CAST_DECL(CFArray); 42 PA_CF_CAST_DECL(CFBag); 43 PA_CF_CAST_DECL(CFBoolean); 44 PA_CF_CAST_DECL(CFData); 45 PA_CF_CAST_DECL(CFDate); 46 PA_CF_CAST_DECL(CFDictionary); 47 PA_CF_CAST_DECL(CFNull); 48 PA_CF_CAST_DECL(CFNumber); 49 PA_CF_CAST_DECL(CFSet); 50 PA_CF_CAST_DECL(CFString); 51 PA_CF_CAST_DECL(CFURL); 52 PA_CF_CAST_DECL(CFUUID); 53 54 #undef PA_CF_CAST_DECL 55 56 } // namespace partition_alloc::internal::base::mac 57 58 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_FOUNDATION_UTIL_H_ 59