OLD | NEW |
---|---|
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 Loading... | |
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_DEFN(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_DEFN(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(TypeCF, TypeNS) \ |
128 inline TypeNS* CFToNSCast(TypeCF cf_val) { \ | 136 OBJC_CPP_CLASS_DEFN(TypeNS); \ |
137 \ | |
138 namespace base { \ | |
139 namespace mac { \ | |
140 inline TypeNS* CFToNSCast(TypeCF##Ref cf_val) { \ | |
141 DCHECK(!cf_val || TypeCF##GetTypeID() == CFGetTypeID(cf_val)); \ | |
129 TypeNS* ns_val = \ | 142 TypeNS* ns_val = \ |
130 const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ | 143 const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ |
131 DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ | |
132 return ns_val; \ | 144 return ns_val; \ |
133 } | 145 } \ |
146 \ | |
147 inline TypeCF##Ref NSToCFCast(TypeNS* ns_val) { \ | |
148 TypeCF##Ref cf_val = reinterpret_cast<TypeCF##Ref>(ns_val); \ | |
149 DCHECK(!cf_val || TypeCF##GetTypeID() == CFGetTypeID(cf_val)); \ | |
150 return cf_val; \ | |
151 } \ | |
152 } \ | |
153 } \ | |
134 | 154 |
155 #define CF_TO_NS_MUTABLE_CAST(name) \ | |
156 CF_TO_NS_CAST(CF##name, NS##name) \ | |
157 OBJC_CPP_CLASS_DEFN(NSMutable##name) \ | |
158 \ | |
159 namespace base { \ | |
160 namespace mac { \ | |
161 inline NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val) { \ | |
Nico
2011/03/02 02:21:22
I guess Elliot would be happier if all these funct
dmac
2011/03/02 22:20:09
Done.
| |
162 DCHECK(!cf_val || CF##name##GetTypeID() == CFGetTypeID(cf_val)); \ | |
163 NSMutable##name* ns_val = reinterpret_cast<NSMutable##name*>(cf_val); \ | |
164 return ns_val; \ | |
165 } \ | |
166 \ | |
167 inline CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val) { \ | |
168 CFMutable##name##Ref cf_val = \ | |
169 reinterpret_cast<CFMutable##name##Ref>(ns_val); \ | |
170 DCHECK(!cf_val || CF##name##GetTypeID() == CFGetTypeID(cf_val)); \ | |
171 return cf_val; \ | |
172 } \ | |
173 } \ | |
174 } \ | |
135 // List of toll-free bridged types taken from: | 175 // List of toll-free bridged types taken from: |
136 // http://www.cocoadev.com/index.pl?TollFreeBridged | 176 // http://www.cocoadev.com/index.pl?TollFreeBridged |
137 | 177 |
138 CF_TO_NS_CAST(CFArrayRef, NSArray); | 178 CF_TO_NS_MUTABLE_CAST(Array); |
139 CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); | 179 CF_TO_NS_MUTABLE_CAST(AttributedString); |
140 CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); | 180 CF_TO_NS_CAST(CFCalendar, NSCalendar); |
141 CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); | 181 CF_TO_NS_MUTABLE_CAST(CharacterSet); |
142 CF_TO_NS_CAST(CFCalendarRef, NSCalendar); | 182 CF_TO_NS_MUTABLE_CAST(Data); |
143 CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); | 183 CF_TO_NS_CAST(CFDate, NSDate); |
144 CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); | 184 CF_TO_NS_MUTABLE_CAST(Dictionary); |
145 CF_TO_NS_CAST(CFDataRef, NSData); | 185 CF_TO_NS_CAST(CFError, NSError); |
146 CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); | 186 CF_TO_NS_CAST(CFLocale, NSLocale); |
147 CF_TO_NS_CAST(CFDateRef, NSDate); | 187 CF_TO_NS_CAST(CFNumber, NSNumber); |
148 CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); | 188 CF_TO_NS_CAST(CFRunLoopTimer, NSTimer); |
149 CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); | 189 CF_TO_NS_CAST(CFTimeZone, NSTimeZone); |
150 CF_TO_NS_CAST(CFNumberRef, NSNumber); | 190 CF_TO_NS_MUTABLE_CAST(Set); |
151 CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); | 191 CF_TO_NS_CAST(CFReadStream, NSInputStream); |
152 CF_TO_NS_CAST(CFSetRef, NSSet); | 192 CF_TO_NS_CAST(CFWriteStream, NSOutputStream); |
153 CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); | 193 CF_TO_NS_MUTABLE_CAST(String); |
154 CF_TO_NS_CAST(CFStringRef, NSString); | 194 CF_TO_NS_CAST(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 | 195 |
161 #endif // __OBJC__ | 196 // Stream operations for CFTypes. They can be used with NSTypes as well |
162 | 197 // by using the NSToCFCast methods above. |
163 } // namespace mac | 198 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
164 } // namespace base | 199 // Operator << can not be overloaded for ObjectiveC types as the compiler |
200 // can not distinguish between overloads for id with overloads for void*. | |
201 extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err); | |
202 extern std::ostream& operator<<(std::ostream& o, const CFStringRef str); | |
165 | 203 |
166 #endif // BASE_MAC_MAC_UTIL_H_ | 204 #endif // BASE_MAC_MAC_UTIL_H_ |
OLD | NEW |