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 | |
|
Mark Mentovai
2011/10/19 22:59:32
Tiny nit: NSObject-derived, with a hyphen in there
| |
| 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 NULL and | |
| 234 // ObjCCastStrict<>() will DCHECK. Providing a NULL pointer to either | |
| 235 // variant results in NULL being returned without triggering any DCHECK. | |
| 236 // | |
| 237 // Example usage: | |
| 238 // NSNumber some_number = base::mac::ObjCCast<NSNumber>( | |
|
Mark Mentovai
2011/10/19 22:59:32
NSNumber* for the type in the declaration, right?
| |
| 239 // [dict objectForKey:@"object"]); | |
| 240 // | |
| 241 // TODO: Write a good example for ObjCCastStrict<>() | |
|
Mark Mentovai
2011/10/19 22:59:32
The strict variant is handy when you’re retrieving
KushalP
2011/10/19 23:25:24
All of this is incredibly useful. Should I also ad
KushalP
2011/10/20 14:59:38
You didn't state whether I should add what you sai
Mark Mentovai
2011/10/21 14:30:54
KushalP wrote:
| |
| 242 BASE_EXPORT template<class T> | |
| 243 T* ObjCCast(id objc_val) { | |
| 244 if (objc_val == NULL) { | |
|
Mark Mentovai
2011/10/19 22:59:32
Use nil for Objective-C. All of your uses of NULL
Mark Mentovai
2011/10/19 22:59:32
Neat trick: you can remove this condition entirely
KushalP
2011/10/19 23:25:24
That is very cool!
A quick google for "nil isKind
Mark Mentovai
2011/10/20 14:14:16
KushalP wrote:
| |
| 245 return NULL; | |
| 246 } | |
| 247 | |
| 248 if ([objc_val isKindOfClass:[T class]]) { | |
| 249 return reinterpret_cast<T*>(objc_val); | |
| 250 } | |
| 251 | |
| 252 return NULL; | |
| 253 } | |
| 254 | |
| 255 BASE_EXPORT template<class T> | |
| 256 T* ObjCCastStrict(id objc_val) { | |
| 257 T* rv = ObjCCast<T>(objc_val); | |
| 258 DCHECK(objc_val == NULL || rv); | |
| 259 return rv; | |
| 260 } | |
| 261 | |
| 262 #endif | |
|
Mark Mentovai
2011/10/19 22:59:32
#endif // defined(__OBJC__)
| |
| 263 | |
| 227 } // namespace mac | 264 } // namespace mac |
| 228 } // namespace base | 265 } // namespace base |
| 229 | 266 |
| 230 // Stream operations for CFTypes. They can be used with NSTypes as well | 267 // Stream operations for CFTypes. They can be used with NSTypes as well |
| 231 // by using the NSToCFCast methods above. | 268 // by using the NSToCFCast methods above. |
| 232 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); | 269 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
| 233 // Operator << can not be overloaded for ObjectiveC types as the compiler | 270 // Operator << can not be overloaded for ObjectiveC types as the compiler |
| 234 // can not distinguish between overloads for id with overloads for void*. | 271 // can not distinguish between overloads for id with overloads for void*. |
| 235 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 272 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 236 const CFErrorRef err); | 273 const CFErrorRef err); |
| 237 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 274 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 238 const CFStringRef str); | 275 const CFStringRef str); |
| 239 | 276 |
| 240 #endif // BASE_MAC_FOUNDATION_UTIL_H_ | 277 #endif // BASE_MAC_FOUNDATION_UTIL_H_ |
| OLD | NEW |