]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/PassRefPtr.h
1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef WTF_PassRefPtr_h
23 #define WTF_PassRefPtr_h
25 #include "AlwaysInline.h"
29 template<typename T
> class RefPtr
;
30 template<typename T
> class PassRefPtr
;
31 template <typename T
> PassRefPtr
<T
> adoptRef(T
*);
33 template<typename T
> class PassRefPtr
{
35 PassRefPtr() : m_ptr(0) {}
36 PassRefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->ref(); }
37 // It somewhat breaks the type system to allow transfer of ownership out of
38 // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
39 // temporaries, and we don't really have a need to use real const PassRefPtrs
41 PassRefPtr(const PassRefPtr
& o
) : m_ptr(o
.releaseRef()) {}
42 template <typename U
> PassRefPtr(const PassRefPtr
<U
>& o
) : m_ptr(o
.releaseRef()) { }
44 ALWAYS_INLINE
~PassRefPtr() { if (T
* ptr
= m_ptr
) ptr
->deref(); }
47 PassRefPtr(const RefPtr
<U
>& o
) : m_ptr(o
.get()) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
49 T
* get() const { return m_ptr
; }
51 void clear() { if (T
* ptr
= m_ptr
) ptr
->deref(); m_ptr
= 0; }
52 T
* releaseRef() const { T
* tmp
= m_ptr
; m_ptr
= 0; return tmp
; }
54 T
& operator*() const { return *m_ptr
; }
55 T
* operator->() const { return m_ptr
; }
57 bool operator!() const { return !m_ptr
; }
59 // This conversion operator allows implicit conversion to bool but not to other integer types.
60 typedef T
* PassRefPtr::*UnspecifiedBoolType
;
61 operator UnspecifiedBoolType() const { return m_ptr
? &PassRefPtr::m_ptr
: 0; }
63 PassRefPtr
& operator=(T
*);
64 PassRefPtr
& operator=(const PassRefPtr
&);
65 template <typename U
> PassRefPtr
& operator=(const PassRefPtr
<U
>&);
66 template <typename U
> PassRefPtr
& operator=(const RefPtr
<U
>&);
68 friend PassRefPtr adoptRef
<T
>(T
*);
70 // adopting constructor
71 PassRefPtr(T
* ptr
, bool) : m_ptr(ptr
) {}
75 template <typename T
> template <typename U
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
87 template <typename T
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(T
* optr
)
98 template <typename T
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const PassRefPtr
<T
>& ref
)
101 m_ptr
= ref
.releaseRef();
107 template <typename T
> template <typename U
> inline PassRefPtr
<T
>& PassRefPtr
<T
>::operator=(const PassRefPtr
<U
>& ref
)
110 m_ptr
= ref
.releaseRef();
116 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
118 return a
.get() == b
.get();
121 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, const RefPtr
<U
>& b
)
123 return a
.get() == b
.get();
126 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
128 return a
.get() == b
.get();
131 template <typename T
, typename U
> inline bool operator==(const PassRefPtr
<T
>& a
, U
* b
)
136 template <typename T
, typename U
> inline bool operator==(T
* a
, const PassRefPtr
<U
>& b
)
141 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
143 return a
.get() != b
.get();
146 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, const RefPtr
<U
>& b
)
148 return a
.get() != b
.get();
151 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const PassRefPtr
<U
>& b
)
153 return a
.get() != b
.get();
156 template <typename T
, typename U
> inline bool operator!=(const PassRefPtr
<T
>& a
, U
* b
)
161 template <typename T
, typename U
> inline bool operator!=(T
* a
, const PassRefPtr
<U
>& b
)
166 template <typename T
> inline PassRefPtr
<T
> adoptRef(T
* p
)
168 return PassRefPtr
<T
>(p
, true);
171 template <typename T
, typename U
> inline PassRefPtr
<T
> static_pointer_cast(const PassRefPtr
<U
>& p
)
173 return adoptRef(static_cast<T
*>(p
.releaseRef()));
176 template <typename T
, typename U
> inline PassRefPtr
<T
> const_pointer_cast(const PassRefPtr
<U
>& p
)
178 return adoptRef(const_cast<T
*>(p
.releaseRef()));
181 template <typename T
> inline T
* getPtr(const PassRefPtr
<T
>& p
)
188 using WTF::PassRefPtr
;
190 using WTF::static_pointer_cast
;
191 using WTF::const_pointer_cast
;
193 #endif // WTF_PassRefPtr_h