summaryrefslogtreecommitdiffstats
path: root/base/mac_util.h
diff options
context:
space:
mode:
authordmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-17 18:42:23 +0000
committerdmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-17 18:42:23 +0000
commit45568795e2a04e29cefdaad058f55563a98cd970 (patch)
tree72384759ceac39525a9a53dff4d595da5c7030e7 /base/mac_util.h
parentcdb188a4ad827fb2a82375e7674cb4fe3f1616df (diff)
downloadchromium_src-45568795e2a04e29cefdaad058f55563a98cd970.zip
chromium_src-45568795e2a04e29cefdaad058f55563a98cd970.tar.gz
chromium_src-45568795e2a04e29cefdaad058f55563a98cd970.tar.bz2
Add support for easily casting CFTypes to NSTypes
BUG=NONE TEST=BUILD Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=69552 Review URL: http://codereview.chromium.org/5857006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69561 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac_util.h')
-rw-r--r--base/mac_util.h62
1 files changed, 58 insertions, 4 deletions
diff --git a/base/mac_util.h b/base/mac_util.h
index d31bf82..4497da3 100644
--- a/base/mac_util.h
+++ b/base/mac_util.h
@@ -10,16 +10,20 @@
#include <string>
#include <vector>
-class FilePath;
+#include "base/logging.h"
+
+#if defined(__OBJC__)
+#import <Foundation/Foundation.h>
-#ifdef __OBJC__
@class NSBundle;
@class NSWindow;
-#else
+#else // __OBJC__
class NSBundle;
class NSImage;
class NSWindow;
-#endif
+#endif // __OBJC__
+
+class FilePath;
// Adapted from NSPathUtilities.h and NSObjCRuntime.h.
#if __LP64__ || NS_BUILD_32_LIKE_64
@@ -188,6 +192,56 @@ bool WasLaunchedAsHiddenLoginItem();
void NSObjectRetain(void* obj);
void NSObjectRelease(void* obj);
+#if defined(__OBJC__)
+
+// 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) \
+inline 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);
+
+#endif // __OBJC__
+
} // namespace mac_util
#endif // BASE_MAC_UTIL_H_