2 * Copyright (C) 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include <wtf/HashTraits.h>
25 #include <wtf/NullPtr.h>
26 #include <wtf/TypeTraits.h>
30 #include <CoreFoundation/CoreFoundation.h>
34 #import <Foundation/Foundation.h>
39 // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
40 // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
42 enum AdoptCFTag { AdoptCF };
43 enum AdoptNSTag { AdoptNS };
46 inline void adoptNSReference(id ptr)
55 template<typename T> class RetainPtr {
57 typedef typename RemovePointer<T>::Type ValueType;
58 typedef ValueType* PtrType;
60 RetainPtr() : m_ptr(0) {}
61 RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
63 RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
64 RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
66 RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
68 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
69 RetainPtr(RetainPtr&& o) : m_ptr(o.leakRef()) { }
72 // Hash table deleted values, which are only constructed and never copied or destroyed.
73 RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
74 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
76 ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
78 template<typename U> RetainPtr(const RetainPtr<U>&);
80 PtrType get() const { return m_ptr; }
83 PtrType leakRef() WARN_UNUSED_RETURN;
85 PtrType operator->() const { return m_ptr; }
87 bool operator!() const { return !m_ptr; }
89 // This conversion operator allows implicit conversion to bool but not to other integer types.
90 typedef PtrType RetainPtr::*UnspecifiedBoolType;
91 operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : 0; }
93 RetainPtr& operator=(const RetainPtr&);
94 template<typename U> RetainPtr& operator=(const RetainPtr<U>&);
95 RetainPtr& operator=(PtrType);
96 template<typename U> RetainPtr& operator=(U*);
98 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
99 RetainPtr& operator=(RetainPtr&&);
100 template<typename U> RetainPtr& operator=(RetainPtr<U>&&);
103 #if !COMPILER_SUPPORTS(CXX_NULLPTR)
104 RetainPtr& operator=(std::nullptr_t) { clear(); return *this; }
107 void adoptCF(PtrType);
108 void adoptNS(PtrType);
110 void swap(RetainPtr&);
113 static PtrType hashTableDeletedValue() { return reinterpret_cast<PtrType>(-1); }
118 template<typename T> template<typename U> inline RetainPtr<T>::RetainPtr(const RetainPtr<U>& o)
121 if (PtrType ptr = m_ptr)
125 template<typename T> inline void RetainPtr<T>::clear()
127 if (PtrType ptr = m_ptr) {
133 template<typename T> inline typename RetainPtr<T>::PtrType RetainPtr<T>::leakRef()
140 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
142 PtrType optr = o.get();
152 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
154 PtrType optr = o.get();
164 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
175 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
186 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
187 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<T>&& o)
189 adoptCF(o.leakRef());
193 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<U>&& o)
195 adoptCF(o.leakRef());
200 template<typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
208 template<typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
210 adoptNSReference(optr);
218 template<typename T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
220 std::swap(m_ptr, o.m_ptr);
223 template<typename T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
228 template<typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
230 return a.get() == b.get();
233 template<typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
238 template<typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
243 template<typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
245 return a.get() != b.get();
248 template<typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
253 template<typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
258 template<typename T> inline RetainPtr<T> adoptCF(T) WARN_UNUSED_RETURN;
259 template<typename T> inline RetainPtr<T> adoptCF(T o)
261 return RetainPtr<T>(AdoptCF, o);
264 template<typename T> inline RetainPtr<T> adoptNS(T) WARN_UNUSED_RETURN;
265 template<typename T> inline RetainPtr<T> adoptNS(T o)
267 return RetainPtr<T>(AdoptNS, o);
270 // Helper function for creating a RetainPtr using template argument deduction.
271 template<typename T> inline RetainPtr<T> retainPtr(T) WARN_UNUSED_RETURN;
272 template<typename T> inline RetainPtr<T> retainPtr(T o)
274 return RetainPtr<T>(o);
277 template<typename P> struct HashTraits<RetainPtr<P> > : SimpleClassHashTraits<RetainPtr<P> > { };
279 template<typename P> struct PtrHash<RetainPtr<P> > : PtrHash<typename RetainPtr<P>::PtrType> {
280 using PtrHash<typename RetainPtr<P>::PtrType>::hash;
281 static unsigned hash(const RetainPtr<P>& key) { return hash(key.get()); }
282 using PtrHash<typename RetainPtr<P>::PtrType>::equal;
283 static bool equal(const RetainPtr<P>& a, const RetainPtr<P>& b) { return a == b; }
284 static bool equal(typename RetainPtr<P>::PtrType a, const RetainPtr<P>& b) { return a == b; }
285 static bool equal(const RetainPtr<P>& a, typename RetainPtr<P>::PtrType b) { return a == b; }
288 template<typename P> struct DefaultHash<RetainPtr<P> > { typedef PtrHash<RetainPtr<P> > Hash; };
296 using WTF::RetainPtr;
297 using WTF::retainPtr;
299 #endif // WTF_RetainPtr_h