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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/mac/foundation_util.mm » ('j') | base/mac/foundation_util.mm » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_ 5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_
6 #define BASE_MAC_FOUNDATION_UTIL_H_ 6 #define BASE_MAC_FOUNDATION_UTIL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <CoreFoundation/CoreFoundation.h> 9 #include <CoreFoundation/CoreFoundation.h>
10 10
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/base_export.h" 14 #include "base/base_export.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/mac/scoped_cftyperef.h"
16 17
17 #if defined(__OBJC__) 18 #if defined(__OBJC__)
18 #import <Foundation/Foundation.h> 19 #import <Foundation/Foundation.h>
19 #else // __OBJC__ 20 #else // __OBJC__
20 class NSBundle; 21 class NSBundle;
21 class NSString; 22 class NSString;
22 #endif // __OBJC__ 23 #endif // __OBJC__
23 24
24 class FilePath; 25 class FilePath;
25 26
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // Returns the ~/Library directory. 90 // Returns the ~/Library directory.
90 BASE_EXPORT FilePath GetUserLibraryPath(); 91 BASE_EXPORT FilePath GetUserLibraryPath();
91 92
92 // Takes a path to an (executable) binary and tries to provide the path to an 93 // Takes a path to an (executable) binary and tries to provide the path to an
93 // application bundle containing it. It takes the outermost bundle that it can 94 // application bundle containing it. It takes the outermost bundle that it can
94 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). 95 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app").
95 // |exec_name| - path to the binary 96 // |exec_name| - path to the binary
96 // returns - path to the application bundle, or empty on error 97 // returns - path to the application bundle, or empty on error
97 BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name); 98 BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name);
98 99
100 #define TYPE_NAME_FOR_CF_TYPE_DECL(TypeCF) \
101 std::string TypeNameForCFType(TypeCF##Ref cfo);
102
103 TYPE_NAME_FOR_CF_TYPE_DECL(CFArray);
104 TYPE_NAME_FOR_CF_TYPE_DECL(CFBag);
105 TYPE_NAME_FOR_CF_TYPE_DECL(CFBoolean);
106 TYPE_NAME_FOR_CF_TYPE_DECL(CFData);
107 TYPE_NAME_FOR_CF_TYPE_DECL(CFDate);
108 TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary);
109 TYPE_NAME_FOR_CF_TYPE_DECL(CFNull);
110 TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber);
111 TYPE_NAME_FOR_CF_TYPE_DECL(CFSet);
112 TYPE_NAME_FOR_CF_TYPE_DECL(CFString);
113
114 // Helper function for GetValueFromDictionary to create the error message
115 // that appears when a type mismatch is encountered.
116 std::string GetValueFromDictionaryErrorMessage(CFStringRef key,
117 CFStringRef expected_type_ref,
118 CFTypeRef value);
119
99 // Utility function to pull out a value from a dictionary, check its type, and 120 // Utility function to pull out a value from a dictionary, check its type, and
100 // return it. Returns NULL if the key is not present or of the wrong type. 121 // return it. Returns NULL if the key is not present or of the wrong type.
101 BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, 122 BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
102 CFStringRef key, 123 CFStringRef key,
103 CFTypeID expected_type); 124 CFTypeID expected_type);
104 125
105 // Retain/release calls for memory management in C++. 126 // Retain/release calls for memory management in C++.
106 BASE_EXPORT void NSObjectRetain(void* obj); 127 BASE_EXPORT void NSObjectRetain(void* obj);
107 BASE_EXPORT void NSObjectRelease(void* obj); 128 BASE_EXPORT void NSObjectRelease(void* obj);
108 129
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // triggering any DCHECK. 232 // triggering any DCHECK.
212 // 233 //
213 // Example usage: 234 // Example usage:
214 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( 235 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>(
215 // CFArrayGetValueAtIndex(array, index)); 236 // CFArrayGetValueAtIndex(array, index));
216 // 237 //
217 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( 238 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(
218 // base::mac::GetValueFromDictionary(some_dict, 239 // base::mac::GetValueFromDictionary(some_dict,
219 // CFSTR("a_key"), 240 // CFSTR("a_key"),
220 // CFStringGetTypeID())); 241 // CFStringGetTypeID()));
221 BASE_EXPORT template<class T> 242 BASE_EXPORT template<typename T>
222 T CFCast(const CFTypeRef& cf_val); 243 T CFCast(const CFTypeRef& cf_val);
223 244
224 BASE_EXPORT template<class T> 245 BASE_EXPORT template<typename T>
225 T CFCastStrict(const CFTypeRef& cf_val); 246 T CFCastStrict(const CFTypeRef& cf_val);
226 247
227 #if defined(__OBJC__) 248 #if defined(__OBJC__)
228 249
229 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more 250 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more
230 // specific (NSObject-derived) type. The compatibility of the passed 251 // specific (NSObject-derived) type. The compatibility of the passed
231 // object is found by checking if it's a kind of the requested type 252 // object is found by checking if it's a kind of the requested type
232 // identifier. If the supplied object is not compatible with the 253 // identifier. If the supplied object is not compatible with the
233 // requested return type, ObjCCast<>() returns nil and 254 // requested return type, ObjCCast<>() returns nil and
234 // ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either 255 // ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either
235 // variant results in nil being returned without triggering any DCHECK. 256 // variant results in nil being returned without triggering any DCHECK.
236 // 257 //
237 // The strict variant is useful when retrieving a value from a 258 // The strict variant is useful when retrieving a value from a
238 // collection which only has values of a specific type, e.g. an 259 // collection which only has values of a specific type, e.g. an
239 // NSArray of NSStrings. The non-strict variant is useful when 260 // NSArray of NSStrings. The non-strict variant is useful when
240 // retrieving values from data that you can't fully control. For 261 // retrieving values from data that you can't fully control. For
241 // example, a plist read from disk may be beyond your exclusive 262 // example, a plist read from disk may be beyond your exclusive
242 // control, so you'd only want to check that the values you retrieve 263 // control, so you'd only want to check that the values you retrieve
243 // from it are of the expected types, but not crash if they're not. 264 // from it are of the expected types, but not crash if they're not.
244 // 265 //
245 // Example usage: 266 // Example usage:
246 // NSString* version = base::mac::ObjCCast<NSString>( 267 // NSString* version = base::mac::ObjCCast<NSString>(
247 // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]); 268 // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
248 // 269 //
249 // NSString* str = base::mac::ObjCCastStrict<NSString>( 270 // NSString* str = base::mac::ObjCCastStrict<NSString>(
250 // [ns_arr_of_ns_strs objectAtIndex:0]); 271 // [ns_arr_of_ns_strs objectAtIndex:0]);
251 BASE_EXPORT template<class T> 272 BASE_EXPORT template<typename T>
252 T* ObjCCast(id objc_val) { 273 T* ObjCCast(id objc_val) {
253 if ([objc_val isKindOfClass:[T class]]) { 274 if ([objc_val isKindOfClass:[T class]]) {
254 return reinterpret_cast<T*>(objc_val); 275 return reinterpret_cast<T*>(objc_val);
255 } 276 }
256 return nil; 277 return nil;
257 } 278 }
258 279
259 BASE_EXPORT template<class T> 280 BASE_EXPORT template<typename T>
260 T* ObjCCastStrict(id objc_val) { 281 T* ObjCCastStrict(id objc_val) {
261 T* rv = ObjCCast<T>(objc_val); 282 T* rv = ObjCCast<T>(objc_val);
262 DCHECK(objc_val == nil || rv); 283 DCHECK(objc_val == nil || rv);
263 return rv; 284 return rv;
264 } 285 }
265 286
266 #endif // defined(__OBJC__) 287 #endif // defined(__OBJC__)
267 288
289 // Utility function to pull out a value from a dictionary, check its type, and
290 // return it. Returns NULL if the key is not present or of the wrong type.
291 // Is a cleaner implementation of base::mac::GetValueFromDictionary() above.
292 BASE_EXPORT template<typename T>
293 T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) {
294 CFTypeRef value = CFDictionaryGetValue(dict, key);
295 T value_specific = CFCast<T>(value);
296
297 if (value && !value_specific) {
298 // TODO: Get the string value of the type name T.
299 std::string type_name = TypeNameForCFType(value_specific);
300 ScopedCFTypeRef<CFStringRef> expected_type_ref(CFStringCreateWithCString(
Mark Mentovai 2011/11/14 18:08:55 Why are you turning this into a CFStringRef, when
301 kCFAllocatorDefault,
302 type_name.c_str(),
303 kCFStringEncodingUTF8));
304 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
305 expected_type_ref,
306 value);
307 }
308 return value_specific;
309 }
310
268 } // namespace mac 311 } // namespace mac
269 } // namespace base 312 } // namespace base
270 313
271 // Stream operations for CFTypes. They can be used with NSTypes as well 314 // Stream operations for CFTypes. They can be used with NSTypes as well
272 // by using the NSToCFCast methods above. 315 // by using the NSToCFCast methods above.
273 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); 316 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
274 // Operator << can not be overloaded for ObjectiveC types as the compiler 317 // Operator << can not be overloaded for ObjectiveC types as the compiler
275 // can not distinguish between overloads for id with overloads for void*. 318 // can not distinguish between overloads for id with overloads for void*.
276 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 319 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
277 const CFErrorRef err); 320 const CFErrorRef err);
278 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 321 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
279 const CFStringRef str); 322 const CFStringRef str);
280 323
281 #endif // BASE_MAC_FOUNDATION_UTIL_H_ 324 #endif // BASE_MAC_FOUNDATION_UTIL_H_
OLDNEW
« 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