diff options
author | dmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-17 17:05:26 +0000 |
---|---|---|
committer | dmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-17 17:05:26 +0000 |
commit | 1340071ae15389bc71bac04738b0c3306494f3d8 (patch) | |
tree | 5ec282dcabded8969d4bc13e5a99281f0eb4b2de /base/mac_util.h | |
parent | 936cb4f663e5bc277f2474f07c0857feae71c9aa (diff) | |
download | chromium_src-1340071ae15389bc71bac04738b0c3306494f3d8.zip chromium_src-1340071ae15389bc71bac04738b0c3306494f3d8.tar.gz chromium_src-1340071ae15389bc71bac04738b0c3306494f3d8.tar.bz2 |
Add support for easily casting CFTypes to NSTypes
BUG=NONE
TEST=BUILD
Review URL: http://codereview.chromium.org/5857006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69552 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac_util.h')
-rw-r--r-- | base/mac_util.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/base/mac_util.h b/base/mac_util.h index d31bf82..9da90d2 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -10,6 +10,8 @@ #include <string> #include <vector> +#include "base/logging.h" + class FilePath; #ifdef __OBJC__ @@ -188,6 +190,52 @@ bool WasLaunchedAsHiddenLoginItem(); void NSObjectRetain(void* obj); void NSObjectRelease(void* obj); +// Convert toll-free bridged CFTypes to NSTypes. This does not autorelease +// |cf_val|. This is useful for the case where there is a CFType in a call that +// expects an NSType and the compiler is complaining about const casting +// problems. +// The call is used like this: +// NSString *foo = CFToNSCast(CFSTR("Hello")); +// The macro magic below is to enforce safe casting. It could possibly have +// been done using template function specialization, but template function +// specialization doesn't always work intuitively, +// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination +// of macros and function overloading is used instead. + +#define CF_TO_NS_CAST(TypeCF, TypeNS) \ +TypeNS* CFToNSCast(TypeCF cf_val) { \ + TypeNS* ns_val = \ + const_cast<id>(reinterpret_cast<const struct objc_object*>(cf_val)); \ + DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ + return ns_val; \ +} + +// List of toll-free bridged types taken from: +// http://www.cocoadev.com/index.pl?TollFreeBridged + +CF_TO_NS_CAST(CFArrayRef, NSArray); +CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); +CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); +CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); +CF_TO_NS_CAST(CFCalendarRef, NSCalendar); +CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); +CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); +CF_TO_NS_CAST(CFDataRef, NSData); +CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); +CF_TO_NS_CAST(CFDateRef, NSDate); +CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); +CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); +CF_TO_NS_CAST(CFNumberRef, NSNumber); +CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); +CF_TO_NS_CAST(CFSetRef, NSSet); +CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); +CF_TO_NS_CAST(CFStringRef, NSString); +CF_TO_NS_CAST(CFMutableStringRef, NSMutableString); +CF_TO_NS_CAST(CFURLRef, NSURL); +CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone); +CF_TO_NS_CAST(CFReadStreamRef, NSInputStream); +CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream); + } // namespace mac_util #endif // BASE_MAC_UTIL_H_ |