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
;
34 enum HashTableDeletedValueType
{ HashTableDeletedValue
};
36 template <typename T
> class RefPtr
: public FastAllocBase
{
38 RefPtr() : m_ptr(0) { }
39 RefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->ref(); }
40 RefPtr(const RefPtr
& o
) : m_ptr(o
.m_ptr
) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
41 // see comment in PassRefPtr.h for why this takes const reference
42 template <typename U
> RefPtr(const PassRefPtr
<U
>&);
44 // Special constructor for cases where we overwrite an object in place.
45 RefPtr(PlacementNewAdoptType
) { }
47 // Hash table deleted values, which are only constructed and never copied or destroyed.
48 RefPtr(HashTableDeletedValueType
) : m_ptr(hashTableDeletedValue()) { }
49 bool isHashTableDeletedValue() const { return m_ptr
== hashTableDeletedValue(); }
51 ~RefPtr() { if (T
* ptr
= m_ptr
) ptr
->deref(); }
53 template <typename U
> RefPtr(const RefPtr
<U
>& o
) : m_ptr(o
.get()) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
55 T
* get() const { return m_ptr
; }
57 void clear() { if (T
* ptr
= m_ptr
) ptr
->deref(); m_ptr
= 0; }
58 PassRefPtr
<T
> release() { PassRefPtr
<T
> tmp
= adoptRef(m_ptr
); m_ptr
= 0; return tmp
; }
60 T
& operator*() const { return *m_ptr
; }
61 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
63 bool operator!() const { return !m_ptr
; }
65 // This conversion operator allows implicit conversion to bool but not to other integer types.
67 operator bool() const { return m_ptr
; }
69 typedef T
* RefPtr::*UnspecifiedBoolType
;
70 operator UnspecifiedBoolType() const { return m_ptr
? &RefPtr::m_ptr
: 0; }
73 RefPtr
& operator=(const RefPtr
&);
74 RefPtr
& operator=(T
*);
75 RefPtr
& operator=(const PassRefPtr
<T
>&);
76 template <typename U
> RefPtr
& operator=(const RefPtr
<U
>&);
77 template <typename U
> RefPtr
& operator=(const PassRefPtr
<U
>&);
82 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
87 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const PassRefPtr
<U
>& o
)
88 : m_ptr(o
.releaseRef())
92 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<T
>& o
)
104 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
116 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(T
* optr
)
127 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<T
>& o
)
130 m_ptr
= o
.releaseRef();
136 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<U
>& o
)
139 m_ptr
= o
.releaseRef();
145 template <class T
> inline void RefPtr
<T
>::swap(RefPtr
<T
>& o
)
147 std::swap(m_ptr
, o
.m_ptr
);
150 template <class T
> inline void swap(RefPtr
<T
>& a
, RefPtr
<T
>& b
)
155 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
157 return a
.get() == b
.get();
160 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, U
* b
)
165 template <typename T
, typename U
> inline bool operator==(T
* a
, const RefPtr
<U
>& b
)
170 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
172 return a
.get() != b
.get();
175 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, U
* b
)
180 template <typename T
, typename U
> inline bool operator!=(T
* a
, const RefPtr
<U
>& b
)
185 template <typename T
, typename U
> inline RefPtr
<T
> static_pointer_cast(const RefPtr
<U
>& p
)
187 return RefPtr
<T
>(static_cast<T
*>(p
.get()));
190 template <typename T
, typename U
> inline RefPtr
<T
> const_pointer_cast(const RefPtr
<U
>& p
)
192 return RefPtr
<T
>(const_cast<T
*>(p
.get()));
195 template <typename T
> inline T
* getPtr(const RefPtr
<T
>& p
)
203 using WTF::static_pointer_cast
;
204 using WTF::const_pointer_cast
;
206 #endif // WTF_RefPtr_h