]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/PassRefPtr.h
2 * Copyright (C) 2005, 2006, 2007 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 #ifndef WTF_PassRefPtr_h
22 #define WTF_PassRefPtr_h
24 #include "AlwaysInline.h"
28 template<typename T
> class RefPtr
;
29 template<typename T
> class PassRefPtr
;
30 template <typename T
> PassRefPtr
<T
> adoptRef(T
*);
32 template<typename T
> class PassRefPtr
{
34 PassRefPtr() : m_ptr(0) {}
35 PassRefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->ref(); }
36 // It somewhat breaks the type system to allow transfer of ownership out of
37 // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
38 // temporaries, and we don't really have a need to use real const PassRefPtrs
40 PassRefPtr(const PassRefPtr
& o
) : m_ptr(o
.releaseRef()) {}
41 template <typename U
> PassRefPtr(const PassRefPtr
<U
>& o
) : m_ptr(o
.releaseRef()) { }
43 ALWAYS_INLINE
~PassRefPtr() { if (T
* ptr
= m_ptr
) ptr
->deref(); }
46 PassRefPtr(const RefPtr
<U
>& o
) : m_ptr(o
.get()) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
48 T
* get() const { return m_ptr
; }
50 void clear() { if (T
* ptr
= m_ptr
) ptr
->deref(); m_ptr
= 0; }
51 T
* releaseRef() const { T
* tmp
= m_ptr
; m_ptr
= 0; return tmp
; }
53 T
& operator*() const { return *m_ptr
; }
54 T
* operator->() const { return m_ptr
; }
56 bool operator!() const { return !m_ptr
; }
58 // This conversion operator allows implicit conversion to bool but not to other integer types.
60 operator bool() const { return m_ptr
; }
62 typedef T
* PassRefPtr::*UnspecifiedBoolType
;
63 operator UnspecifiedBoolType() const { return m_ptr
? &PassRefPtr::m_ptr
: 0; }
65 PassRefPtr
& operator=(T
*);
66 PassRefPtr
& operator=(const PassRefPtr
&);
67 template <typename U
> PassRefPtr
& operator=(const PassRefPtr
<U
>&);
68 template <typename U
> PassRefPtr
& operator=(const RefPtr
<U
>&);
70 friend PassRefPtr adoptRef
<T
>(T
*);
72 // adopting constructor
73 PassRefPtr(T
* ptr
, bool) : m_ptr(ptr
) {}
77 template <typename T
> template <typename U
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
89 template <typename T
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(T
* optr
)
100 template <typename T
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const PassRefPtr
<T
>& ref
)
103 m_ptr
= ref
.releaseRef();
109 template <typename T
> template <typename U
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const PassRefPtr
<U
>& ref
)
112 m_ptr
= ref
.releaseRef();
118 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
120 return a
.get() == b
.get();
123 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, const RefPtr
<U
>& b
)
125 return a
.get() == b
.get();
128 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
130 return a
.get() == b
.get();
133 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, U
* b
)
138 template <typename T
, typename U
> inline bool operator==(T
* a
, const PassRefPtr
<U
>& b
)
143 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
145 return a
.get() != b
.get();
148 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, const RefPtr
<U
>& b
)
150 return a
.get() != b
.get();
153 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
155 return a
.get() != b
.get();
158 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, U
* b
)
163 template <typename T
, typename U
> inline bool operator!=(T
* a
, const PassRefPtr
<U
>& b
)
168 template <typename T
> inline PassRefPtr
<T
> adoptRef(T
* p
)
170 return PassRefPtr
<T
>(p
, true);
173 template <typename T
, typename U
> inline PassRefPtr
<T
> static_pointer_cast(const PassRefPtr
<U
>& p
)
175 return adoptRef(static_cast<T
*>(p
.releaseRef()));
178 template <typename T
, typename U
> inline PassRefPtr
<T
> const_pointer_cast(const PassRefPtr
<U
>& p
)
180 return adoptRef(const_cast<T
*>(p
.releaseRef()));
183 template <typename T
> inline T
* getPtr(const PassRefPtr
<T
>& p
)
190 using WTF::PassRefPtr
;
192 using WTF::static_pointer_cast
;
193 using WTF::const_pointer_cast
;
195 #endif // WTF_PassRefPtr_h