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

Unified Diff: base/mac/foundation_util.h

Issue 8540021: Create a nicer interface for base::mac::GetValueFromDictionary (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: undefs, alignment, ScopedCFTypeRefs Created 9 years, 1 month 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.mm » ('j') | base/mac/foundation_util_unittest.mm » ('J')
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 f7821de08cdb09bca58aaca59da82bd689407e29..87c8dc74e6accefeb50d99c59aa83ff29c87a7d9 100644
--- a/base/mac/foundation_util.h
+++ b/base/mac/foundation_util.h
@@ -13,6 +13,7 @@
#include "base/base_export.h"
#include "base/logging.h"
+#include "base/mac/scoped_cftyperef.h"
#if defined(__OBJC__)
#import <Foundation/Foundation.h>
@@ -96,8 +97,30 @@ BASE_EXPORT FilePath GetUserLibraryPath();
// returns - path to the application bundle, or empty on error
BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name);
+#define TYPE_NAME_FOR_CF_TYPE_DECL(TypeCF) \
+std::string TypeNameForCFType(TypeCF##Ref);
+
+TYPE_NAME_FOR_CF_TYPE_DECL(CFArray);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFBag);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFBoolean);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFData);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFDate);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFNull);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFSet);
+TYPE_NAME_FOR_CF_TYPE_DECL(CFString);
+
+#undef TYPE_NAME_FOR_CF_TYPE_DECL
+
+// Helper function for GetValueFromDictionary to create the error message
+// that appears when a type mismatch is encountered.
+std::string GetValueFromDictionaryErrorMessage(
+ CFStringRef key, const std::string& expected_type, CFTypeRef value);
+
// Utility function to pull out a value from a dictionary, check its type, and
// return it. Returns NULL if the key is not present or of the wrong type.
+// This is now deprecated.
Mark Mentovai 2011/11/14 21:57:18 This is now deprecated in favor of the two-argumen
BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
CFStringRef key,
CFTypeID expected_type);
@@ -218,10 +241,10 @@ namespace mac {
// base::mac::GetValueFromDictionary(some_dict,
// CFSTR("a_key"),
// CFStringGetTypeID()));
-BASE_EXPORT template<class T>
+BASE_EXPORT template<typename T>
T CFCast(const CFTypeRef& cf_val);
-BASE_EXPORT template<class T>
+BASE_EXPORT template<typename T>
T CFCastStrict(const CFTypeRef& cf_val);
#if defined(__OBJC__)
@@ -248,7 +271,7 @@ T CFCastStrict(const CFTypeRef& cf_val);
//
// NSString* str = base::mac::ObjCCastStrict<NSString>(
// [ns_arr_of_ns_strs objectAtIndex:0]);
-BASE_EXPORT template<class T>
+BASE_EXPORT template<typename T>
T* ObjCCast(id objc_val) {
if ([objc_val isKindOfClass:[T class]]) {
return reinterpret_cast<T*>(objc_val);
@@ -256,7 +279,7 @@ T* ObjCCast(id objc_val) {
return nil;
}
-BASE_EXPORT template<class T>
+BASE_EXPORT template<typename T>
T* ObjCCastStrict(id objc_val) {
T* rv = ObjCCast<T>(objc_val);
DCHECK(objc_val == nil || rv);
@@ -265,6 +288,23 @@ T* ObjCCastStrict(id objc_val) {
#endif // defined(__OBJC__)
+// Utility function to pull out a value from a dictionary, check its type, and
+// return it. Returns NULL if the key is not present or of the wrong type.
+BASE_EXPORT template<typename T>
+T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) {
+ CFTypeRef value = CFDictionaryGetValue(dict, key);
+ T value_specific = CFCast<T>(value);
+
+ if (value && !value_specific) {
+ std::string expected_type = TypeNameForCFType(value_specific);
+ DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
+ expected_type,
+ value);
+ }
+
+ return value_specific;
+}
+
} // namespace mac
} // namespace base
« no previous file with comments | « no previous file | base/mac/foundation_util.mm » ('j') | base/mac/foundation_util_unittest.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698