/* * Copyright (C) 2005-2021 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. * */ #pragma once #include #if USE(CF) || defined(__OBJC__) #include #include #include #include #if USE(CF) #include #endif #ifdef __OBJC__ #import #endif #ifndef CF_BRIDGED_TYPE #define CF_BRIDGED_TYPE(T) #endif #ifndef CF_RELEASES_ARGUMENT #define CF_RELEASES_ARGUMENT #endif #ifndef NS_RELEASES_ARGUMENT #define NS_RELEASES_ARGUMENT #endif #ifndef __OBJC__ typedef struct objc_object *id; #endif // Because ARC enablement is a compile-time choice, and we compile this header // both ways, we need a separate copy of our code when ARC is enabled. #if __has_feature(objc_arc) #define adoptNS adoptNSArc #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 constexpr RetainPtr adoptCF(T CF_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; #ifdef __OBJC__ template RetainPtr::HelperPtrType> adoptNS(T NS_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; #endif template class RetainPtr { public: using ValueType = std::remove_pointer_t; using PtrType = ValueType*; #ifdef __OBJC__ using HelperPtrType = typename std::conditional_t && !std::is_same_v, std::remove_pointer_t, T>; #else using HelperPtrType = PtrType; #endif RetainPtr() = default; RetainPtr(PtrType); RetainPtr(const RetainPtr&); template RetainPtr(const RetainPtr&); constexpr RetainPtr(RetainPtr&& o) : m_ptr(toStorageType(o.leakRef())) { } template constexpr RetainPtr(RetainPtr&& o) : m_ptr(toStorageType(o.leakRef())) { } // Hash table deleted values, which are only constructed and never copied or destroyed. constexpr RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } constexpr bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); } ~RetainPtr(); void clear(); PtrType leakRef() WARN_UNUSED_RETURN; PtrType autorelease(); #ifdef __OBJC__ id bridgingAutorelease(); #endif constexpr PtrType get() const { return fromStorageType(m_ptr); } constexpr PtrType operator->() const { return fromStorageType(m_ptr); } constexpr explicit operator PtrType() const { return fromStorageType(m_ptr); } constexpr explicit operator bool() const { return m_ptr; } constexpr bool operator!() const { return !m_ptr; } // This conversion operator allows implicit conversion to bool but not to other integer types. // FIXME: Eventually we should remove this; it's an outdated technique and less needed since we have explicit operator bool. typedef CFTypeRef 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 constexpr RetainPtr adoptCF(U CF_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; #ifdef __OBJC__ template friend RetainPtr::HelperPtrType> adoptNS(U NS_RELEASES_ARGUMENT) WARN_UNUSED_RETURN; #endif private: enum AdoptTag { Adopt }; constexpr RetainPtr(PtrType ptr, AdoptTag) : m_ptr(toStorageType(ptr)) { } static constexpr PtrType hashTableDeletedValue() { return reinterpret_cast(-1); } #ifdef __OBJC__ template constexpr std::enable_if_t, PtrType> fromStorageTypeHelper(CFTypeRef ptr) const { return (__bridge PtrType)const_cast(ptr); } template constexpr std::enable_if_t, PtrType> fromStorageTypeHelper(CFTypeRef ptr) const { return (PtrType)const_cast(ptr); } constexpr PtrType fromStorageType(CFTypeRef ptr) const { return fromStorageTypeHelper(ptr); } constexpr CFTypeRef toStorageType(id ptr) const { return (__bridge CFTypeRef)ptr; } constexpr CFTypeRef toStorageType(CFTypeRef ptr) const { return (CFTypeRef)ptr; } #else constexpr PtrType fromStorageType(CFTypeRef ptr) const { return (PtrType)const_cast(ptr); } constexpr CFTypeRef toStorageType(PtrType ptr) const { return (CFTypeRef)ptr; } #endif CFTypeRef m_ptr { nullptr }; }; template RetainPtr(T) -> RetainPtr>; // Helper function for creating a RetainPtr using template argument deduction. template RetainPtr::HelperPtrType> retainPtr(T) WARN_UNUSED_RETURN; template inline RetainPtr::~RetainPtr() { if (auto ptr = std::exchange(m_ptr, nullptr)) CFRelease(ptr); } template inline RetainPtr::RetainPtr(PtrType ptr) : m_ptr(toStorageType(ptr)) { if (m_ptr) CFRetain(m_ptr); } template inline RetainPtr::RetainPtr(const RetainPtr& o) : RetainPtr(o.get()) { } template template inline RetainPtr::RetainPtr(const RetainPtr& o) : RetainPtr(o.get()) { } template inline void RetainPtr::clear() { if (auto ptr = std::exchange(m_ptr, nullptr)) CFRelease(ptr); } template inline auto RetainPtr::leakRef() -> PtrType { return fromStorageType(std::exchange(m_ptr, nullptr)); } template inline auto RetainPtr::autorelease() -> PtrType { #ifdef __OBJC__ if constexpr (std::is_convertible_v) return CFBridgingRelease(std::exchange(m_ptr, nullptr)); #endif if (m_ptr) CFAutorelease(m_ptr); return leakRef(); } #ifdef __OBJC__ // FIXME: It would be better if we could base the return type on the type that is toll-free bridged with T rather than using id. template inline id RetainPtr::bridgingAutorelease() { static_assert((!std::is_convertible::value), "Don't use bridgingAutorelease for Objective-C pointer types."); return 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 constexpr bool operator==(const RetainPtr& a, const RetainPtr& b) { return a.get() == b.get(); } template constexpr bool operator==(const RetainPtr& a, U* b) { return a.get() == b; } template constexpr bool operator==(T* a, const RetainPtr& b) { return a == b.get(); } template constexpr bool operator!=(const RetainPtr& a, const RetainPtr& b) { return a.get() != b.get(); } template constexpr bool operator!=(const RetainPtr& a, U* b) { return a.get() != b; } template constexpr bool operator!=(T* a, const RetainPtr& b) { return a != b.get(); } template constexpr 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::HelperPtrType> adoptNS(T NS_RELEASES_ARGUMENT ptr) { #if __has_feature(objc_arc) return ptr; #elif defined(OBJC_NO_GC) using ReturnType = RetainPtr::HelperPtrType>; return ReturnType { ptr, ReturnType::Adopt }; #else RetainPtr::HelperPtrType> result = ptr; [ptr release]; return result; #endif } #endif template inline RetainPtr::HelperPtrType> retainPtr(T ptr) { return ptr; } template struct IsSmartPtr> { static constexpr bool value = true; }; template struct HashTraits> : SimpleClassHashTraits> { }; template struct DefaultHash> : PtrHash> { }; template struct RetainPtrObjectHashTraits : SimpleClassHashTraits> { static const RetainPtr

& emptyValue() { static NeverDestroyed> null; 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 constexpr bool safeToCompareToEmptyOrDeleted = false; }; inline bool safeCFEqual(CFTypeRef a, CFTypeRef b) { return (!a && !b) || (a && b && CFEqual(a, b)); } inline CFHashCode safeCFHash(CFTypeRef a) { return a ? CFHash(a) : 0; } #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::retainPtr; using WTF::safeCFEqual; using WTF::safeCFHash; #ifdef __OBJC__ using WTF::adoptNS; using WTF::dynamic_objc_cast; #endif #endif // USE(CF) || defined(__OBJC__)