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..dc1252f0642950c50e018a29bd1315bb5a80c0b2 100644 |
| --- a/base/mac/foundation_util.h |
| +++ b/base/mac/foundation_util.h |
| @@ -224,6 +224,43 @@ 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 |
|
Mark Mentovai
2011/10/19 22:59:32
Tiny nit: NSObject-derived, with a hyphen in there
|
| +// 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 |
| +// 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>( |
|
Mark Mentovai
2011/10/19 22:59:32
NSNumber* for the type in the declaration, right?
|
| +// [dict objectForKey:@"object"]); |
| +// |
| +// 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:
|
| +BASE_EXPORT template<class T> |
| +T* ObjCCast(id objc_val) { |
| + 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:
|
| + return NULL; |
| + } |
| + |
| + if ([objc_val isKindOfClass:[T class]]) { |
| + return reinterpret_cast<T*>(objc_val); |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +BASE_EXPORT template<class T> |
| +T* ObjCCastStrict(id objc_val) { |
| + T* rv = ObjCCast<T>(objc_val); |
| + DCHECK(objc_val == NULL || rv); |
| + return rv; |
| +} |
| + |
| +#endif |
|
Mark Mentovai
2011/10/19 22:59:32
#endif // defined(__OBJC__)
|
| + |
| } // namespace mac |
| } // namespace base |