OLD | NEW |
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 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 TYPE_NAME_FOR_CF_TYPE_DECL(CFData); | 106 TYPE_NAME_FOR_CF_TYPE_DECL(CFData); |
107 TYPE_NAME_FOR_CF_TYPE_DECL(CFDate); | 107 TYPE_NAME_FOR_CF_TYPE_DECL(CFDate); |
108 TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary); | 108 TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary); |
109 TYPE_NAME_FOR_CF_TYPE_DECL(CFNull); | 109 TYPE_NAME_FOR_CF_TYPE_DECL(CFNull); |
110 TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber); | 110 TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber); |
111 TYPE_NAME_FOR_CF_TYPE_DECL(CFSet); | 111 TYPE_NAME_FOR_CF_TYPE_DECL(CFSet); |
112 TYPE_NAME_FOR_CF_TYPE_DECL(CFString); | 112 TYPE_NAME_FOR_CF_TYPE_DECL(CFString); |
113 | 113 |
114 #undef TYPE_NAME_FOR_CF_TYPE_DECL | 114 #undef TYPE_NAME_FOR_CF_TYPE_DECL |
115 | 115 |
116 // Helper function for GetValueFromDictionary to create the error message | |
117 // that appears when a type mismatch is encountered. | |
118 std::string GetValueFromDictionaryErrorMessage( | |
119 CFStringRef key, const std::string& expected_type, CFTypeRef value); | |
120 | |
121 // Utility function to pull out a value from a dictionary, check its type, and | |
122 // return it. Returns NULL if the key is not present or of the wrong type. | |
123 // This is now deprecated in favor of the two-argument form below. | |
124 // TODO(kushi.p): Remove this function once all cases of it have been | |
125 // replaced with the two-argument form below. See: crbug.com/104200. | |
126 BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, | |
127 CFStringRef key, | |
128 CFTypeID expected_type); | |
129 | |
130 // Retain/release calls for memory management in C++. | 116 // Retain/release calls for memory management in C++. |
131 BASE_EXPORT void NSObjectRetain(void* obj); | 117 BASE_EXPORT void NSObjectRetain(void* obj); |
132 BASE_EXPORT void NSObjectRelease(void* obj); | 118 BASE_EXPORT void NSObjectRelease(void* obj); |
133 | 119 |
134 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation | 120 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation |
135 // object (one derived from CFTypeRef) to the Foundation memory management | 121 // object (one derived from CFTypeRef) to the Foundation memory management |
136 // system. In a traditional managed-memory environment, cf_object is | 122 // system. In a traditional managed-memory environment, cf_object is |
137 // autoreleased and returned as an NSObject. In a garbage-collected | 123 // autoreleased and returned as an NSObject. In a garbage-collected |
138 // environment, cf_object is marked as eligible for garbage collection. | 124 // environment, cf_object is marked as eligible for garbage collection. |
139 // | 125 // |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // requested type identifier. If the supplied object is not | 222 // requested type identifier. If the supplied object is not |
237 // compatible with the requested return type, CFCast<>() returns | 223 // compatible with the requested return type, CFCast<>() returns |
238 // NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer | 224 // NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer |
239 // to either variant results in NULL being returned without | 225 // to either variant results in NULL being returned without |
240 // triggering any DCHECK. | 226 // triggering any DCHECK. |
241 // | 227 // |
242 // Example usage: | 228 // Example usage: |
243 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( | 229 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( |
244 // CFArrayGetValueAtIndex(array, index)); | 230 // CFArrayGetValueAtIndex(array, index)); |
245 // | 231 // |
246 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( | 232 // CFTypeRef hello = CFSTR("hello world"); |
247 // base::mac::GetValueFromDictionary(some_dict, | 233 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello); |
248 // CFSTR("a_key"), | |
249 // CFStringGetTypeID())); | |
250 BASE_EXPORT template<typename T> | 234 BASE_EXPORT template<typename T> |
251 T CFCast(const CFTypeRef& cf_val); | 235 T CFCast(const CFTypeRef& cf_val); |
252 | 236 |
253 BASE_EXPORT template<typename T> | 237 BASE_EXPORT template<typename T> |
254 T CFCastStrict(const CFTypeRef& cf_val); | 238 T CFCastStrict(const CFTypeRef& cf_val); |
255 | 239 |
256 #if defined(__OBJC__) | 240 #if defined(__OBJC__) |
257 | 241 |
258 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more | 242 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more |
259 // specific (NSObject-derived) type. The compatibility of the passed | 243 // specific (NSObject-derived) type. The compatibility of the passed |
(...skipping 27 matching lines...) Expand all Loading... |
287 | 271 |
288 BASE_EXPORT template<typename T> | 272 BASE_EXPORT template<typename T> |
289 T* ObjCCastStrict(id objc_val) { | 273 T* ObjCCastStrict(id objc_val) { |
290 T* rv = ObjCCast<T>(objc_val); | 274 T* rv = ObjCCast<T>(objc_val); |
291 DCHECK(objc_val == nil || rv); | 275 DCHECK(objc_val == nil || rv); |
292 return rv; | 276 return rv; |
293 } | 277 } |
294 | 278 |
295 #endif // defined(__OBJC__) | 279 #endif // defined(__OBJC__) |
296 | 280 |
| 281 // Helper function for GetValueFromDictionary to create the error message |
| 282 // that appears when a type mismatch is encountered. |
| 283 std::string GetValueFromDictionaryErrorMessage( |
| 284 CFStringRef key, const std::string& expected_type, CFTypeRef value); |
| 285 |
297 // Utility function to pull out a value from a dictionary, check its type, and | 286 // Utility function to pull out a value from a dictionary, check its type, and |
298 // return it. Returns NULL if the key is not present or of the wrong type. | 287 // return it. Returns NULL if the key is not present or of the wrong type. |
299 BASE_EXPORT template<typename T> | 288 BASE_EXPORT template<typename T> |
300 T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) { | 289 T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) { |
301 CFTypeRef value = CFDictionaryGetValue(dict, key); | 290 CFTypeRef value = CFDictionaryGetValue(dict, key); |
302 T value_specific = CFCast<T>(value); | 291 T value_specific = CFCast<T>(value); |
303 | 292 |
304 if (value && !value_specific) { | 293 if (value && !value_specific) { |
305 std::string expected_type = TypeNameForCFType(value_specific); | 294 std::string expected_type = TypeNameForCFType(value_specific); |
306 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key, | 295 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key, |
307 expected_type, | 296 expected_type, |
308 value); | 297 value); |
309 } | 298 } |
310 | 299 |
311 return value_specific; | 300 return value_specific; |
312 } | 301 } |
313 | 302 |
314 } // namespace mac | 303 } // namespace mac |
315 } // namespace base | 304 } // namespace base |
316 | 305 |
317 // Stream operations for CFTypes. They can be used with NSTypes as well | 306 // Stream operations for CFTypes. They can be used with NSTypes as well |
318 // by using the NSToCFCast methods above. | 307 // by using the NSToCFCast methods above. |
319 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); | 308 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
320 // Operator << can not be overloaded for ObjectiveC types as the compiler | 309 // Operator << can not be overloaded for ObjectiveC types as the compiler |
321 // can not distinguish between overloads for id with overloads for void*. | 310 // can not distinguish between overloads for id with overloads for void*. |
322 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 311 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
323 const CFErrorRef err); | 312 const CFErrorRef err); |
324 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, | 313 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
325 const CFStringRef str); | 314 const CFStringRef str); |
326 | 315 |
327 #endif // BASE_MAC_FOUNDATION_UTIL_H_ | 316 #endif // BASE_MAC_FOUNDATION_UTIL_H_ |
OLD | NEW |