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

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: Add a TypeNameForCFType function 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.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..93e6badce17072fd91b4c173de54e8926bc3cb3b 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,6 +97,26 @@ 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 cfo);
+
+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);
+
+// Helper function for GetValueFromDictionary to create the error message
+// that appears when a type mismatch is encountered.
+std::string GetValueFromDictionaryErrorMessage(CFStringRef key,
+ CFStringRef expected_type_ref,
+ 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.
BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
@@ -218,10 +239,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 +269,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 +277,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 +286,28 @@ 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.
+// Is a cleaner implementation of base::mac::GetValueFromDictionary() above.
+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) {
+ // TODO: Get the string value of the type name T.
+ std::string type_name = TypeNameForCFType(value_specific);
+ ScopedCFTypeRef<CFStringRef> expected_type_ref(CFStringCreateWithCString(
Mark Mentovai 2011/11/14 18:08:55 Why are you turning this into a CFStringRef, when
+ kCFAllocatorDefault,
+ type_name.c_str(),
+ kCFStringEncodingUTF8));
+ DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
+ expected_type_ref,
+ 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.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698