Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / OwnPtr.h
blob1f65b31fccbae267aeb8218ad1d2e0554b756a42
1 /*
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.
22 #ifndef WTF_OwnPtr_h
23 #define WTF_OwnPtr_h
25 #include "wtf/HashTableDeletedValueType.h"
26 #include "wtf/Noncopyable.h"
27 #include "wtf/NullPtr.h"
28 #include "wtf/OwnPtrCommon.h"
29 #include <algorithm>
30 #include <utility>
32 namespace WTF {
34 template<typename T> class PassOwnPtr;
36 template<typename T> class OwnPtr {
37 WTF_MAKE_NONCOPYABLE(OwnPtr);
38 public:
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(); }
53 ~OwnPtr()
55 OwnedPtrDeleter<T>::deletePtr(m_ptr);
56 m_ptr = 0;
59 PtrType get() const { return m_ptr; }
61 void clear();
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>&);
80 OwnPtr(OwnPtr&&);
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); }
90 private:
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; }
98 PtrType m_ptr;
101 template<typename T> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<T>& o)
102 : m_ptr(o.leakPtr())
106 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o, EnsurePtrConvertibleArgDefn(U, T))
107 : m_ptr(o.leakPtr())
109 static_assert(!IsArray<T>::value, "pointers to array must never be converted");
112 template<typename T> inline void OwnPtr<T>::clear()
114 PtrType ptr = m_ptr;
115 m_ptr = 0;
116 OwnedPtrDeleter<T>::deletePtr(ptr);
119 template<typename T> inline PassOwnPtr<T> OwnPtr<T>::release()
121 PtrType ptr = m_ptr;
122 m_ptr = 0;
123 return PassOwnPtr<T>(ptr);
126 template<typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr()
128 PtrType ptr = m_ptr;
129 m_ptr = 0;
130 return ptr;
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");
136 ASSERT(m_ptr);
137 ASSERT(i >= 0);
138 return m_ptr[i];
141 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o)
143 PtrType ptr = m_ptr;
144 m_ptr = o.leakPtr();
145 ASSERT(!ptr || m_ptr != ptr);
146 OwnedPtrDeleter<T>::deletePtr(ptr);
147 return *this;
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");
153 PtrType ptr = m_ptr;
154 m_ptr = o.leakPtr();
155 ASSERT(!ptr || m_ptr != ptr);
156 OwnedPtrDeleter<T>::deletePtr(ptr);
157 return *this;
160 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o)
161 : m_ptr(o.leakPtr())
165 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o)
166 : m_ptr(o.leakPtr())
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)
173 PtrType ptr = m_ptr;
174 m_ptr = o.leakPtr();
175 ASSERT(!ptr || m_ptr != ptr);
176 OwnedPtrDeleter<T>::deletePtr(ptr);
178 return *this;
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");
184 PtrType ptr = m_ptr;
185 m_ptr = o.leakPtr();
186 ASSERT(!ptr || m_ptr != ptr);
187 OwnedPtrDeleter<T>::deletePtr(ptr);
189 return *this;
192 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b)
194 a.swap(b);
197 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
199 return a.get() == b;
202 template<typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
204 return a == b.get();
207 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
209 return a.get() != b;
212 template<typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
214 return a != b.get();
217 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
219 return p.get();
222 } // namespace WTF
224 using WTF::OwnPtr;
226 #endif // WTF_OwnPtr_h