Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2585)

Unified Diff: base/mac/foundation_util.h

Issue 8356024: Create ObjCCast<>() and ObjCCastStrict<>() methods (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add colloquial *Strict docs prose Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/mac/foundation_util_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/mac/foundation_util.h
diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h
index 4c4abc69dbade923e49adf927d49fe3dcc4721e4..5c0225e29e2a5c29de0106815726ffe971a94830 100644
--- a/base/mac/foundation_util.h
+++ b/base/mac/foundation_util.h
@@ -224,6 +224,48 @@ 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 that you
+// stuffed into a collection, where you know you’ve only stuffed
Mark Mentovai 2011/10/23 16:05:24 Your copy-and-paste job of my e-mail has resulted
+// values of a specific type. For example, an NSArray of NSStrings.
+// The non-strict variant is handy when retrieving values from data
+// that aren't fully controlled yourself. For example, a plist read
Mark Mentovai 2011/10/23 16:05:24 “data that aren’t fully controlled yourself” is no
+// from disk may be beyond your exclusive control, so you’d merely
+// 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
« no previous file with comments | « no previous file | base/mac/foundation_util_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698