1/*
2 * Copyright (C) 2005, 2006, 2007, 2008 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 WTF_RefPtr_h
22#define WTF_RefPtr_h
23
24#include <algorithm>
25#include "AlwaysInline.h"
26#include "FastAllocBase.h"
27#if COMPILER(WINSCW)
28#include "PassRefPtr.h"
29#endif
30
31namespace 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 RefPtr() : m_ptr(0) { }
43 RefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
44 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->ref(); }
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 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#if COMPILER(WINSCW)
57 ~RefPtr() { if (T* ptr = m_ptr) derefIfNotNull<T>(ptr); }
58#else
59 ~RefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
60#endif
61
62 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
63
64 T* get() const { return m_ptr; }
65
66#if COMPILER(WINSCW)
67 void clear() { if (T* ptr = m_ptr) derefIfNotNull<T>(ptr); m_ptr = 0; }
68#else
69 void clear() { if (T* ptr = m_ptr) ptr->deref(); m_ptr = 0; }
70#endif
71 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
72
73 T& operator*() const { return *m_ptr; }
74 ALWAYS_INLINE T* operator->() const { return m_ptr; }
75
76 bool operator!() const { return !m_ptr; }
77
78 // This conversion operator allows implicit conversion to bool but not to other integer types.
79 typedef T* (RefPtr::*UnspecifiedBoolType);
80 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; }
81
82 RefPtr& operator=(const RefPtr&);
83 RefPtr& operator=(T*);
84 RefPtr& operator=(const PassRefPtr<T>&);
85 RefPtr& operator=(const NonNullPassRefPtr<T>&);
86 template <typename U> RefPtr& operator=(const RefPtr<U>&);
87 template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
88 template <typename U> RefPtr& operator=(const NonNullPassRefPtr<U>&);
89
90 void swap(RefPtr&);
91
92 private:
93 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
94
95 T* m_ptr;
96 };
97
98 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
99 : m_ptr(o.releaseRef())
100 {
101 }
102
103 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const NonNullPassRefPtr<U>& o)
104 : m_ptr(o.releaseRef())
105 {
106 }
107
108 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
109 {
110 T* optr = o.get();
111 if (optr)
112 optr->ref();
113 T* ptr = m_ptr;
114 m_ptr = optr;
115 if (ptr)
116 ptr->deref();
117 return *this;
118 }
119
120 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
121 {
122 T* optr = o.get();
123 if (optr)
124 optr->ref();
125 T* ptr = m_ptr;
126 m_ptr = optr;
127 if (ptr)
128 ptr->deref();
129 return *this;
130 }
131
132 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
133 {
134 if (optr)
135 optr->ref();
136 T* ptr = m_ptr;
137 m_ptr = optr;
138 if (ptr)
139 ptr->deref();
140 return *this;
141 }
142
143 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
144 {
145 T* ptr = m_ptr;
146 m_ptr = o.releaseRef();
147 if (ptr)
148 ptr->deref();
149 return *this;
150 }
151
152 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<T>& o)
153 {
154 T* ptr = m_ptr;
155 m_ptr = o.releaseRef();
156 if (ptr)
157 ptr->deref();
158 return *this;
159 }
160
161 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
162 {
163 T* ptr = m_ptr;
164 m_ptr = o.releaseRef();
165 if (ptr)
166 ptr->deref();
167 return *this;
168 }
169
170 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<U>& o)
171 {
172 T* ptr = m_ptr;
173 m_ptr = o.releaseRef();
174 if (ptr)
175 ptr->deref();
176 return *this;
177 }
178
179 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
180 {
181 std::swap(m_ptr, o.m_ptr);
182 }
183
184 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
185 {
186 a.swap(b);
187 }
188
189 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
190 {
191 return a.get() == b.get();
192 }
193
194 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
195 {
196 return a.get() == b;
197 }
198
199 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
200 {
201 return a == b.get();
202 }
203
204 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
205 {
206 return a.get() != b.get();
207 }
208
209 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
210 {
211 return a.get() != b;
212 }
213
214 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
215 {
216 return a != b.get();
217 }
218
219 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
220 {
221 return RefPtr<T>(static_cast<T*>(p.get()));
222 }
223
224 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
225 {
226 return RefPtr<T>(const_cast<T*>(p.get()));
227 }
228
229 template <typename T> inline T* getPtr(const RefPtr<T>& p)
230 {
231 return p.get();
232 }
233
234} // namespace WTF
235
236using WTF::RefPtr;
237using WTF::static_pointer_cast;
238using WTF::const_pointer_cast;
239
240#endif // WTF_RefPtr_h
241