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

Side by Side Diff: base/mac/foundation_util.h

Issue 1927003004: Mac: Introduce "StaticCast" variants of base::mac::FooCastStrict, which do not typecheck in Release (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 7 months 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
« 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 7
8 #include <CoreFoundation/CoreFoundation.h> 8 #include <CoreFoundation/CoreFoundation.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>( 265 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>(
266 // CFArrayGetValueAtIndex(array, index)); 266 // CFArrayGetValueAtIndex(array, index));
267 // 267 //
268 // CFTypeRef hello = CFSTR("hello world"); 268 // CFTypeRef hello = CFSTR("hello world");
269 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello); 269 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello);
270 270
271 template<typename T> 271 template<typename T>
272 T CFCast(const CFTypeRef& cf_val); 272 T CFCast(const CFTypeRef& cf_val);
273 273
274 template<typename T> 274 template<typename T>
275 T CFCastStrict(const CFTypeRef& cf_val); 275 T CFCastStrict(const CFTypeRef& cf_val) {
276 DCHECK(!cf_val || CFCast<T>(cf_val));
277 // Note: CFTypeRef is const void*, but CGColorRef and some others are non-
278 // const. So this may also cast away a const qualifier.
279 return (T)(cf_val);
280 }
276 281
277 #define CF_CAST_DECL(TypeCF) \ 282 #define CF_CAST_DECL(TypeCF) \
278 template<> BASE_EXPORT TypeCF##Ref \ 283 template<> BASE_EXPORT TypeCF##Ref \
279 CFCast<TypeCF##Ref>(const CFTypeRef& cf_val);\ 284 CFCast<TypeCF##Ref>(const CFTypeRef& cf_val);
280 \
281 template<> BASE_EXPORT TypeCF##Ref \
282 CFCastStrict<TypeCF##Ref>(const CFTypeRef& cf_val);
283 285
284 CF_CAST_DECL(CFArray); 286 CF_CAST_DECL(CFArray);
285 CF_CAST_DECL(CFBag); 287 CF_CAST_DECL(CFBag);
286 CF_CAST_DECL(CFBoolean); 288 CF_CAST_DECL(CFBoolean);
287 CF_CAST_DECL(CFData); 289 CF_CAST_DECL(CFData);
288 CF_CAST_DECL(CFDate); 290 CF_CAST_DECL(CFDate);
289 CF_CAST_DECL(CFDictionary); 291 CF_CAST_DECL(CFDictionary);
290 CF_CAST_DECL(CFNull); 292 CF_CAST_DECL(CFNull);
291 CF_CAST_DECL(CFNumber); 293 CF_CAST_DECL(CFNumber);
292 CF_CAST_DECL(CFSet); 294 CF_CAST_DECL(CFSet);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 template<typename T> 334 template<typename T>
333 T* ObjCCast(id objc_val) { 335 T* ObjCCast(id objc_val) {
334 if ([objc_val isKindOfClass:[T class]]) { 336 if ([objc_val isKindOfClass:[T class]]) {
335 return reinterpret_cast<T*>(objc_val); 337 return reinterpret_cast<T*>(objc_val);
336 } 338 }
337 return nil; 339 return nil;
338 } 340 }
339 341
340 template<typename T> 342 template<typename T>
341 T* ObjCCastStrict(id objc_val) { 343 T* ObjCCastStrict(id objc_val) {
342 T* rv = ObjCCast<T>(objc_val); 344 DCHECK(!objc_val || ObjCCast<T>(objc_val));
343 DCHECK(objc_val == nil || rv); 345 return reinterpret_cast<T*>(objc_val);
344 return rv;
345 } 346 }
346 347
347 #endif // defined(__OBJC__) 348 #endif // defined(__OBJC__)
348 349
349 // Helper function for GetValueFromDictionary to create the error message 350 // Helper function for GetValueFromDictionary to create the error message
350 // that appears when a type mismatch is encountered. 351 // that appears when a type mismatch is encountered.
351 BASE_EXPORT std::string GetValueFromDictionaryErrorMessage( 352 BASE_EXPORT std::string GetValueFromDictionaryErrorMessage(
352 CFStringRef key, const std::string& expected_type, CFTypeRef value); 353 CFStringRef key, const std::string& expected_type, CFTypeRef value);
353 354
354 // Utility function to pull out a value from a dictionary, check its type, and 355 // Utility function to pull out a value from a dictionary, check its type, and
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 // by using the NSToCFCast methods above. 390 // by using the NSToCFCast methods above.
390 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); 391 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
391 // Operator << can not be overloaded for ObjectiveC types as the compiler 392 // Operator << can not be overloaded for ObjectiveC types as the compiler
392 // can not distinguish between overloads for id with overloads for void*. 393 // can not distinguish between overloads for id with overloads for void*.
393 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 394 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
394 const CFErrorRef err); 395 const CFErrorRef err);
395 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 396 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
396 const CFStringRef str); 397 const CFStringRef str);
397 398
398 #endif // BASE_MAC_FOUNDATION_UTIL_H_ 399 #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