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.
25 #include "AlwaysInline.h"
26 #include "FastAllocBase.h"
30 enum PlacementNewAdoptType
{ PlacementNewAdopt
};
32 template <typename T
> class PassRefPtr
;
33 template <typename T
> class NonNullPassRefPtr
;
35 enum HashTableDeletedValueType
{ HashTableDeletedValue
};
37 template <typename T
> class RefPtr
: public FastAllocBase
{
39 RefPtr() : m_ptr(0) { }
40 RefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->ref(); }
41 RefPtr(const RefPtr
& o
) : m_ptr(o
.m_ptr
) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
42 // see comment in PassRefPtr.h for why this takes const reference
43 template <typename U
> RefPtr(const PassRefPtr
<U
>&);
44 template <typename U
> RefPtr(const NonNullPassRefPtr
<U
>&);
46 // Special constructor for cases where we overwrite an object in place.
47 RefPtr(PlacementNewAdoptType
) { }
49 // Hash table deleted values, which are only constructed and never copied or destroyed.
50 RefPtr(HashTableDeletedValueType
) : m_ptr(hashTableDeletedValue()) { }
51 bool isHashTableDeletedValue() const { return m_ptr
== hashTableDeletedValue(); }
53 ~RefPtr() { if (T
* ptr
= m_ptr
) ptr
->deref(); }
55 template <typename U
> RefPtr(const RefPtr
<U
>& o
) : m_ptr(o
.get()) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
57 T
* get() const { return m_ptr
; }
59 void clear() { if (T
* ptr
= m_ptr
) ptr
->deref(); m_ptr
= 0; }
60 PassRefPtr
<T
> release() { PassRefPtr
<T
> tmp
= adoptRef(m_ptr
); m_ptr
= 0; return tmp
; }
62 T
& operator*() const { return *m_ptr
; }
63 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
65 bool operator!() const { return !m_ptr
; }
67 // This conversion operator allows implicit conversion to bool but not to other integer types.
69 operator bool() const { return m_ptr
; }
71 typedef T
* RefPtr::*UnspecifiedBoolType
;
72 operator UnspecifiedBoolType() const { return m_ptr
? &RefPtr::m_ptr
: 0; }
75 RefPtr
& operator=(const RefPtr
&);
76 RefPtr
& operator=(T
*);
77 RefPtr
& operator=(const PassRefPtr
<T
>&);
78 RefPtr
& operator=(const NonNullPassRefPtr
<T
>&);
79 template <typename U
> RefPtr
& operator=(const RefPtr
<U
>&);
80 template <typename U
> RefPtr
& operator=(const PassRefPtr
<U
>&);
81 template <typename U
> RefPtr
& operator=(const NonNullPassRefPtr
<U
>&);
86 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
91 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const PassRefPtr
<U
>& o
)
92 : m_ptr(o
.releaseRef())
96 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const NonNullPassRefPtr
<U
>& o
)
97 : m_ptr(o
.releaseRef())
101 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<T
>& o
)
113 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
125 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(T
* optr
)
136 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<T
>& o
)
139 m_ptr
= o
.releaseRef();
145 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const NonNullPassRefPtr
<T
>& o
)
148 m_ptr
= o
.releaseRef();
154 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<U
>& o
)
157 m_ptr
= o
.releaseRef();
163 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const NonNullPassRefPtr
<U
>& o
)
166 m_ptr
= o
.releaseRef();
172 template <class T
> inline void RefPtr
<T
>::swap(RefPtr
<T
>& o
)
174 std::swap(m_ptr
, o
.m_ptr
);
177 template <class T
> inline void swap(RefPtr
<T
>& a
, RefPtr
<T
>& b
)
182 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
184 return a
.get() == b
.get();
187 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, U
* b
)
192 template <typename T
, typename U
> inline bool operator==(T
* a
, const RefPtr
<U
>& b
)
197 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
199 return a
.get() != b
.get();
202 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, U
* b
)
207 template <typename T
, typename U
> inline bool operator!=(T
* a
, const RefPtr
<U
>& b
)
212 template <typename T
, typename U
> inline RefPtr
<T
> static_pointer_cast(const RefPtr
<U
>& p
)
214 return RefPtr
<T
>(static_cast<T
*>(p
.get()));
217 template <typename T
, typename U
> inline RefPtr
<T
> const_pointer_cast(const RefPtr
<U
>& p
)
219 return RefPtr
<T
>(const_cast<T
*>(p
.get()));
222 template <typename T
> inline T
* getPtr(const RefPtr
<T
>& p
)
230 using WTF::static_pointer_cast
;
231 using WTF::const_pointer_cast
;
233 #endif // WTF_RefPtr_h