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 from a |
| 238 // collection which only has values of a specific type, e.g. an |
| 239 // NSArray of NSStrings. The non-strict variant is useful when |
| 240 // retrieving values from data that you can't fully control. For |
| 241 // example, a plist read from disk may be beyond your exclusive |
| 242 // control, so you'd only want to check that the values you retrieve |
| 243 // from it are of the expected types, but not crash if they're not. |
| 244 // |
| 245 // Example usage: |
| 246 // NSString* version = base::mac::ObjCCast<NSString>( |
| 247 // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]); |
| 248 // |
| 249 // NSString* str = base::mac::ObjCCastStrict<NSString>( |
| 250 // [ns_arr_of_ns_strs objectAtIndex:0]); |
| 251 BASE_EXPORT template<class T> |
| 252 T* ObjCCast(id objc_val) { |
| 253 if ([objc_val isKindOfClass:[T class]]) { |
| 254 return reinterpret_cast<T*>(objc_val); |
| 255 } |
| 256 return nil; |
| 257 } |
| 258 |
| 259 BASE_EXPORT template<class T> |
| 260 T* ObjCCastStrict(id objc_val) { |
| 261 T* rv = ObjCCast<T>(objc_val); |
| 262 DCHECK(objc_val == nil || rv); |
| 263 return rv; |
| 264 } |
| 265 |
| 266 #endif // defined(__OBJC__) |
| 267 |
227 } // namespace mac | 268 } // namespace mac |
228 } // namespace base | 269 } // namespace base |
229 | 270 |
230 // Stream operations for CFTypes. They can be used with NSTypes as well | 271 // Stream operations for CFTypes. They can be used with NSTypes as well |
231 // by using the NSToCFCast methods above. | 272 // by using the NSToCFCast methods above. |
232 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); | 273 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
233 // Operator << can not be overloaded for ObjectiveC types as the compiler | 274 // Operator << can not be overloaded for ObjectiveC types as the compiler |
234 // can not distinguish between overloads for id with overloads for void*. | 275 // can not distinguish between overloads for id with overloads for void*. |
235 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 276 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
236 const CFErrorRef err); | 277 const CFErrorRef err); |
237 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 278 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
238 const CFStringRef str); | 279 const CFStringRef str); |
239 | 280 |
240 #endif // BASE_MAC_FOUNDATION_UTIL_H_ | 281 #endif // BASE_MAC_FOUNDATION_UTIL_H_ |
OLD | NEW |