2 * Copyright (C) 2005, 2006, 2007, 2008 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.
21 // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html
27 #include "AlwaysInline.h"
28 #include "FastAllocBase.h"
29 #include "PassRefPtr.h"
33 enum PlacementNewAdoptType
{ PlacementNewAdopt
};
35 template <typename T
> class PassRefPtr
;
36 template <typename T
> class NonNullPassRefPtr
;
38 enum HashTableDeletedValueType
{ HashTableDeletedValue
};
40 template <typename T
> class RefPtr
: public FastAllocBase
{
42 RefPtr() : m_ptr(0) { }
43 RefPtr(T
* ptr
) : m_ptr(ptr
) { refIfNotNull(ptr
); }
44 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
>&);
49 // Special constructor for cases where we overwrite an object in place.
50 RefPtr(PlacementNewAdoptType
) { }
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(); }
56 ~RefPtr() { derefIfNotNull(m_ptr
); }
58 template <typename U
> RefPtr(const RefPtr
<U
>& o
) : m_ptr(o
.get()) { T
* ptr
= m_ptr
; refIfNotNull(ptr
); }
60 T
* get() const { return m_ptr
; }
62 void clear() { derefIfNotNull(m_ptr
); m_ptr
= 0; }
63 PassRefPtr
<T
> release() { PassRefPtr
<T
> tmp
= adoptRef(m_ptr
); m_ptr
= 0; return tmp
; }
65 T
& operator*() const { return *m_ptr
; }
66 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
68 bool operator!() const { return !m_ptr
; }
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; }
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
>&);
84 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
90 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const PassRefPtr
<U
>& o
)
91 : m_ptr(o
.releaseRef())
95 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const NonNullPassRefPtr
<U
>& o
)
96 : m_ptr(o
.releaseRef())
100 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<T
>& o
)
110 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
120 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(T
* optr
)
129 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<T
>& o
)
132 m_ptr
= o
.releaseRef();
137 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const NonNullPassRefPtr
<T
>& o
)
140 m_ptr
= o
.releaseRef();
145 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<U
>& o
)
148 m_ptr
= o
.releaseRef();
153 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const NonNullPassRefPtr
<U
>& o
)
156 m_ptr
= o
.releaseRef();
161 template <class T
> inline void RefPtr
<T
>::swap(RefPtr
<T
>& o
)
163 std::swap(m_ptr
, o
.m_ptr
);
166 template <class T
> inline void swap(RefPtr
<T
>& a
, RefPtr
<T
>& b
)
171 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
173 return a
.get() == b
.get();
176 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, U
* b
)
181 template <typename T
, typename U
> inline bool operator==(T
* a
, const RefPtr
<U
>& b
)
186 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
188 return a
.get() != b
.get();
191 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, U
* b
)
196 template <typename T
, typename U
> inline bool operator!=(T
* a
, const RefPtr
<U
>& b
)
201 template <typename T
, typename U
> inline RefPtr
<T
> static_pointer_cast(const RefPtr
<U
>& p
)
203 return RefPtr
<T
>(static_cast<T
*>(p
.get()));
206 template <typename T
, typename U
> inline RefPtr
<T
> const_pointer_cast(const RefPtr
<U
>& p
)
208 return RefPtr
<T
>(const_cast<T
*>(p
.get()));
211 template <typename T
> inline T
* getPtr(const RefPtr
<T
>& p
)
219 using WTF::static_pointer_cast
;
220 using WTF::const_pointer_cast
;
222 #endif // WTF_RefPtr_h