]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/RefPtr.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.
26 #include "AlwaysInline.h"
30 enum PlacementNewAdoptType
{ PlacementNewAdopt
};
32 template <typename T
> class PassRefPtr
;
34 template <typename T
> class RefPtr
{
36 RefPtr() : m_ptr(0) {}
37 RefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->ref(); }
38 RefPtr(const RefPtr
& o
) : m_ptr(o
.m_ptr
) { if (T
* ptr
= m_ptr
) ptr
->ref(); }
39 // see comment in PassRefPtr.h for why this takes const reference
40 template <typename U
> RefPtr(const PassRefPtr
<U
>&);
42 // Special constructor for cases where we overwrite an object in place.
43 RefPtr(PlacementNewAdoptType
) { }
45 ~RefPtr() { if (T
* ptr
= m_ptr
) ptr
->deref(); }
47 template <typename U
> RefPtr(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 PassRefPtr
<T
> release() { PassRefPtr
<T
> tmp
= adoptRef(m_ptr
); m_ptr
= 0; return tmp
; }
54 T
& operator*() const { return *m_ptr
; }
55 ALWAYS_INLINE 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
* RefPtr::*UnspecifiedBoolType
;
61 operator UnspecifiedBoolType() const { return m_ptr
? &RefPtr::m_ptr
: 0; }
63 RefPtr
& operator=(const RefPtr
&);
64 RefPtr
& operator=(T
*);
65 RefPtr
& operator=(const PassRefPtr
<T
>&);
66 template <typename U
> RefPtr
& operator=(const RefPtr
<U
>&);
67 template <typename U
> RefPtr
& operator=(const PassRefPtr
<U
>&);
75 template <typename T
> template <typename U
> inline RefPtr
<T
>::RefPtr(const PassRefPtr
<U
>& o
)
76 : m_ptr(o
.releaseRef())
80 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<T
>& o
)
92 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const RefPtr
<U
>& o
)
104 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(T
* optr
)
115 template <typename T
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<T
>& o
)
118 m_ptr
= o
.releaseRef();
124 template <typename T
> template <typename U
> inline RefPtr
<T
>& RefPtr
<T
>::operator=(const PassRefPtr
<U
>& o
)
127 m_ptr
= o
.releaseRef();
133 template <class T
> inline void RefPtr
<T
>::swap(RefPtr
<T
>& o
)
135 std::swap(m_ptr
, o
.m_ptr
);
138 template <class T
> inline void swap(RefPtr
<T
>& a
, RefPtr
<T
>& b
)
143 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
145 return a
.get() == b
.get();
148 template <typename T
, typename U
> inline bool operator==(const RefPtr
<T
>& a
, U
* b
)
153 template <typename T
, typename U
> inline bool operator==(T
* a
, const RefPtr
<U
>& b
)
158 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, const RefPtr
<U
>& b
)
160 return a
.get() != b
.get();
163 template <typename T
, typename U
> inline bool operator!=(const RefPtr
<T
>& a
, U
* b
)
168 template <typename T
, typename U
> inline bool operator!=(T
* a
, const RefPtr
<U
>& b
)
173 template <typename T
, typename U
> inline RefPtr
<T
> static_pointer_cast(const RefPtr
<U
>& p
)
175 return RefPtr
<T
>(static_cast<T
*>(p
.get()));
178 template <typename T
, typename U
> inline RefPtr
<T
> const_pointer_cast(const RefPtr
<U
>& p
)
180 return RefPtr
<T
>(const_cast<T
*>(p
.get()));
183 template <typename T
> inline T
* getPtr(const RefPtr
<T
>& p
)
191 using WTF::static_pointer_cast
;
192 using WTF::const_pointer_cast
;
194 #endif // WTF_RefPtr_h