| Index: base/mac/foundation_util.h
|
| diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h
|
| index 4c4abc69dbade923e49adf927d49fe3dcc4721e4..f7821de08cdb09bca58aaca59da82bd689407e29 100644
|
| --- a/base/mac/foundation_util.h
|
| +++ b/base/mac/foundation_util.h
|
| @@ -224,6 +224,47 @@ 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 nil and
|
| +// ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either
|
| +// variant results in nil being returned without triggering any DCHECK.
|
| +//
|
| +// The strict variant is useful when retrieving a value from a
|
| +// collection which only has values of a specific type, e.g. an
|
| +// NSArray of NSStrings. The non-strict variant is useful when
|
| +// retrieving values from data that you can't fully control. For
|
| +// example, a plist read from disk may be beyond your exclusive
|
| +// control, so you'd only want to check that the values you retrieve
|
| +// from it are of the expected types, but not crash if they're not.
|
| +//
|
| +// Example usage:
|
| +// NSString* version = base::mac::ObjCCast<NSString>(
|
| +// [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
|
| +//
|
| +// NSString* 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
|
|
|
|
|