OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. | |
3 * | |
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. | |
8 * | |
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. | |
13 * | |
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. | |
18 * | |
19 */ | |
20 | |
21 #ifndef RetainPtr_h | |
22 #define RetainPtr_h | |
23 | |
24 #include <wtf/HashTraits.h> | |
25 #include <wtf/NullPtr.h> | |
26 #include <wtf/TypeTraits.h> | |
27 #include <algorithm> | |
28 | |
29 #if USE(CF) | |
30 #include <CoreFoundation/CoreFoundation.h> | |
31 #endif | |
32 | |
33 #ifdef __OBJC__ | |
34 #import <Foundation/Foundation.h> | |
35 #endif | |
36 | |
37 #ifndef CF_RELEASES_ARGUMENT | |
38 #define CF_RELEASES_ARGUMENT | |
39 #endif | |
40 | |
41 #ifndef NS_RELEASES_ARGUMENT | |
42 #define NS_RELEASES_ARGUMENT | |
43 #endif | |
44 | |
45 namespace WTF { | |
46 | |
47 // Unlike most most of our smart pointers, RetainPtr can take either the poi
nter type or the pointed-to type, | |
48 // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work. | |
49 | |
50 enum AdoptCFTag { AdoptCF }; | |
51 enum AdoptNSTag { AdoptNS }; | |
52 | |
53 #ifdef __OBJC__ | |
54 inline void adoptNSReference(id ptr) | |
55 { | |
56 if (ptr) { | |
57 CFRetain(ptr); | |
58 [ptr release]; | |
59 } | |
60 } | |
61 #endif | |
62 | |
63 template<typename T> class RetainPtr { | |
64 public: | |
65 typedef typename RemovePointer<T>::Type ValueType; | |
66 typedef ValueType* PtrType; | |
67 | |
68 RetainPtr() : m_ptr(0) {} | |
69 RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); } | |
70 | |
71 RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { } | |
72 RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr);
} | |
73 | |
74 RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr
) CFRetain(ptr); } | |
75 | |
76 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
77 RetainPtr(RetainPtr&& o) : m_ptr(o.leakRef()) { } | |
78 #endif | |
79 | |
80 // Hash table deleted values, which are only constructed and never copie
d or destroyed. | |
81 RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {
} | |
82 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } | |
83 | |
84 ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); } | |
85 | |
86 template<typename U> RetainPtr(const RetainPtr<U>&); | |
87 | |
88 void clear(); | |
89 PtrType leakRef() WARN_UNUSED_RETURN; | |
90 | |
91 PtrType get() const { return m_ptr; } | |
92 PtrType operator->() const { return m_ptr; } | |
93 #if COMPILER_SUPPORTS(CXX_EXPLICIT_CONVERSIONS) | |
94 explicit operator PtrType() const { return m_ptr; } | |
95 #endif | |
96 | |
97 bool operator!() const { return !m_ptr; } | |
98 | |
99 // This conversion operator allows implicit conversion to bool but not t
o other integer types. | |
100 typedef PtrType RetainPtr::*UnspecifiedBoolType; | |
101 operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr
: 0; } | |
102 | |
103 RetainPtr& operator=(const RetainPtr&); | |
104 template<typename U> RetainPtr& operator=(const RetainPtr<U>&); | |
105 RetainPtr& operator=(PtrType); | |
106 template<typename U> RetainPtr& operator=(U*); | |
107 | |
108 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
109 RetainPtr& operator=(RetainPtr&&); | |
110 template<typename U> RetainPtr& operator=(RetainPtr<U>&&); | |
111 #endif | |
112 | |
113 #if !COMPILER_SUPPORTS(CXX_NULLPTR) | |
114 RetainPtr& operator=(std::nullptr_t) { clear(); return *this; } | |
115 #endif | |
116 | |
117 void adoptCF(PtrType); | |
118 void adoptNS(PtrType); | |
119 | |
120 void swap(RetainPtr&); | |
121 | |
122 private: | |
123 static PtrType hashTableDeletedValue() { return reinterpret_cast<PtrType
>(-1); } | |
124 | |
125 PtrType m_ptr; | |
126 }; | |
127 | |
128 template<typename T> template<typename U> inline RetainPtr<T>::RetainPtr(con
st RetainPtr<U>& o) | |
129 : m_ptr(o.get()) | |
130 { | |
131 if (PtrType ptr = m_ptr) | |
132 CFRetain(ptr); | |
133 } | |
134 | |
135 template<typename T> inline void RetainPtr<T>::clear() | |
136 { | |
137 if (PtrType ptr = m_ptr) { | |
138 m_ptr = 0; | |
139 CFRelease(ptr); | |
140 } | |
141 } | |
142 | |
143 template<typename T> inline typename RetainPtr<T>::PtrType RetainPtr<T>::lea
kRef() | |
144 { | |
145 PtrType ptr = m_ptr; | |
146 m_ptr = 0; | |
147 return ptr; | |
148 } | |
149 | |
150 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const Reta
inPtr<T>& o) | |
151 { | |
152 PtrType optr = o.get(); | |
153 if (optr) | |
154 CFRetain(optr); | |
155 PtrType ptr = m_ptr; | |
156 m_ptr = optr; | |
157 if (ptr) | |
158 CFRelease(ptr); | |
159 return *this; | |
160 } | |
161 | |
162 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>:
:operator=(const RetainPtr<U>& o) | |
163 { | |
164 PtrType optr = o.get(); | |
165 if (optr) | |
166 CFRetain(optr); | |
167 PtrType ptr = m_ptr; | |
168 m_ptr = optr; | |
169 if (ptr) | |
170 CFRelease(ptr); | |
171 return *this; | |
172 } | |
173 | |
174 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType op
tr) | |
175 { | |
176 if (optr) | |
177 CFRetain(optr); | |
178 PtrType ptr = m_ptr; | |
179 m_ptr = optr; | |
180 if (ptr) | |
181 CFRelease(ptr); | |
182 return *this; | |
183 } | |
184 | |
185 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>:
:operator=(U* optr) | |
186 { | |
187 if (optr) | |
188 CFRetain(optr); | |
189 PtrType ptr = m_ptr; | |
190 m_ptr = optr; | |
191 if (ptr) | |
192 CFRelease(ptr); | |
193 return *this; | |
194 } | |
195 | |
196 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
197 template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<
T>&& o) | |
198 { | |
199 adoptCF(o.leakRef()); | |
200 return *this; | |
201 } | |
202 | |
203 template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>:
:operator=(RetainPtr<U>&& o) | |
204 { | |
205 adoptCF(o.leakRef()); | |
206 return *this; | |
207 } | |
208 #endif | |
209 | |
210 template<typename T> inline void RetainPtr<T>::adoptCF(PtrType optr) | |
211 { | |
212 PtrType ptr = m_ptr; | |
213 m_ptr = optr; | |
214 if (ptr) | |
215 CFRelease(ptr); | |
216 } | |
217 | |
218 template<typename T> inline void RetainPtr<T>::adoptNS(PtrType optr) | |
219 { | |
220 adoptNSReference(optr); | |
221 | |
222 PtrType ptr = m_ptr; | |
223 m_ptr = optr; | |
224 if (ptr) | |
225 CFRelease(ptr); | |
226 } | |
227 | |
228 template<typename T> inline void RetainPtr<T>::swap(RetainPtr<T>& o) | |
229 { | |
230 std::swap(m_ptr, o.m_ptr); | |
231 } | |
232 | |
233 template<typename T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b) | |
234 { | |
235 a.swap(b); | |
236 } | |
237 | |
238 template<typename T, typename U> inline bool operator==(const RetainPtr<T>&
a, const RetainPtr<U>& b) | |
239 { | |
240 return a.get() == b.get(); | |
241 } | |
242 | |
243 template<typename T, typename U> inline bool operator==(const RetainPtr<T>&
a, U* b) | |
244 { | |
245 return a.get() == b; | |
246 } | |
247 | |
248 template<typename T, typename U> inline bool operator==(T* a, const RetainPt
r<U>& b) | |
249 { | |
250 return a == b.get(); | |
251 } | |
252 | |
253 template<typename T, typename U> inline bool operator!=(const RetainPtr<T>&
a, const RetainPtr<U>& b) | |
254 { | |
255 return a.get() != b.get(); | |
256 } | |
257 | |
258 template<typename T, typename U> inline bool operator!=(const RetainPtr<T>&
a, U* b) | |
259 { | |
260 return a.get() != b; | |
261 } | |
262 | |
263 template<typename T, typename U> inline bool operator!=(T* a, const RetainPt
r<U>& b) | |
264 { | |
265 return a != b.get(); | |
266 } | |
267 | |
268 template<typename T> inline RetainPtr<T> adoptCF(T CF_RELEASES_ARGUMENT) WAR
N_UNUSED_RETURN; | |
269 template<typename T> inline RetainPtr<T> adoptCF(T o) | |
270 { | |
271 return RetainPtr<T>(AdoptCF, o); | |
272 } | |
273 | |
274 template<typename T> inline RetainPtr<T> adoptNS(T NS_RELEASES_ARGUMENT) WAR
N_UNUSED_RETURN; | |
275 template<typename T> inline RetainPtr<T> adoptNS(T o) | |
276 { | |
277 return RetainPtr<T>(AdoptNS, o); | |
278 } | |
279 | |
280 // Helper function for creating a RetainPtr using template argument deductio
n. | |
281 template<typename T> inline RetainPtr<T> retainPtr(T) WARN_UNUSED_RETURN; | |
282 template<typename T> inline RetainPtr<T> retainPtr(T o) | |
283 { | |
284 return RetainPtr<T>(o); | |
285 } | |
286 | |
287 template<typename P> struct HashTraits<RetainPtr<P> > : SimpleClassHashTrait
s<RetainPtr<P> > { }; | |
288 | |
289 template<typename P> struct PtrHash<RetainPtr<P> > : PtrHash<typename Retain
Ptr<P>::PtrType> { | |
290 using PtrHash<typename RetainPtr<P>::PtrType>::hash; | |
291 static unsigned hash(const RetainPtr<P>& key) { return hash(key.get());
} | |
292 using PtrHash<typename RetainPtr<P>::PtrType>::equal; | |
293 static bool equal(const RetainPtr<P>& a, const RetainPtr<P>& b) { return
a == b; } | |
294 static bool equal(typename RetainPtr<P>::PtrType a, const RetainPtr<P>&
b) { return a == b; } | |
295 static bool equal(const RetainPtr<P>& a, typename RetainPtr<P>::PtrType
b) { return a == b; } | |
296 }; | |
297 | |
298 template<typename P> struct DefaultHash<RetainPtr<P> > { typedef PtrHash<Ret
ainPtr<P> > Hash; }; | |
299 | |
300 template <typename P> | |
301 struct RetainPtrObjectHashTraits : GenericHashTraits<RetainPtr<P> > { | |
302 static const bool emptyValueIsZero = true; | |
303 static const RetainPtr<P>& emptyValue() | |
304 { | |
305 static RetainPtr<P>& null = *(new RetainPtr<P>); | |
306 return null; | |
307 } | |
308 static const bool needsDestruction = true; | |
309 static void constructDeletedValue(RetainPtr<P>& slot) { new (&slot) Reta
inPtr<P>(HashTableDeletedValue); } | |
310 static bool isDeletedValue(const RetainPtr<P>& value) { return value.isH
ashTableDeletedValue(); } | |
311 }; | |
312 | |
313 template <typename P> | |
314 struct RetainPtrObjectHash { | |
315 static unsigned hash(const RetainPtr<P>& o) | |
316 { | |
317 ASSERT_WITH_MESSAGE(o.get(), "attempt to use null RetainPtr in HashT
able"); | |
318 return CFHash(o.get()); | |
319 } | |
320 static bool equal(const RetainPtr<P>& a, const RetainPtr<P>& b) | |
321 { | |
322 return CFEqual(a.get(), b.get()); | |
323 } | |
324 static const bool safeToCompareToEmptyOrDeleted = false; | |
325 }; | |
326 } // namespace WTF | |
327 | |
328 using WTF::AdoptCF; | |
329 using WTF::AdoptNS; | |
330 using WTF::adoptCF; | |
331 using WTF::adoptNS; | |
332 using WTF::RetainPtr; | |
333 using WTF::retainPtr; | |
334 | |
335 #endif // WTF_RetainPtr_h | |
OLD | NEW |