Chromium Code Reviews| Index: base/mac/foundation_util.h |
| diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h |
| index 4c4abc69dbade923e49adf927d49fe3dcc4721e4..c9bb7dbe4a46100767a06928d3cdcc11a0c1d1f7 100644 |
| --- a/base/mac/foundation_util.h |
| +++ b/base/mac/foundation_util.h |
| @@ -224,6 +224,39 @@ T CFCast(const CFTypeRef& cf_val); |
| BASE_EXPORT template<class T> |
| T CFCastStrict(const CFTypeRef& cf_val); |
| +#if defined(__OBJC__) |
| + |
| +// ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more |
| +// specific (NSObject-derived) type. The compatibility of the passed |
| +// object is found by checking if it's a kind of the requested type |
| +// identifier. If the supplied object is not compatible with the |
| +// requested return type, ObjCCast<>() returns NULL and |
|
Mark Mentovai
2011/10/20 14:14:16
Adjust the comment to reference nil instead of NUL
|
| +// ObjCCastStrict<>() will DCHECK. Providing a NULL pointer to either |
| +// variant results in NULL being returned without triggering any DCHECK. |
| +// |
| +// Example usage: |
| +// NSNumber* some_number = base::mac::ObjCCast<NSNumber>( |
| +// [dict objectForKey:@"object"]); |
|
Mark Mentovai
2011/10/20 14:14:16
A good example for this would be:
NSString* versi
|
| +// |
| +// NSString* some_str = base::mac::ObjCCastStrict<NSString>( |
| +// [ns_arr_of_ns_strs objectAtIndex:0]); |
| +BASE_EXPORT template<class T> |
| +T* ObjCCast(id objc_val) { |
| + if ([objc_val isKindOfClass:[T class]]) { |
| + return reinterpret_cast<T*>(objc_val); |
| + } |
| + return nil; |
| +} |
| + |
| +BASE_EXPORT template<class T> |
| +T* ObjCCastStrict(id objc_val) { |
| + T* rv = ObjCCast<T>(objc_val); |
| + DCHECK(objc_val == nil || rv); |
| + return rv; |
| +} |
| + |
| +#endif // defined(__OBJC__) |
| + |
| } // namespace mac |
| } // namespace base |