Chromium Code Reviews| Index: base/mac/foundation_util.h |
| diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h |
| index f7821de08cdb09bca58aaca59da82bd689407e29..5f1b5800eb45370dddba2d97e3572074a2f8ee26 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,31 @@ 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 in favor of the two-argument form below. |
| +// TODO(kushi.p) |
|
Mark Mentovai
2011/11/14 22:11:37
Please finish this comment by referencing your new
|
| BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, |
| CFStringRef key, |
| CFTypeID expected_type); |
| @@ -198,6 +222,9 @@ CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); |
| CF_TO_NS_MUTABLE_CAST_DECL(String); |
| CF_TO_NS_CAST_DECL(CFURL, NSURL); |
| +#undef CF_TO_NS_CAST_DECL |
| +#undef CF_TO_NS_MUTABLE_CAST_DECL |
|
Mark Mentovai
2011/11/14 22:13:21
I said three, right?
How about OBJC_CPP_CLASS_DEC
KushalP
2011/11/14 22:20:47
Ah, thought you just mean this 10 line block here.
|
| + |
| namespace base { |
| namespace mac { |
| @@ -218,10 +245,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 +275,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 +283,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 +292,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 |