| 1 |
#ifndef std_move |
| 2 |
#define std_move |
| 3 |
|
| 4 |
namespace std { |
| 5 |
|
| 6 |
template <typename T> struct remove_reference { |
| 7 |
typedef T type; |
| 8 |
}; |
| 9 |
|
| 10 |
template <typename T> struct remove_reference<T&> { |
| 11 |
typedef T type; |
| 12 |
}; |
| 13 |
|
| 14 |
template<typename T> typename remove_reference<T>::type&& move(T&& t); |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
#endif |
| 19 |
|
| 20 |
namespace WTF { |
| 21 |
|
| 22 |
template<typename T> typename std::remove_reference<T>::type&& move(T&& t); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
namespace std { |
| 27 |
|
| 28 |
template <bool, typename U = void> struct enable_if { |
| 29 |
}; |
| 30 |
|
| 31 |
template <typename T> struct enable_if<true, T> { |
| 32 |
using type = T; |
| 33 |
}; |
| 34 |
|
| 35 |
template <bool value, class T = void> |
| 36 |
using enable_if_t = typename enable_if<value, T>::type; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
@class NSString; |
| 41 |
@class NSArray; |
| 42 |
@class NSMutableArray; |
| 43 |
@class NSDictionary; |
| 44 |
@class NSMutableDictionary; |
| 45 |
#define nil ((id)0) |
| 46 |
#define CF_BRIDGED_TYPE(T) __attribute__((objc_bridge(T))) |
| 47 |
#define CF_BRIDGED_MUTABLE_TYPE(T) __attribute__((objc_bridge_mutable(T))) |
| 48 |
typedef CF_BRIDGED_TYPE(id) void * CFTypeRef; |
| 49 |
typedef unsigned long long CFTypeID; |
| 50 |
typedef signed char BOOL; |
| 51 |
typedef unsigned char Boolean; |
| 52 |
typedef signed long CFIndex; |
| 53 |
typedef unsigned long NSUInteger; |
| 54 |
typedef const struct CF_BRIDGED_TYPE(id) __CFAllocator * CFAllocatorRef; |
| 55 |
typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef; |
| 56 |
typedef const struct CF_BRIDGED_TYPE(NSArray) __CFArray * CFArrayRef; |
| 57 |
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableArray) __CFArray * CFMutableArrayRef; |
| 58 |
typedef struct CF_BRIDGED_MUTABLE_TYPE(CFRunLoopRef) __CFRunLoop * CFRunLoopRef; |
| 59 |
typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef; |
| 60 |
|
| 61 |
#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) |
| 62 |
#define CF_CONSUMED __attribute__((cf_consumed)) |
| 63 |
#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) |
| 64 |
#define NS_REQUIRES_PROPERTY_DEFINITIONS __attribute__((objc_requires_property_definitions)) |
| 65 |
|
| 66 |
extern const CFAllocatorRef kCFAllocatorDefault; |
| 67 |
typedef struct _NSZone NSZone; |
| 68 |
|
| 69 |
CFTypeID CFGetTypeID(CFTypeRef cf); |
| 70 |
|
| 71 |
CFTypeID CFGetTypeID(CFTypeRef cf); |
| 72 |
CFTypeID CFArrayGetTypeID(); |
| 73 |
CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity); |
| 74 |
extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
| 75 |
CFArrayRef CFArrayCreate(CFAllocatorRef allocator, const void **values, CFIndex numValues); |
| 76 |
CFIndex CFArrayGetCount(CFArrayRef theArray); |
| 77 |
|
| 78 |
typedef const struct CF_BRIDGED_TYPE(NSDictionary) __CFDictionary * CFDictionaryRef; |
| 79 |
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableDictionary) __CFDictionary * CFMutableDictionaryRef; |
| 80 |
|
| 81 |
CFTypeID CFDictionaryGetTypeID(); |
| 82 |
CFDictionaryRef CFDictionaryCreate(CFAllocatorRef allocator, const void **keys, const void **values, CFIndex numValues); |
| 83 |
CFDictionaryRef CFDictionaryCreateCopy(CFAllocatorRef allocator, CFDictionaryRef theDict); |
| 84 |
CFDictionaryRef CFDictionaryCreateMutableCopy(CFAllocatorRef allocator, CFIndex capacity, CFDictionaryRef theDict); |
| 85 |
CFIndex CFDictionaryGetCount(CFDictionaryRef theDict); |
| 86 |
Boolean CFDictionaryContainsKey(CFDictionaryRef theDict, const void *key); |
| 87 |
Boolean CFEqual(CFTypeRef, CFTypeRef); |
| 88 |
Boolean CFDictionaryContainsValue(CFDictionaryRef theDict, const void *value); |
| 89 |
const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
| 90 |
void CFDictionaryAddValue(CFMutableDictionaryRef theDict, const void *key, const void *value); |
| 91 |
void CFDictionarySetValue(CFMutableDictionaryRef theDict, const void *key, const void *value); |
| 92 |
void CFDictionaryReplaceValue(CFMutableDictionaryRef theDict, const void *key, const void *value); |
| 93 |
void CFDictionaryRemoveValue(CFMutableDictionaryRef theDict, const void *key); |
| 94 |
|
| 95 |
typedef struct CF_BRIDGED_TYPE(id) __SecTask * SecTaskRef; |
| 96 |
SecTaskRef SecTaskCreateFromSelf(CFAllocatorRef allocator); |
| 97 |
|
| 98 |
typedef struct CF_BRIDGED_TYPE(id) CF_BRIDGED_MUTABLE_TYPE(IOSurface) __IOSurface * IOSurfaceRef; |
| 99 |
IOSurfaceRef IOSurfaceCreate(CFDictionaryRef properties); |
| 100 |
|
| 101 |
typedef struct CF_BRIDGED_TYPE(id) __CVBuffer *CVBufferRef; |
| 102 |
typedef CVBufferRef CVImageBufferRef; |
| 103 |
typedef CVImageBufferRef CVPixelBufferRef; |
| 104 |
typedef signed int CVReturn; |
| 105 |
CVReturn CVPixelBufferCreateWithIOSurface(CFAllocatorRef allocator, IOSurfaceRef surface, CFDictionaryRef pixelBufferAttributes, CF_RETURNS_RETAINED CVPixelBufferRef * pixelBufferOut); |
| 106 |
|
| 107 |
extern "C" NSString *NSStringFromSelector(SEL aSelector); |
| 108 |
extern "C" SEL NSSelectorFromString(NSString *aSelectorName); |
| 109 |
|
| 110 |
extern "C" NSString *NSStringFromClass(Class aClass); |
| 111 |
extern "C" Class NSClassFromString(NSString *aClassName); |
| 112 |
|
| 113 |
extern "C" NSString *NSStringFromProtocol(Protocol *proto); |
| 114 |
extern "C" Protocol * NSProtocolFromString(NSString *namestr); |
| 115 |
|
| 116 |
CFRunLoopRef CFRunLoopGetCurrent(void); |
| 117 |
CFRunLoopRef CFRunLoopGetMain(void); |
| 118 |
extern CFTypeRef CFRetain(CFTypeRef cf); |
| 119 |
extern void CFRelease(CFTypeRef cf); |
| 120 |
#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr "")) |
| 121 |
|
| 122 |
#if __has_feature(objc_arc) |
| 123 |
id CFBridgingRelease(CFTypeRef X) { |
| 124 |
return (__bridge_transfer id)X; |
| 125 |
} |
| 126 |
#endif |
| 127 |
|
| 128 |
__attribute__((objc_root_class)) |
| 129 |
@interface NSObject |
| 130 |
+ (instancetype) alloc; |
| 131 |
+ (instancetype) allocWithZone:(NSZone *)zone; |
| 132 |
+ (Class) class; |
| 133 |
+ (Class) superclass; |
| 134 |
- (instancetype) init; |
| 135 |
- (instancetype)retain; |
| 136 |
- (instancetype)autorelease; |
| 137 |
- (void)release; |
| 138 |
- (BOOL)isKindOfClass:(Class)aClass; |
| 139 |
@end |
| 140 |
|
| 141 |
@protocol NSCopying |
| 142 |
- (id)copyWithZone:(NSZone *)zone; |
| 143 |
@end |
| 144 |
|
| 145 |
@protocol NSFastEnumeration |
| 146 |
- (int)countByEnumeratingWithState:(void *)state objects:(id *)objects count:(unsigned)count; |
| 147 |
- (void)protocolMethod; |
| 148 |
@end |
| 149 |
|
| 150 |
@interface NSEnumerator <NSFastEnumeration> |
| 151 |
@end |
| 152 |
|
| 153 |
@interface NSDictionary : NSObject <NSCopying> |
| 154 |
- (NSUInteger)count; |
| 155 |
- (id)objectForKey:(id)aKey; |
| 156 |
- (id)objectForKeyedSubscript:(id)aKey; |
| 157 |
- (NSEnumerator *)keyEnumerator; |
| 158 |
+ (id)dictionary; |
| 159 |
+ (id)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key; |
| 160 |
- (NSMutableDictionary *)mutableCopy; |
| 161 |
+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt; |
| 162 |
@end |
| 163 |
|
| 164 |
@interface NSArray : NSObject <NSCopying, NSFastEnumeration> |
| 165 |
- (NSUInteger)count; |
| 166 |
- (NSEnumerator *)objectEnumerator; |
| 167 |
- (NSMutableArray *)mutableCopy; |
| 168 |
+ (NSArray *)arrayWithObjects:(const id [])objects count:(NSUInteger)count; |
| 169 |
@end |
| 170 |
|
| 171 |
@interface NSString : NSObject <NSCopying> |
| 172 |
- (NSUInteger)length; |
| 173 |
- (NSString *)stringByAppendingString:(NSString *)aString; |
| 174 |
- ( const char *)UTF8String; |
| 175 |
- (id)initWithUTF8String:(const char *)nullTerminatedCString; |
| 176 |
- (NSString *)copy; |
| 177 |
+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 178 |
@end |
| 179 |
|
| 180 |
@interface NSMutableString : NSString |
| 181 |
@end |
| 182 |
|
| 183 |
@interface NSValue : NSObject <NSCopying> |
| 184 |
- (void)getValue:(void *)value; |
| 185 |
@end |
| 186 |
|
| 187 |
@interface NSNumber : NSValue |
| 188 |
- (char)charValue; |
| 189 |
- (int)intValue; |
| 190 |
- (id)initWithInt:(int)value; |
| 191 |
+ (NSNumber *)numberWithInt:(int)value; |
| 192 |
+ (NSNumber *)numberWithBool:(BOOL)value; |
| 193 |
@end |
| 194 |
|
| 195 |
@interface NSResponder : NSObject |
| 196 |
@end |
| 197 |
|
| 198 |
@interface NSApplication : NSResponder |
| 199 |
|
| 200 |
extern NSApplication * NSApp; |
| 201 |
|
| 202 |
@property (class, readonly, strong) NSApplication *sharedApplication; |
| 203 |
|
| 204 |
- (void)finishLaunching; |
| 205 |
- (void)run; |
| 206 |
- (void)stop:(id)sender; |
| 207 |
- (void)terminate:(id)sender; |
| 208 |
|
| 209 |
@end |
| 210 |
|
| 211 |
@interface SomeObj : NSObject |
| 212 |
- (instancetype)_init; |
| 213 |
- (SomeObj *)mutableCopy; |
| 214 |
- (SomeObj *)copyWithValue:(int)value; |
| 215 |
- (void)doWork; |
| 216 |
- (SomeObj *)other; |
| 217 |
- (SomeObj *)next; |
| 218 |
- (int)value; |
| 219 |
@end |
| 220 |
|
| 221 |
@interface OtherObj : SomeObj |
| 222 |
- (void)doMoreWork:(OtherObj *)other; |
| 223 |
@end |
| 224 |
|
| 225 |
@protocol OS_dispatch_queue |
| 226 |
@end |
| 227 |
|
| 228 |
typedef NSObject<OS_dispatch_queue> *dispatch_queue_t; |
| 229 |
|
| 230 |
@protocol OS_dispatch_queue_attr |
| 231 |
@end |
| 232 |
|
| 233 |
typedef NSObject<OS_dispatch_queue_attr> *dispatch_queue_attr_t; |
| 234 |
|
| 235 |
NS_RETURNS_RETAINED dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr); |
| 236 |
const char *dispatch_queue_get_label(dispatch_queue_t queue); |
| 237 |
|
| 238 |
namespace std { |
| 239 |
|
| 240 |
template <typename StorageType> |
| 241 |
void swap(StorageType& a, StorageType& b) |
| 242 |
{ |
| 243 |
StorageType temp = static_cast<StorageType&&>(a); |
| 244 |
a = static_cast<StorageType&&>(b); |
| 245 |
b = static_cast<StorageType&&>(temp); |
| 246 |
} |
| 247 |
|
| 248 |
template <typename StorageType, typename ValueType> |
| 249 |
StorageType exchange(StorageType& obj, ValueType& value) |
| 250 |
{ |
| 251 |
StorageType returnValue = static_cast<StorageType&&>(obj); |
| 252 |
obj = static_cast<StorageType&&>(value); |
| 253 |
return returnValue; |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
namespace WTF { |
| 259 |
|
| 260 |
void WTFCrash(void); |
| 261 |
|
| 262 |
#if __has_feature(objc_arc) |
| 263 |
#define RetainPtr RetainPtrArc |
| 264 |
#endif |
| 265 |
|
| 266 |
template<typename T> class RetainPtr; |
| 267 |
template<typename T> RetainPtr<T> adoptNS(T*); |
| 268 |
template<typename T> RetainPtr<T> adoptCF(T); |
| 269 |
|
| 270 |
template <typename T, typename S> T *downcast(S *t) { return static_cast<T*>(t); } |
| 271 |
|
| 272 |
template <typename T> struct RemovePointer { |
| 273 |
typedef T Type; |
| 274 |
}; |
| 275 |
|
| 276 |
template <typename T> struct RemovePointer<T*> { |
| 277 |
typedef T Type; |
| 278 |
}; |
| 279 |
|
| 280 |
template <typename T> struct IsPointer { |
| 281 |
static constexpr bool value = false; |
| 282 |
}; |
| 283 |
|
| 284 |
template <typename T> struct IsPointer<T*> { |
| 285 |
static constexpr bool value = true; |
| 286 |
}; |
| 287 |
|
| 288 |
template <typename T> struct RetainPtr { |
| 289 |
using ValueType = typename RemovePointer<T>::Type; |
| 290 |
using PtrType = ValueType*; |
| 291 |
PtrType t; |
| 292 |
|
| 293 |
RetainPtr() : t(nullptr) { } |
| 294 |
RetainPtr(PtrType t) |
| 295 |
: t(t) { |
| 296 |
if (t) |
| 297 |
CFRetain(toCFTypeRef(t)); |
| 298 |
} |
| 299 |
RetainPtr(RetainPtr&&); |
| 300 |
RetainPtr(const RetainPtr&); |
| 301 |
template <typename U> |
| 302 |
RetainPtr(const RetainPtr<U>& o) |
| 303 |
: RetainPtr(o.t) |
| 304 |
{} |
| 305 |
RetainPtr operator=(const RetainPtr& o) |
| 306 |
{ |
| 307 |
if (t) |
| 308 |
CFRelease(toCFTypeRef(t)); |
| 309 |
t = o.t; |
| 310 |
if (t) |
| 311 |
CFRetain(toCFTypeRef(t)); |
| 312 |
return *this; |
| 313 |
} |
| 314 |
template <typename U> |
| 315 |
RetainPtr operator=(const RetainPtr<U>& o) |
| 316 |
{ |
| 317 |
if (t) |
| 318 |
CFRelease(toCFTypeRef(t)); |
| 319 |
t = o.t; |
| 320 |
if (t) |
| 321 |
CFRetain(toCFTypeRef(t)); |
| 322 |
return *this; |
| 323 |
} |
| 324 |
~RetainPtr() { |
| 325 |
clear(); |
| 326 |
} |
| 327 |
void clear() { |
| 328 |
if (t) |
| 329 |
CFRelease(toCFTypeRef(t)); |
| 330 |
t = nullptr; |
| 331 |
} |
| 332 |
void swap(RetainPtr& o) { |
| 333 |
PtrType tmp = t; |
| 334 |
t = o.t; |
| 335 |
o.t = tmp; |
| 336 |
} |
| 337 |
PtrType get() const { return t; } |
| 338 |
PtrType operator->() const { return t; } |
| 339 |
T &operator*() const { return *t; } |
| 340 |
RetainPtr &operator=(PtrType t); |
| 341 |
|
| 342 |
template <typename U = PtrType> |
| 343 |
std::enable_if_t<IsPointer<U>::value, U> leakRef() CF_RETURNS_RETAINED |
| 344 |
{ |
| 345 |
PtrType s = t; |
| 346 |
t = nullptr; |
| 347 |
return s; |
| 348 |
} |
| 349 |
|
| 350 |
template <typename U = PtrType> |
| 351 |
std::enable_if_t<!IsPointer<U>::value, U> leakRef() NS_RETURNS_RETAINED |
| 352 |
{ |
| 353 |
PtrType s = t; |
| 354 |
t = nullptr; |
| 355 |
return s; |
| 356 |
} |
| 357 |
|
| 358 |
operator PtrType() const { return t; } |
| 359 |
operator bool() const { return t; } |
| 360 |
|
| 361 |
#if !__has_feature(objc_arc) |
| 362 |
PtrType autorelease() { [[clang::suppress]] return [t autorelease]; } |
| 363 |
#endif |
| 364 |
|
| 365 |
private: |
| 366 |
CFTypeRef toCFTypeRef(id ptr) { return (__bridge CFTypeRef)ptr; } |
| 367 |
CFTypeRef toCFTypeRef(const void* ptr) { return (CFTypeRef)ptr; } |
| 368 |
|
| 369 |
template <typename U> friend RetainPtr<U> adoptNS(U*); |
| 370 |
template <typename U> friend RetainPtr<U> adoptCF(U); |
| 371 |
|
| 372 |
enum AdoptTag { Adopt }; |
| 373 |
RetainPtr(PtrType t, AdoptTag) : t(t) { } |
| 374 |
}; |
| 375 |
|
| 376 |
template <typename T> |
| 377 |
RetainPtr<T>::RetainPtr(RetainPtr<T>&& o) |
| 378 |
: RetainPtr(o.t) |
| 379 |
{ |
| 380 |
o.t = nullptr; |
| 381 |
} |
| 382 |
|
| 383 |
template <typename T> |
| 384 |
RetainPtr<T>::RetainPtr(const RetainPtr<T>& o) |
| 385 |
: RetainPtr(o.t) |
| 386 |
{ |
| 387 |
} |
| 388 |
|
| 389 |
template <typename T> |
| 390 |
RetainPtr<T> adoptNS(T* t) { |
| 391 |
#if __has_feature(objc_arc) |
| 392 |
return t; |
| 393 |
#else |
| 394 |
return RetainPtr<T>(t, RetainPtr<T>::Adopt); |
| 395 |
#endif |
| 396 |
} |
| 397 |
|
| 398 |
template <typename T> |
| 399 |
RetainPtr<T> adoptCF(T t) { |
| 400 |
return RetainPtr<T>(t, RetainPtr<T>::Adopt); |
| 401 |
} |
| 402 |
|
| 403 |
template<typename T> inline RetainPtr<T> retainPtr(T ptr) |
| 404 |
{ |
| 405 |
return ptr; |
| 406 |
} |
| 407 |
|
| 408 |
template<typename T> inline RetainPtr<T> retainPtr(T* ptr) |
| 409 |
{ |
| 410 |
return ptr; |
| 411 |
} |
| 412 |
|
| 413 |
template<typename> class OSObjectPtr; |
| 414 |
template<typename T> OSObjectPtr<T> adoptOSObject(T); |
| 415 |
|
| 416 |
template<typename T> static inline void retainOSObject(T ptr) |
| 417 |
{ |
| 418 |
#if !__has_feature(objc_arc) |
| 419 |
[ptr retain]; |
| 420 |
#endif |
| 421 |
} |
| 422 |
|
| 423 |
template<typename T> static inline void releaseOSObject(T ptr) |
| 424 |
{ |
| 425 |
#if !__has_feature(objc_arc) |
| 426 |
[ptr release]; |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
template<typename T> class OSObjectPtr { |
| 431 |
public: |
| 432 |
OSObjectPtr() |
| 433 |
: m_ptr(nullptr) |
| 434 |
{ |
| 435 |
} |
| 436 |
|
| 437 |
~OSObjectPtr() |
| 438 |
{ |
| 439 |
if (m_ptr) |
| 440 |
releaseOSObject(m_ptr); |
| 441 |
} |
| 442 |
|
| 443 |
T get() const { return m_ptr; } |
| 444 |
|
| 445 |
explicit operator bool() const { return m_ptr; } |
| 446 |
bool operator!() const { return !m_ptr; } |
| 447 |
|
| 448 |
OSObjectPtr(const OSObjectPtr& other) |
| 449 |
: m_ptr(other.m_ptr) |
| 450 |
{ |
| 451 |
if (m_ptr) |
| 452 |
retainOSObject(m_ptr); |
| 453 |
} |
| 454 |
|
| 455 |
OSObjectPtr(OSObjectPtr&& other) |
| 456 |
: m_ptr(std::move(other.m_ptr)) |
| 457 |
{ |
| 458 |
other.m_ptr = nullptr; |
| 459 |
} |
| 460 |
|
| 461 |
OSObjectPtr(T ptr) |
| 462 |
: m_ptr(WTF::move(ptr)) |
| 463 |
{ |
| 464 |
if (m_ptr) |
| 465 |
retainOSObject(m_ptr); |
| 466 |
} |
| 467 |
|
| 468 |
OSObjectPtr& operator=(const OSObjectPtr& other) |
| 469 |
{ |
| 470 |
OSObjectPtr ptr = other; |
| 471 |
swap(ptr); |
| 472 |
return *this; |
| 473 |
} |
| 474 |
|
| 475 |
OSObjectPtr& operator=(OSObjectPtr&& other) |
| 476 |
{ |
| 477 |
OSObjectPtr ptr = std::move(other); |
| 478 |
swap(ptr); |
| 479 |
return *this; |
| 480 |
} |
| 481 |
|
| 482 |
OSObjectPtr& operator=(decltype(nullptr)) |
| 483 |
{ |
| 484 |
if (m_ptr) |
| 485 |
releaseOSObject(m_ptr); |
| 486 |
m_ptr = nullptr; |
| 487 |
return *this; |
| 488 |
} |
| 489 |
|
| 490 |
OSObjectPtr& operator=(T other) |
| 491 |
{ |
| 492 |
OSObjectPtr ptr = WTF::move(other); |
| 493 |
swap(ptr); |
| 494 |
return *this; |
| 495 |
} |
| 496 |
|
| 497 |
void swap(OSObjectPtr& other) |
| 498 |
{ |
| 499 |
std::swap(m_ptr, other.m_ptr); |
| 500 |
} |
| 501 |
|
| 502 |
T leakRef() |
| 503 |
{ |
| 504 |
return std::exchange(m_ptr, nullptr); |
| 505 |
} |
| 506 |
|
| 507 |
friend OSObjectPtr adoptOSObject<T>(T); |
| 508 |
|
| 509 |
private: |
| 510 |
struct AdoptOSObject { }; |
| 511 |
OSObjectPtr(AdoptOSObject, T ptr) |
| 512 |
: m_ptr(std::move(ptr)) |
| 513 |
{ |
| 514 |
} |
| 515 |
|
| 516 |
T m_ptr; |
| 517 |
}; |
| 518 |
|
| 519 |
template<typename T> inline OSObjectPtr<T> adoptOSObject(T ptr) |
| 520 |
{ |
| 521 |
return OSObjectPtr<T> { typename OSObjectPtr<T>::AdoptOSObject { }, std::move(ptr) }; |
| 522 |
} |
| 523 |
|
| 524 |
inline NSObject *bridge_cast(CFTypeRef object) |
| 525 |
{ |
| 526 |
return (__bridge NSObject *)object; |
| 527 |
} |
| 528 |
|
| 529 |
inline CFTypeRef bridge_cast(NSObject *object) |
| 530 |
{ |
| 531 |
return (__bridge CFTypeRef)object; |
| 532 |
} |
| 533 |
|
| 534 |
inline id bridge_id_cast(CFTypeRef object) |
| 535 |
{ |
| 536 |
return (__bridge id)object; |
| 537 |
} |
| 538 |
|
| 539 |
inline RetainPtr<id> bridge_id_cast(RetainPtr<CFTypeRef>&& object) |
| 540 |
{ |
| 541 |
#if __has_feature(objc_arc) |
| 542 |
return adoptNS((__bridge_transfer id)object.leakRef()); |
| 543 |
#else |
| 544 |
return adoptNS((__bridge id)object.leakRef()); |
| 545 |
#endif |
| 546 |
} |
| 547 |
|
| 548 |
template <typename ExpectedType> |
| 549 |
struct ObjCTypeCastTraits { |
| 550 |
public: |
| 551 |
static bool isType(id object) { return [object isKindOfClass:[ExpectedType class]]; } |
| 552 |
|
| 553 |
template <typename ArgType> |
| 554 |
static bool isType(const ArgType *object) { return [object isKindOfClass:[ExpectedType class]]; } |
| 555 |
}; |
| 556 |
|
| 557 |
template <typename ExpectedType, typename ArgType> |
| 558 |
inline bool is_objc(ArgType * source) |
| 559 |
{ |
| 560 |
return source && ObjCTypeCastTraits<ExpectedType>::isType(source); |
| 561 |
} |
| 562 |
|
| 563 |
template<typename T> inline T *checked_objc_cast(id object) |
| 564 |
{ |
| 565 |
if (!object) |
| 566 |
return nullptr; |
| 567 |
|
| 568 |
if (!is_objc<T>(object)) |
| 569 |
WTFCrash(); |
| 570 |
|
| 571 |
return reinterpret_cast<T*>(object); |
| 572 |
} |
| 573 |
|
| 574 |
template<typename T, typename U> inline T *checked_objc_cast(U *object) |
| 575 |
{ |
| 576 |
if (!object) |
| 577 |
return nullptr; |
| 578 |
|
| 579 |
if (!is_objc<T>(object)) |
| 580 |
WTFCrash(); |
| 581 |
|
| 582 |
return static_cast<T*>(object); |
| 583 |
} |
| 584 |
|
| 585 |
template<typename T, typename U> RetainPtr<T> dynamic_objc_cast(RetainPtr<U>&& object) |
| 586 |
{ |
| 587 |
if (!is_objc<T>(object.get())) |
| 588 |
return nullptr; |
| 589 |
return adoptNS(static_cast<T*>(object.leakRef())); |
| 590 |
} |
| 591 |
|
| 592 |
template<typename T> RetainPtr<T> dynamic_objc_cast(RetainPtr<id>&& object) |
| 593 |
{ |
| 594 |
if (!is_objc<T>(object.get())) |
| 595 |
return nullptr; |
| 596 |
return adoptNS(reinterpret_cast<T*>(object.leakRef())); |
| 597 |
} |
| 598 |
|
| 599 |
template<typename T, typename U> RetainPtr<T> dynamic_objc_cast(const RetainPtr<U>& object) |
| 600 |
{ |
| 601 |
if (!is_objc<T>(object.get())) |
| 602 |
return nullptr; |
| 603 |
return static_cast<T*>(object.get()); |
| 604 |
} |
| 605 |
|
| 606 |
template<typename T> RetainPtr<T> dynamic_objc_cast(const RetainPtr<id>& object) |
| 607 |
{ |
| 608 |
if (!is_objc<T>(object.get())) |
| 609 |
return nullptr; |
| 610 |
return reinterpret_cast<T*>(object.get()); |
| 611 |
} |
| 612 |
|
| 613 |
template<typename T> T *dynamic_objc_cast(NSObject *object) |
| 614 |
{ |
| 615 |
if (!is_objc<T>(object)) |
| 616 |
return nullptr; |
| 617 |
return static_cast<T*>(object); |
| 618 |
} |
| 619 |
|
| 620 |
template<typename T> T *dynamic_objc_cast(id object) |
| 621 |
{ |
| 622 |
if (!is_objc<T>(object)) |
| 623 |
return nullptr; |
| 624 |
return reinterpret_cast<T*>(object); |
| 625 |
} |
| 626 |
|
| 627 |
template <typename> struct CFTypeTrait; |
| 628 |
|
| 629 |
template<typename T> T dynamic_cf_cast(CFTypeRef object) |
| 630 |
{ |
| 631 |
if (!object) |
| 632 |
return nullptr; |
| 633 |
|
| 634 |
if (CFGetTypeID(object) != CFTypeTrait<T>::typeID()) |
| 635 |
return nullptr; |
| 636 |
|
| 637 |
return static_cast<T>(const_cast<CF_BRIDGED_TYPE(id) void*>(object)); |
| 638 |
} |
| 639 |
|
| 640 |
template<typename T> T checked_cf_cast(CFTypeRef object) |
| 641 |
{ |
| 642 |
if (!object) |
| 643 |
return nullptr; |
| 644 |
|
| 645 |
if (CFGetTypeID(object) != CFTypeTrait<T>::typeID()) |
| 646 |
WTFCrash(); |
| 647 |
|
| 648 |
return static_cast<T>(const_cast<CF_BRIDGED_TYPE(id) void*>(object)); |
| 649 |
} |
| 650 |
|
| 651 |
template<typename T, typename U> RetainPtr<T> dynamic_cf_cast(RetainPtr<U>&& object) |
| 652 |
{ |
| 653 |
if (!object) |
| 654 |
return nullptr; |
| 655 |
|
| 656 |
if (CFGetTypeID(object.get()) != CFTypeTrait<T>::typeID()) |
| 657 |
return nullptr; |
| 658 |
|
| 659 |
return adoptCF(static_cast<T>(const_cast<CF_BRIDGED_TYPE(id) void*>(object.leakRef()))); |
| 660 |
} |
| 661 |
|
| 662 |
} // namespace WTF |
| 663 |
|
| 664 |
#define WTF_DECLARE_CF_TYPE_TRAIT(ClassName) \ |
| 665 |
template <> \ |
| 666 |
struct WTF::CFTypeTrait<ClassName##Ref> { \ |
| 667 |
static inline CFTypeID typeID(void) { return ClassName##GetTypeID(); } \ |
| 668 |
}; |
| 669 |
|
| 670 |
WTF_DECLARE_CF_TYPE_TRAIT(CFArray); |
| 671 |
WTF_DECLARE_CF_TYPE_TRAIT(CFDictionary); |
| 672 |
|
| 673 |
#define WTF_DECLARE_CF_MUTABLE_TYPE_TRAIT(ClassName, MutableClassName) \ |
| 674 |
template <> \ |
| 675 |
struct WTF::CFTypeTrait<MutableClassName##Ref> { \ |
| 676 |
static inline CFTypeID typeID(void) { return ClassName##GetTypeID(); } \ |
| 677 |
}; |
| 678 |
|
| 679 |
WTF_DECLARE_CF_MUTABLE_TYPE_TRAIT(CFArray, CFMutableArray); |
| 680 |
WTF_DECLARE_CF_MUTABLE_TYPE_TRAIT(CFDictionary, CFMutableDictionary); |
| 681 |
|
| 682 |
using WTF::RetainPtr; |
| 683 |
using WTF::adoptNS; |
| 684 |
using WTF::adoptCF; |
| 685 |
using WTF::retainPtr; |
| 686 |
using WTF::OSObjectPtr; |
| 687 |
using WTF::adoptOSObject; |
| 688 |
using WTF::downcast; |
| 689 |
using WTF::bridge_cast; |
| 690 |
using WTF::bridge_id_cast; |
| 691 |
using WTF::is_objc; |
| 692 |
using WTF::checked_objc_cast; |
| 693 |
using WTF::dynamic_objc_cast; |
| 694 |
using WTF::checked_cf_cast; |
| 695 |
using WTF::dynamic_cf_cast; |