2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Intel Corporation. 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.
25 #include "wtf/HashTableDeletedValueType.h"
26 #include "wtf/Noncopyable.h"
27 #include "wtf/NullPtr.h"
28 #include "wtf/OwnPtrCommon.h"
34 template<typename T
> class PassOwnPtr
;
36 template<typename T
> class OwnPtr
{
37 WTF_MAKE_NONCOPYABLE(OwnPtr
);
39 typedef typename RemoveExtent
<T
>::Type ValueType
;
40 typedef ValueType
* PtrType
;
42 OwnPtr() : m_ptr(0) { }
43 OwnPtr(std::nullptr_t
) : m_ptr(0) { }
45 // See comment in PassOwnPtr.h for why this takes a const reference.
46 OwnPtr(const PassOwnPtr
<T
>&);
47 template<typename U
> OwnPtr(const PassOwnPtr
<U
>&, EnsurePtrConvertibleArgDecl(U
, T
));
49 // Hash table deleted values, which are only constructed and never copied or destroyed.
50 OwnPtr(HashTableDeletedValueType
) : m_ptr(hashTableDeletedValue()) { }
51 bool isHashTableDeletedValue() const { return m_ptr
== hashTableDeletedValue(); }
55 OwnedPtrDeleter
<T
>::deletePtr(m_ptr
);
59 PtrType
get() const { return m_ptr
; }
62 PassOwnPtr
<T
> release();
63 PtrType
leakPtr() WARN_UNUSED_RETURN
;
65 ValueType
& operator*() const { ASSERT(m_ptr
); return *m_ptr
; }
66 PtrType
operator->() const { ASSERT(m_ptr
); return m_ptr
; }
68 ValueType
& operator[](std::ptrdiff_t i
) const;
70 bool operator!() const { return !m_ptr
; }
72 // This conversion operator allows implicit conversion to bool but not to other integer types.
73 typedef PtrType
OwnPtr::*UnspecifiedBoolType
;
74 operator UnspecifiedBoolType() const { return m_ptr
? &OwnPtr::m_ptr
: 0; }
76 OwnPtr
& operator=(const PassOwnPtr
<T
>&);
77 OwnPtr
& operator=(std::nullptr_t
) { clear(); return *this; }
78 template<typename U
> OwnPtr
& operator=(const PassOwnPtr
<U
>&);
81 template<typename U
> OwnPtr(OwnPtr
<U
>&&);
83 OwnPtr
& operator=(OwnPtr
&&);
84 template<typename U
> OwnPtr
& operator=(OwnPtr
<U
>&&);
86 void swap(OwnPtr
& o
) { std::swap(m_ptr
, o
.m_ptr
); }
88 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
91 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
92 // double-destruction), so these equality operators should never be needed.
93 template<typename U
> bool operator==(const OwnPtr
<U
>&) const { static_assert(!sizeof(U
*), "OwnPtrs should never be equal"); return false; }
94 template<typename U
> bool operator!=(const OwnPtr
<U
>&) const { static_assert(!sizeof(U
*), "OwnPtrs should never be equal"); return false; }
95 template<typename U
> bool operator==(const PassOwnPtr
<U
>&) const { static_assert(!sizeof(U
*), "OwnPtrs should never be equal"); return false; }
96 template<typename U
> bool operator!=(const PassOwnPtr
<U
>&) const { static_assert(!sizeof(U
*), "OwnPtrs should never be equal"); return false; }
101 template<typename T
> inline OwnPtr
<T
>::OwnPtr(const PassOwnPtr
<T
>& o
)
106 template<typename T
> template<typename U
> inline OwnPtr
<T
>::OwnPtr(const PassOwnPtr
<U
>& o
, EnsurePtrConvertibleArgDefn(U
, T
))
109 static_assert(!IsArray
<T
>::value
, "pointers to array must never be converted");
112 template<typename T
> inline void OwnPtr
<T
>::clear()
116 OwnedPtrDeleter
<T
>::deletePtr(ptr
);
119 template<typename T
> inline PassOwnPtr
<T
> OwnPtr
<T
>::release()
123 return PassOwnPtr
<T
>(ptr
);
126 template<typename T
> inline typename OwnPtr
<T
>::PtrType OwnPtr
<T
>::leakPtr()
133 template<typename T
> inline typename OwnPtr
<T
>::ValueType
& OwnPtr
<T
>::operator[](std::ptrdiff_t i
) const
135 static_assert(IsArray
<T
>::value
, "elements access is possible for arrays only");
141 template<typename T
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(const PassOwnPtr
<T
>& o
)
145 ASSERT(!ptr
|| m_ptr
!= ptr
);
146 OwnedPtrDeleter
<T
>::deletePtr(ptr
);
150 template<typename T
> template<typename U
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(const PassOwnPtr
<U
>& o
)
152 static_assert(!IsArray
<T
>::value
, "pointers to array must never be converted");
155 ASSERT(!ptr
|| m_ptr
!= ptr
);
156 OwnedPtrDeleter
<T
>::deletePtr(ptr
);
160 template<typename T
> inline OwnPtr
<T
>::OwnPtr(OwnPtr
<T
>&& o
)
165 template<typename T
> template<typename U
> inline OwnPtr
<T
>::OwnPtr(OwnPtr
<U
>&& o
)
168 static_assert(!IsArray
<T
>::value
, "pointers to array must never be converted");
171 template<typename T
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(OwnPtr
<T
>&& o
)
175 ASSERT(!ptr
|| m_ptr
!= ptr
);
176 OwnedPtrDeleter
<T
>::deletePtr(ptr
);
181 template<typename T
> template<typename U
> inline OwnPtr
<T
>& OwnPtr
<T
>::operator=(OwnPtr
<U
>&& o
)
183 static_assert(!IsArray
<T
>::value
, "pointers to array must never be converted");
186 ASSERT(!ptr
|| m_ptr
!= ptr
);
187 OwnedPtrDeleter
<T
>::deletePtr(ptr
);
192 template<typename T
> inline void swap(OwnPtr
<T
>& a
, OwnPtr
<T
>& b
)
197 template<typename T
, typename U
> inline bool operator==(const OwnPtr
<T
>& a
, U
* b
)
202 template<typename T
, typename U
> inline bool operator==(T
* a
, const OwnPtr
<U
>& b
)
207 template<typename T
, typename U
> inline bool operator!=(const OwnPtr
<T
>& a
, U
* b
)
212 template<typename T
, typename U
> inline bool operator!=(T
* a
, const OwnPtr
<U
>& b
)
217 template<typename T
> inline typename OwnPtr
<T
>::PtrType
getPtr(const OwnPtr
<T
>& p
)
226 #endif // WTF_OwnPtr_h