Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: base/mac/mac_util.h

Issue 6594096: Clean up CF To NS Casts and make them slightly safer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix space Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/mac/mac_util.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_MAC_MAC_UTIL_H_ 5 #ifndef BASE_MAC_MAC_UTIL_H_
6 #define BASE_MAC_MAC_UTIL_H_ 6 #define BASE_MAC_MAC_UTIL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <Carbon/Carbon.h> 9 #include <Carbon/Carbon.h>
10 #include <string> 10 #include <string>
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // specified hide flag. 103 // specified hide flag.
104 void AddToLoginItems(bool hide_on_startup); 104 void AddToLoginItems(bool hide_on_startup);
105 105
106 // Removes the current application from the list Of Login Items. 106 // Removes the current application from the list Of Login Items.
107 void RemoveFromLoginItems(); 107 void RemoveFromLoginItems();
108 108
109 // Returns true if the current process was automatically launched as a 109 // Returns true if the current process was automatically launched as a
110 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows. 110 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
111 bool WasLaunchedAsHiddenLoginItem(); 111 bool WasLaunchedAsHiddenLoginItem();
112 112
113 #if defined(__OBJC__) 113 } // namespace mac
114 } // namespace base
114 115
115 // Convert toll-free bridged CFTypes to NSTypes. This does not autorelease 116 #if !defined(__OBJC__)
116 // |cf_val|. This is useful for the case where there is a CFType in a call that 117 #define OBJC_CPP_CLASS_DECL(x) class x;
117 // expects an NSType and the compiler is complaining about const casting 118 #else // __OBJC__
118 // problems. 119 #define OBJC_CPP_CLASS_DECL(x)
119 // The call is used like this: 120 #endif // __OBJC__
121
122 // Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not
123 // autorelease |cf_val|. This is useful for the case where there is a CFType in
124 // a call that expects an NSType and the compiler is complaining about const
125 // casting problems.
126 // The calls are used like this:
120 // NSString *foo = CFToNSCast(CFSTR("Hello")); 127 // NSString *foo = CFToNSCast(CFSTR("Hello"));
128 // CFStringRef foo2 = NSToCFCast(@"Hello");
121 // The macro magic below is to enforce safe casting. It could possibly have 129 // The macro magic below is to enforce safe casting. It could possibly have
122 // been done using template function specialization, but template function 130 // been done using template function specialization, but template function
123 // specialization doesn't always work intuitively, 131 // specialization doesn't always work intuitively,
124 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination 132 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination
125 // of macros and function overloading is used instead. 133 // of macros and function overloading is used instead.
126 134
127 #define CF_TO_NS_CAST(TypeCF, TypeNS) \ 135 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \
128 inline TypeNS* CFToNSCast(TypeCF cf_val) { \ 136 OBJC_CPP_CLASS_DECL(TypeNS) \
129 TypeNS* ns_val = \ 137 \
130 const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ 138 namespace base { \
131 DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ 139 namespace mac { \
132 return ns_val; \ 140 TypeNS* CFToNSCast(TypeCF##Ref cf_val); \
133 } 141 TypeCF##Ref NSToCFCast(TypeNS* ns_val); \
142 } \
143 } \
144
145 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \
146 CF_TO_NS_CAST_DECL(CF##name, NS##name) \
147 OBJC_CPP_CLASS_DECL(NSMutable##name) \
148 \
149 namespace base { \
150 namespace mac { \
151 NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \
152 CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \
153 } \
154 } \
134 155
135 // List of toll-free bridged types taken from: 156 // List of toll-free bridged types taken from:
136 // http://www.cocoadev.com/index.pl?TollFreeBridged 157 // http://www.cocoadev.com/index.pl?TollFreeBridged
137 158
138 CF_TO_NS_CAST(CFArrayRef, NSArray); 159 CF_TO_NS_MUTABLE_CAST_DECL(Array);
139 CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); 160 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString);
140 CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); 161 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar);
141 CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); 162 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet);
142 CF_TO_NS_CAST(CFCalendarRef, NSCalendar); 163 CF_TO_NS_MUTABLE_CAST_DECL(Data);
143 CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); 164 CF_TO_NS_CAST_DECL(CFDate, NSDate);
144 CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); 165 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary);
145 CF_TO_NS_CAST(CFDataRef, NSData); 166 CF_TO_NS_CAST_DECL(CFError, NSError);
146 CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); 167 CF_TO_NS_CAST_DECL(CFLocale, NSLocale);
147 CF_TO_NS_CAST(CFDateRef, NSDate); 168 CF_TO_NS_CAST_DECL(CFNumber, NSNumber);
148 CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); 169 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer);
149 CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); 170 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone);
150 CF_TO_NS_CAST(CFNumberRef, NSNumber); 171 CF_TO_NS_MUTABLE_CAST_DECL(Set);
151 CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); 172 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream);
152 CF_TO_NS_CAST(CFSetRef, NSSet); 173 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream);
153 CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); 174 CF_TO_NS_MUTABLE_CAST_DECL(String);
154 CF_TO_NS_CAST(CFStringRef, NSString); 175 CF_TO_NS_CAST_DECL(CFURL, NSURL);
155 CF_TO_NS_CAST(CFMutableStringRef, NSMutableString);
156 CF_TO_NS_CAST(CFURLRef, NSURL);
157 CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone);
158 CF_TO_NS_CAST(CFReadStreamRef, NSInputStream);
159 CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream);
160 176
161 #endif // __OBJC__ 177 // Stream operations for CFTypes. They can be used with NSTypes as well
162 178 // by using the NSToCFCast methods above.
163 } // namespace mac 179 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
164 } // namespace base 180 // Operator << can not be overloaded for ObjectiveC types as the compiler
181 // can not distinguish between overloads for id with overloads for void*.
182 extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err);
183 extern std::ostream& operator<<(std::ostream& o, const CFStringRef str);
165 184
166 #endif // BASE_MAC_MAC_UTIL_H_ 185 #endif // BASE_MAC_MAC_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/mac/mac_util.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698