diff options
author | kushi.p@gmail.com <kushi.p@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 00:36:56 +0000 |
---|---|---|
committer | kushi.p@gmail.com <kushi.p@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 00:36:56 +0000 |
commit | 28f5649780a53eb29f76565fe64a726a4123ac77 (patch) | |
tree | 4c0280ef2cf799d692aabb9610d2562e1f4dcd93 /base/mac/foundation_util.h | |
parent | 69a57e0d5d571cc8222b01a508841c6b7f10f680 (diff) | |
download | chromium_src-28f5649780a53eb29f76565fe64a726a4123ac77.zip chromium_src-28f5649780a53eb29f76565fe64a726a4123ac77.tar.gz chromium_src-28f5649780a53eb29f76565fe64a726a4123ac77.tar.bz2 |
Create a CFCast<>() method for casting a basic CFTypeRef to a more specific CF type
BUG=86004
Review URL: http://codereview.chromium.org/8108003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106186 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac/foundation_util.h')
-rw-r--r-- | base/mac/foundation_util.h | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h index 2e7757e..4c4abc6 100644 --- a/base/mac/foundation_util.h +++ b/base/mac/foundation_util.h @@ -164,7 +164,7 @@ namespace mac { \ BASE_EXPORT TypeNS* CFToNSCast(TypeCF##Ref cf_val); \ BASE_EXPORT TypeCF##Ref NSToCFCast(TypeNS* ns_val); \ } \ -} \ +} #define CF_TO_NS_MUTABLE_CAST_DECL(name) \ CF_TO_NS_CAST_DECL(CF##name, NS##name) \ @@ -175,7 +175,7 @@ namespace mac { \ BASE_EXPORT NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \ BASE_EXPORT CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \ } \ -} \ +} // List of toll-free bridged types taken from: // http://www.cocoadev.com/index.pl?TollFreeBridged @@ -198,6 +198,35 @@ CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); CF_TO_NS_MUTABLE_CAST_DECL(String); CF_TO_NS_CAST_DECL(CFURL, NSURL); +namespace base { +namespace mac { + +// CFCast<>() and CFCastStrict<>() cast a basic CFTypeRef to a more +// specific CoreFoundation type. The compatibility of the passed +// object is found by comparing its opaque type against the +// requested type identifier. If the supplied object is not +// compatible with the requested return type, CFCast<>() returns +// NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer +// to either variant results in NULL being returned without +// triggering any DCHECK. +// +// Example usage: +// CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( +// CFArrayGetValueAtIndex(array, index)); +// +// CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( +// base::mac::GetValueFromDictionary(some_dict, +// CFSTR("a_key"), +// CFStringGetTypeID())); +BASE_EXPORT template<class T> +T CFCast(const CFTypeRef& cf_val); + +BASE_EXPORT template<class T> +T CFCastStrict(const CFTypeRef& cf_val); + +} // namespace mac +} // namespace base + // Stream operations for CFTypes. They can be used with NSTypes as well // by using the NSToCFCast methods above. // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |