]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/OwnPtr.h
2 * Copyright (C) 2006, 2007, 2008, 2009 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.
24 #include "Assertions.h"
25 #include "Noncopyable.h"
26 #include "OwnPtrCommon.h"
27 #include "TypeTraits.h"
33 // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
35 template <typename T
> class PassOwnPtr
;
37 template <typename T
> class OwnPtr
: public Noncopyable
{
39 typedef typename RemovePointer
<T
>::Type ValueType
;
40 typedef ValueType
* PtrType
;
42 explicit OwnPtr(PtrType ptr
= 0) : m_ptr(ptr
) { }
43 OwnPtr(std::auto_ptr
<ValueType
> autoPtr
) : m_ptr(autoPtr
.release()) { }
44 // See comment in PassOwnPtr.h for why this takes a const reference.
45 template <typename U
> OwnPtr(const PassOwnPtr
<U
>& o
);
47 // This copy constructor is used implicitly by gcc when it generates
48 // transients for assigning a PassOwnPtr<T> object to a stack-allocated
49 // OwnPtr<T> object. It should never be called explicitly and gcc
50 // should optimize away the constructor when generating code.
51 OwnPtr(const OwnPtr
<ValueType
>& o
);
53 ~OwnPtr() { deleteOwnedPtr(m_ptr
); }
55 PtrType
get() const { return m_ptr
; }
56 PtrType
release() { PtrType ptr
= m_ptr
; m_ptr
= 0; return ptr
; }
58 // FIXME: This should be renamed to adopt.
59 void set(PtrType ptr
) { ASSERT(!ptr
|| m_ptr
!= ptr
); deleteOwnedPtr(m_ptr
); m_ptr
= ptr
; }
61 void adopt(std::auto_ptr
<ValueType
> autoPtr
) { ASSERT(!autoPtr
.get() || m_ptr
!= autoPtr
.get()); deleteOwnedPtr(m_ptr
); m_ptr
= autoPtr
.release(); }
63 void clear() { deleteOwnedPtr(m_ptr
); m_ptr
= 0; }
65 ValueType
& operator*() const { ASSERT(m_ptr
); return *m_ptr
; }
66 PtrType
operator->() const { ASSERT(m_ptr
); return m_ptr
; }
68 bool operator!() const { return !m_ptr
; }
70 // This conversion operator allows implicit conversion to bool but not to other integer types.
71 typedef PtrType
OwnPtr::*UnspecifiedBoolType
;
72 operator UnspecifiedBoolType() const { return m_ptr
? &OwnPtr::m_ptr
: 0; }
74 OwnPtr
& operator=(const PassOwnPtr
<T
>&);
75 template <typename U
> OwnPtr
& operator=(const PassOwnPtr
<U
>&);
77 void swap(OwnPtr
& o
) { std::swap(m_ptr
, o
.m_ptr
); }
83 template <typename T
> template <typename U
> inline OwnPtr
<T
>::OwnPtr(const PassOwnPtr
<U
>& o
)
88 template <typename T
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(const PassOwnPtr
<T
>& o
)
92 ASSERT(!ptr
|| m_ptr
!= ptr
);
98 template <typename T
> template <typename U
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(const PassOwnPtr
<U
>& o
)
102 ASSERT(!ptr
|| m_ptr
!= ptr
);
108 template <typename T
> inline void swap(OwnPtr
<T
>& a
, OwnPtr
<T
>& b
)
113 template <typename T
, typename U
> inline bool operator==(const OwnPtr
<T
>& a
, U
* b
)
118 template <typename T
, typename U
> inline bool operator==(T
* a
, const OwnPtr
<U
>& b
)
123 template <typename T
, typename U
> inline bool operator!=(const OwnPtr
<T
>& a
, U
* b
)
128 template <typename T
, typename U
> inline bool operator!=(T
* a
, const OwnPtr
<U
>& b
)
133 template <typename T
> inline typename OwnPtr
<T
>::PtrType
getPtr(const OwnPtr
<T
>& p
)
142 #endif // WTF_OwnPtr_h