/* * Copyright (C) 2005, 2006, 2007, 2008, 2010, 2013, 2014 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef RetainPtr_h #define RetainPtr_h #include #if USE(CF) || defined(__OBJC__) #include #include #include #if USE(CF) #include #endif #ifdef __OBJC__ #import #endif #ifndef CF_RELEASES_ARGUMENT #define CF_RELEASES_ARGUMENT #endif #ifndef NS_RELEASES_ARGUMENT #define NS_RELEASES_ARGUMENT #endif namespace WTF { // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type, // so both RetainPtr and RetainPtr will work. template class RetainPtr; template RetainPtr adoptCF(T CF_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; template RetainPtr adoptNS(T NS_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; template class RetainPtr { public: typedef typename std::remove_pointer::type ValueType; typedef ValueType* PtrType; typedef CFTypeRef StorageType; RetainPtr() : m_ptr(nullptr) { } RetainPtr(PtrType ptr) : m_ptr(toStorageType(ptr)) { if (m_ptr) CFRetain(m_ptr); } RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (StorageType ptr = m_ptr) CFRetain(ptr); } RetainPtr(RetainPtr&& o) : m_ptr(toStorageType(o.leakRef())) { } template RetainPtr(RetainPtr&& o) : m_ptr(toStorageType(o.leakRef())) { } // Hash table deleted values, which are only constructed and never copied or destroyed. RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); } ~RetainPtr(); template RetainPtr(const RetainPtr&); void clear(); PtrType leakRef() WARN_UNUSED_RETURN; PtrType autorelease(); PtrType get() const { return fromStorageType(m_ptr); } PtrType operator->() const { return fromStorageType(m_ptr); } explicit operator PtrType() const { return fromStorageType(m_ptr); } explicit operator bool() const { return m_ptr; } bool operator!() const { return !m_ptr; } // This conversion operator allows implicit conversion to bool but not to other integer types. typedef StorageType RetainPtr::*UnspecifiedBoolType; operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : nullptr; } RetainPtr& operator=(const RetainPtr&); template RetainPtr& operator=(const RetainPtr&); RetainPtr& operator=(PtrType); template RetainPtr& operator=(U*); RetainPtr& operator=(RetainPtr&&); template RetainPtr& operator=(RetainPtr&&); void swap(RetainPtr&); template friend RetainPtr adoptCF(U CF_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; template friend RetainPtr adoptNS(U NS_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; private: enum AdoptTag { Adopt }; RetainPtr(PtrType ptr, AdoptTag) : m_ptr(toStorageType(ptr)) { } static PtrType hashTableDeletedValue() { return reinterpret_cast(-1); } #if defined (__OBJC__) && __has_feature(objc_arc) template typename std::enable_if::value, PtrType>::type fromStorageTypeHelper(StorageType ptr) const { return (__bridge PtrType)ptr; } template typename std::enable_if::value, PtrType>::type fromStorageTypeHelper(StorageType ptr) const { return (PtrType)ptr; } PtrType fromStorageType(StorageType ptr) const { return fromStorageTypeHelper(ptr); } StorageType toStorageType(id ptr) const { return (__bridge StorageType)ptr; } StorageType toStorageType(CFTypeRef ptr) const { return (StorageType)ptr; } #else PtrType fromStorageType(StorageType ptr) const { return (PtrType)ptr; } StorageType toStorageType(PtrType ptr) const { return (StorageType)ptr; } #endif StorageType m_ptr; }; template inline RetainPtr::~RetainPtr() { if (StorageType ptr = std::exchange(m_ptr, nullptr)) CFRelease(ptr); } // Helper function for creating a RetainPtr using template argument deduction. template inline RetainPtr retainPtr(T) WARN_UNUSED_RETURN; template template inline RetainPtr::RetainPtr(const RetainPtr& o) : m_ptr(toStorageType(o.get())) { if (StorageType ptr = m_ptr) CFRetain(ptr); } template inline void RetainPtr::clear() { if (StorageType ptr = std::exchange(m_ptr, nullptr)) CFRelease(ptr); } template inline typename RetainPtr::PtrType RetainPtr::leakRef() { return fromStorageType(std::exchange(m_ptr, nullptr)); } #ifdef __OBJC__ template inline auto RetainPtr::autorelease() -> PtrType { return (__bridge PtrType)CFBridgingRelease(leakRef()); } #endif template inline RetainPtr& RetainPtr::operator=(const RetainPtr& o) { RetainPtr ptr = o; swap(ptr); return *this; } template template inline RetainPtr& RetainPtr::operator=(const RetainPtr& o) { RetainPtr ptr = o; swap(ptr); return *this; } template inline RetainPtr& RetainPtr::operator=(PtrType optr) { RetainPtr ptr = optr; swap(ptr); return *this; } template template inline RetainPtr& RetainPtr::operator=(U* optr) { RetainPtr ptr = optr; swap(ptr); return *this; } template inline RetainPtr& RetainPtr::operator=(RetainPtr&& o) { RetainPtr ptr = WTFMove(o); swap(ptr); return *this; } template template inline RetainPtr& RetainPtr::operator=(RetainPtr&& o) { RetainPtr ptr = WTFMove(o); swap(ptr); return *this; } template inline void RetainPtr::swap(RetainPtr& o) { std::swap(m_ptr, o.m_ptr); } template inline void swap(RetainPtr& a, RetainPtr& b) { a.swap(b); } template inline bool operator==(const RetainPtr& a, const RetainPtr& b) { return a.get() == b.get(); } template inline bool operator==(const RetainPtr& a, U* b) { return a.get() == b; } template inline bool operator==(T* a, const RetainPtr& b) { return a == b.get(); } template inline bool operator!=(const RetainPtr& a, const RetainPtr& b) { return a.get() != b.get(); } template inline bool operator!=(const RetainPtr& a, U* b) { return a.get() != b; } template inline bool operator!=(T* a, const RetainPtr& b) { return a != b.get(); } template inline RetainPtr adoptCF(T CF_RELEASES_ARGUMENT ptr) { #ifdef __OBJC__ static_assert((!std::is_convertible::value), "Don't use adoptCF with Objective-C pointer types, use adoptNS."); #endif return RetainPtr(ptr, RetainPtr::Adopt); } #ifdef __OBJC__ template inline RetainPtr adoptNS(T NS_RELEASES_ARGUMENT ptr) { #if __has_feature(objc_arc) return ptr; #elif defined(OBJC_NO_GC) return RetainPtr(ptr, RetainPtr::Adopt); #else RetainPtr result = ptr; [ptr release]; return result; #endif } #endif template inline RetainPtr retainPtr(T ptr) { return ptr; } template struct IsSmartPtr> { static const bool value = true; }; template struct HashTraits> : SimpleClassHashTraits> { }; template struct DefaultHash> { typedef PtrHash> Hash; }; template struct RetainPtrObjectHashTraits : SimpleClassHashTraits> { static const RetainPtr

& emptyValue() { static RetainPtr

& null = *(new RetainPtr

); return null; } }; template struct RetainPtrObjectHash { static unsigned hash(const RetainPtr

& o) { ASSERT_WITH_MESSAGE(o.get(), "attempt to use null RetainPtr in HashTable"); return static_cast(CFHash(o.get())); } static bool equal(const RetainPtr

& a, const RetainPtr

& b) { return CFEqual(a.get(), b.get()); } static const bool safeToCompareToEmptyOrDeleted = false; }; #ifdef __OBJC__ template T* dynamic_objc_cast(id object) { if ([object isKindOfClass:[T class]]) return (T *)object; return nil; } #endif } // namespace WTF using WTF::RetainPtr; using WTF::adoptCF; using WTF::adoptNS; using WTF::retainPtr; #ifdef __OBJC__ using WTF::dynamic_objc_cast; #endif #endif // USE(CF) || defined(__OBJC__) #endif // WTF_RetainPtr_h