Chromium Code Reviews| 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_FOUNDATION_UTIL_H_ | 5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_ |
| 6 #define BASE_MAC_FOUNDATION_UTIL_H_ | 6 #define BASE_MAC_FOUNDATION_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
| 10 | 10 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( | 217 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( |
| 218 // base::mac::GetValueFromDictionary(some_dict, | 218 // base::mac::GetValueFromDictionary(some_dict, |
| 219 // CFSTR("a_key"), | 219 // CFSTR("a_key"), |
| 220 // CFStringGetTypeID())); | 220 // CFStringGetTypeID())); |
| 221 BASE_EXPORT template<class T> | 221 BASE_EXPORT template<class T> |
| 222 T CFCast(const CFTypeRef& cf_val); | 222 T CFCast(const CFTypeRef& cf_val); |
| 223 | 223 |
| 224 BASE_EXPORT template<class T> | 224 BASE_EXPORT template<class T> |
| 225 T CFCastStrict(const CFTypeRef& cf_val); | 225 T CFCastStrict(const CFTypeRef& cf_val); |
| 226 | 226 |
| 227 #if defined(__OBJC__) | |
| 228 | |
| 229 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more | |
| 230 // specific (NSObject-derived) type. The compatibility of the passed | |
| 231 // object is found by checking if it's a kind of the requested type | |
| 232 // identifier. If the supplied object is not compatible with the | |
| 233 // requested return type, ObjCCast<>() returns nil and | |
| 234 // ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either | |
| 235 // variant results in nil being returned without triggering any DCHECK. | |
| 236 // | |
| 237 // The strict variant is useful when retrieving a value that you | |
| 238 // stuffed into a collection, where you know you’ve only stuffed | |
|
Mark Mentovai
2011/10/23 16:05:24
Your copy-and-paste job of my e-mail has resulted
| |
| 239 // values of a specific type. For example, an NSArray of NSStrings. | |
| 240 // The non-strict variant is handy when retrieving values from data | |
| 241 // that aren't fully controlled yourself. For example, a plist read | |
|
Mark Mentovai
2011/10/23 16:05:24
“data that aren’t fully controlled yourself” is no
| |
| 242 // from disk may be beyond your exclusive control, so you’d merely | |
| 243 // want to check that the values you retrieve from it are of the expected | |
| 244 // types, but not crash if they’re not. | |
| 245 // | |
| 246 // Example usage: | |
| 247 // NSString* version = base::mac::ObjCCast<NSString>( | |
| 248 // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]); | |
| 249 // | |
| 250 // NSString* str = base::mac::ObjCCastStrict<NSString>( | |
| 251 // [ns_arr_of_ns_strs objectAtIndex:0]); | |
| 252 BASE_EXPORT template<class T> | |
| 253 T* ObjCCast(id objc_val) { | |
| 254 if ([objc_val isKindOfClass:[T class]]) { | |
| 255 return reinterpret_cast<T*>(objc_val); | |
| 256 } | |
| 257 return nil; | |
| 258 } | |
| 259 | |
| 260 BASE_EXPORT template<class T> | |
| 261 T* ObjCCastStrict(id objc_val) { | |
| 262 T* rv = ObjCCast<T>(objc_val); | |
| 263 DCHECK(objc_val == nil || rv); | |
| 264 return rv; | |
| 265 } | |
| 266 | |
| 267 #endif // defined(__OBJC__) | |
| 268 | |
| 227 } // namespace mac | 269 } // namespace mac |
| 228 } // namespace base | 270 } // namespace base |
| 229 | 271 |
| 230 // Stream operations for CFTypes. They can be used with NSTypes as well | 272 // Stream operations for CFTypes. They can be used with NSTypes as well |
| 231 // by using the NSToCFCast methods above. | 273 // by using the NSToCFCast methods above. |
| 232 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); | 274 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
| 233 // Operator << can not be overloaded for ObjectiveC types as the compiler | 275 // Operator << can not be overloaded for ObjectiveC types as the compiler |
| 234 // can not distinguish between overloads for id with overloads for void*. | 276 // can not distinguish between overloads for id with overloads for void*. |
| 235 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 277 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 236 const CFErrorRef err); | 278 const CFErrorRef err); |
| 237 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 279 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 238 const CFStringRef str); | 280 const CFStringRef str); |
| 239 | 281 |
| 240 #endif // BASE_MAC_FOUNDATION_UTIL_H_ | 282 #endif // BASE_MAC_FOUNDATION_UTIL_H_ |
| OLD | NEW |