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