|
1 /* |
|
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 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 // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html |
|
22 |
|
23 #ifndef WTF_RefPtr_h |
|
24 #define WTF_RefPtr_h |
|
25 |
|
26 #include <algorithm> |
|
27 #include "AlwaysInline.h" |
|
28 #include "FastAllocBase.h" |
|
29 #include "PassRefPtr.h" |
|
30 |
|
31 namespace WTF { |
|
32 |
|
33 enum PlacementNewAdoptType { PlacementNewAdopt }; |
|
34 |
|
35 template <typename T> class PassRefPtr; |
|
36 template <typename T> class NonNullPassRefPtr; |
|
37 |
|
38 enum HashTableDeletedValueType { HashTableDeletedValue }; |
|
39 |
|
40 template <typename T> class RefPtr : public FastAllocBase { |
|
41 public: |
|
42 ALWAYS_INLINE RefPtr() : m_ptr(0) { } |
|
43 ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } |
|
44 ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { T* ptr = m_ptr; refIfNotNull(ptr); } |
|
45 // see comment in PassRefPtr.h for why this takes const reference |
|
46 template <typename U> RefPtr(const PassRefPtr<U>&); |
|
47 template <typename U> RefPtr(const NonNullPassRefPtr<U>&); |
|
48 |
|
49 // Special constructor for cases where we overwrite an object in place. |
|
50 ALWAYS_INLINE RefPtr(PlacementNewAdoptType) { } |
|
51 |
|
52 // Hash table deleted values, which are only constructed and never copied or destroyed. |
|
53 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
|
54 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); } |
|
55 |
|
56 ALWAYS_INLINE ~RefPtr() { derefIfNotNull(m_ptr); } |
|
57 |
|
58 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { T* ptr = m_ptr; refIfNotNull(ptr); } |
|
59 |
|
60 T* get() const { return m_ptr; } |
|
61 |
|
62 void clear(); |
|
63 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; } |
|
64 |
|
65 T& operator*() const { return *m_ptr; } |
|
66 ALWAYS_INLINE T* operator->() const { return m_ptr; } |
|
67 |
|
68 bool operator!() const { return !m_ptr; } |
|
69 |
|
70 // This conversion operator allows implicit conversion to bool but not to other integer types. |
|
71 typedef T* (RefPtr::*UnspecifiedBoolType); |
|
72 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; } |
|
73 |
|
74 RefPtr& operator=(const RefPtr&); |
|
75 RefPtr& operator=(T*); |
|
76 RefPtr& operator=(const PassRefPtr<T>&); |
|
77 RefPtr& operator=(const NonNullPassRefPtr<T>&); |
|
78 template <typename U> RefPtr& operator=(const RefPtr<U>&); |
|
79 template <typename U> RefPtr& operator=(const PassRefPtr<U>&); |
|
80 template <typename U> RefPtr& operator=(const NonNullPassRefPtr<U>&); |
|
81 |
|
82 void swap(RefPtr&); |
|
83 |
|
84 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
|
85 |
|
86 private: |
|
87 T* m_ptr; |
|
88 }; |
|
89 |
|
90 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o) |
|
91 : m_ptr(o.releaseRef()) |
|
92 { |
|
93 } |
|
94 |
|
95 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const NonNullPassRefPtr<U>& o) |
|
96 : m_ptr(o.releaseRef()) |
|
97 { |
|
98 } |
|
99 |
|
100 template <typename T> inline void RefPtr<T>::clear() |
|
101 { |
|
102 T* ptr = m_ptr; |
|
103 m_ptr = 0; |
|
104 derefIfNotNull(ptr); |
|
105 } |
|
106 |
|
107 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o) |
|
108 { |
|
109 T* optr = o.get(); |
|
110 refIfNotNull(optr); |
|
111 T* ptr = m_ptr; |
|
112 m_ptr = optr; |
|
113 derefIfNotNull(ptr); |
|
114 return *this; |
|
115 } |
|
116 |
|
117 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o) |
|
118 { |
|
119 T* optr = o.get(); |
|
120 refIfNotNull(optr); |
|
121 T* ptr = m_ptr; |
|
122 m_ptr = optr; |
|
123 derefIfNotNull(ptr); |
|
124 return *this; |
|
125 } |
|
126 |
|
127 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr) |
|
128 { |
|
129 refIfNotNull(optr); |
|
130 T* ptr = m_ptr; |
|
131 m_ptr = optr; |
|
132 derefIfNotNull(ptr); |
|
133 return *this; |
|
134 } |
|
135 |
|
136 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o) |
|
137 { |
|
138 T* ptr = m_ptr; |
|
139 m_ptr = o.releaseRef(); |
|
140 derefIfNotNull(ptr); |
|
141 return *this; |
|
142 } |
|
143 |
|
144 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<T>& o) |
|
145 { |
|
146 T* ptr = m_ptr; |
|
147 m_ptr = o.releaseRef(); |
|
148 derefIfNotNull(ptr); |
|
149 return *this; |
|
150 } |
|
151 |
|
152 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o) |
|
153 { |
|
154 T* ptr = m_ptr; |
|
155 m_ptr = o.releaseRef(); |
|
156 derefIfNotNull(ptr); |
|
157 return *this; |
|
158 } |
|
159 |
|
160 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<U>& o) |
|
161 { |
|
162 T* ptr = m_ptr; |
|
163 m_ptr = o.releaseRef(); |
|
164 derefIfNotNull(ptr); |
|
165 return *this; |
|
166 } |
|
167 |
|
168 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o) |
|
169 { |
|
170 std::swap(m_ptr, o.m_ptr); |
|
171 } |
|
172 |
|
173 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b) |
|
174 { |
|
175 a.swap(b); |
|
176 } |
|
177 |
|
178 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b) |
|
179 { |
|
180 return a.get() == b.get(); |
|
181 } |
|
182 |
|
183 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b) |
|
184 { |
|
185 return a.get() == b; |
|
186 } |
|
187 |
|
188 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b) |
|
189 { |
|
190 return a == b.get(); |
|
191 } |
|
192 |
|
193 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b) |
|
194 { |
|
195 return a.get() != b.get(); |
|
196 } |
|
197 |
|
198 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b) |
|
199 { |
|
200 return a.get() != b; |
|
201 } |
|
202 |
|
203 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b) |
|
204 { |
|
205 return a != b.get(); |
|
206 } |
|
207 |
|
208 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p) |
|
209 { |
|
210 return RefPtr<T>(static_cast<T*>(p.get())); |
|
211 } |
|
212 |
|
213 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p) |
|
214 { |
|
215 return RefPtr<T>(const_cast<T*>(p.get())); |
|
216 } |
|
217 |
|
218 template <typename T> inline T* getPtr(const RefPtr<T>& p) |
|
219 { |
|
220 return p.get(); |
|
221 } |
|
222 |
|
223 } // namespace WTF |
|
224 |
|
225 using WTF::RefPtr; |
|
226 using WTF::static_pointer_cast; |
|
227 using WTF::const_pointer_cast; |
|
228 |
|
229 #endif // WTF_RefPtr_h |