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

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

Issue 8356024: Create ObjCCast<>() and ObjCCastStrict<>() methods (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Nils. Fix comments. Add AutoreleasePool. Created 9 years, 2 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 | Annotate | Revision Log
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
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>( 217 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(
218 // base::mac::GetValueFromDictionary(some_dict, 218 // base::mac::GetValueFromDictionary(some_dict,
219 // CFSTR("a_key"), 219 // CFSTR("a_key"),
220 // CFStringGetTypeID())); 220 // CFStringGetTypeID()));
221 BASE_EXPORT template<class T> 221 BASE_EXPORT template<class T>
222 T CFCast(const CFTypeRef& cf_val); 222 T CFCast(const CFTypeRef& cf_val);
223 223
224 BASE_EXPORT template<class T> 224 BASE_EXPORT template<class T>
225 T CFCastStrict(const CFTypeRef& cf_val); 225 T CFCastStrict(const CFTypeRef& cf_val);
226 226
227 #if defined(__OBJC__)
228
229 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more
230 // specific (NSObject-derived) type. The compatibility of the passed
231 // 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
233 // requested return type, ObjCCast<>() returns NULL and
Mark Mentovai 2011/10/20 14:14:16 Adjust the comment to reference nil instead of NUL
234 // ObjCCastStrict<>() will DCHECK. Providing a NULL pointer to either
235 // variant results in NULL being returned without triggering any DCHECK.
236 //
237 // Example usage:
238 // NSNumber* some_number = base::mac::ObjCCast<NSNumber>(
239 // [dict objectForKey:@"object"]);
Mark Mentovai 2011/10/20 14:14:16 A good example for this would be: NSString* versi
240 //
241 // NSString* some_str = base::mac::ObjCCastStrict<NSString>(
242 // [ns_arr_of_ns_strs objectAtIndex:0]);
243 BASE_EXPORT template<class T>
244 T* ObjCCast(id objc_val) {
245 if ([objc_val isKindOfClass:[T class]]) {
246 return reinterpret_cast<T*>(objc_val);
247 }
248 return nil;
249 }
250
251 BASE_EXPORT template<class T>
252 T* ObjCCastStrict(id objc_val) {
253 T* rv = ObjCCast<T>(objc_val);
254 DCHECK(objc_val == nil || rv);
255 return rv;
256 }
257
258 #endif // defined(__OBJC__)
259
227 } // namespace mac 260 } // namespace mac
228 } // namespace base 261 } // namespace base
229 262
230 // Stream operations for CFTypes. They can be used with NSTypes as well 263 // Stream operations for CFTypes. They can be used with NSTypes as well
231 // by using the NSToCFCast methods above. 264 // by using the NSToCFCast methods above.
232 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); 265 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
233 // Operator << can not be overloaded for ObjectiveC types as the compiler 266 // Operator << can not be overloaded for ObjectiveC types as the compiler
234 // can not distinguish between overloads for id with overloads for void*. 267 // can not distinguish between overloads for id with overloads for void*.
235 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 268 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
236 const CFErrorRef err); 269 const CFErrorRef err);
237 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, 270 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
238 const CFStringRef str); 271 const CFStringRef str);
239 272
240 #endif // BASE_MAC_FOUNDATION_UTIL_H_ 273 #endif // BASE_MAC_FOUNDATION_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/mac/foundation_util_unittest.mm » ('j') | base/mac/foundation_util_unittest.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698