OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. |
| 4 * |
| 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. |
| 9 * |
| 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. |
| 14 * |
| 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. |
| 19 * |
| 20 */ |
| 21 |
| 22 #ifndef WTF_OwnPtr_h |
| 23 #define WTF_OwnPtr_h |
| 24 |
| 25 #include "wtf/Allocator.h" |
| 26 #include "wtf/Compiler.h" |
| 27 #include "wtf/Forward.h" |
| 28 #include "wtf/HashTableDeletedValueType.h" |
| 29 #include "wtf/Noncopyable.h" |
| 30 #include "wtf/OwnPtrCommon.h" |
| 31 #include <algorithm> |
| 32 #include <utility> |
| 33 |
| 34 namespace WTF { |
| 35 |
| 36 template <typename T> class OwnPtr { |
| 37 USING_FAST_MALLOC(OwnPtr); |
| 38 WTF_MAKE_NONCOPYABLE(OwnPtr); |
| 39 public: |
| 40 template <typename U> friend PassOwnPtr<U> adoptPtr(U* ptr); |
| 41 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U* ptr); |
| 42 |
| 43 typedef typename std::remove_extent<T>::type ValueType; |
| 44 typedef ValueType* PtrType; |
| 45 |
| 46 OwnPtr() : m_ptr(nullptr) {} |
| 47 OwnPtr(std::nullptr_t) : m_ptr(nullptr) {} |
| 48 |
| 49 // Hash table deleted values, which are only constructed and never copied or |
| 50 // destroyed. |
| 51 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {} |
| 52 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } |
| 53 |
| 54 ~OwnPtr() |
| 55 { |
| 56 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 57 m_ptr = nullptr; |
| 58 } |
| 59 |
| 60 PtrType get() const { return m_ptr; } |
| 61 |
| 62 void reset(); |
| 63 |
| 64 PtrType leakPtr() WARN_UNUSED_RETURN; |
| 65 |
| 66 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
| 67 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
| 68 |
| 69 ValueType& operator[](std::ptrdiff_t i) const; |
| 70 |
| 71 bool operator!() const { return !m_ptr; } |
| 72 explicit operator bool() const { return m_ptr; } |
| 73 |
| 74 OwnPtr& operator=(std::nullptr_t) { reset(); return *this; } |
| 75 |
| 76 OwnPtr(OwnPtr&&); |
| 77 template <typename U, typename = typename std::enable_if<std::is_convertible
<U*, T*>::value>::type> OwnPtr(OwnPtr<U>&&); |
| 78 |
| 79 OwnPtr& operator=(OwnPtr&&); |
| 80 template <typename U> OwnPtr& operator=(OwnPtr<U>&&); |
| 81 |
| 82 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 83 |
| 84 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 85 |
| 86 private: |
| 87 explicit OwnPtr(PtrType ptr) : m_ptr(ptr) {} |
| 88 |
| 89 // We should never have two OwnPtrs for the same underlying object |
| 90 // (otherwise we'll get double-destruction), so these equality operators |
| 91 // should never be needed. |
| 92 template <typename U> bool operator==(const OwnPtr<U>&) const |
| 93 { |
| 94 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 95 return false; |
| 96 } |
| 97 template <typename U> bool operator!=(const OwnPtr<U>&) const |
| 98 { |
| 99 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 100 return false; |
| 101 } |
| 102 |
| 103 PtrType m_ptr; |
| 104 }; |
| 105 |
| 106 template <typename T> inline void OwnPtr<T>::reset() |
| 107 { |
| 108 PtrType ptr = m_ptr; |
| 109 m_ptr = nullptr; |
| 110 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 111 } |
| 112 |
| 113 template <typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr() |
| 114 { |
| 115 PtrType ptr = m_ptr; |
| 116 m_ptr = nullptr; |
| 117 return ptr; |
| 118 } |
| 119 |
| 120 template <typename T> inline typename OwnPtr<T>::ValueType& OwnPtr<T>::operator[
](std::ptrdiff_t i) const |
| 121 { |
| 122 static_assert(std::is_array<T>::value, "elements access is possible for arra
ys only"); |
| 123 ASSERT(m_ptr); |
| 124 ASSERT(i >= 0); |
| 125 return m_ptr[i]; |
| 126 } |
| 127 |
| 128 template <typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) |
| 129 : m_ptr(o.leakPtr()) |
| 130 { |
| 131 } |
| 132 |
| 133 template <typename T> |
| 134 template <typename U, typename> inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o) |
| 135 : m_ptr(o.leakPtr()) |
| 136 { |
| 137 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
| 138 } |
| 139 |
| 140 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<T>&& o) |
| 141 { |
| 142 PtrType ptr = m_ptr; |
| 143 m_ptr = o.leakPtr(); |
| 144 ASSERT(!ptr || m_ptr != ptr); |
| 145 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 146 |
| 147 return *this; |
| 148 } |
| 149 |
| 150 template <typename T> |
| 151 template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<U>&& o) |
| 152 { |
| 153 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
| 154 PtrType ptr = m_ptr; |
| 155 m_ptr = o.leakPtr(); |
| 156 ASSERT(!ptr || m_ptr != ptr); |
| 157 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 158 |
| 159 return *this; |
| 160 } |
| 161 |
| 162 template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) |
| 163 { |
| 164 a.swap(b); |
| 165 } |
| 166 |
| 167 template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U*
b) |
| 168 { |
| 169 return a.get() == b; |
| 170 } |
| 171 |
| 172 template <typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>&
b) |
| 173 { |
| 174 return a == b.get(); |
| 175 } |
| 176 |
| 177 template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U*
b) |
| 178 { |
| 179 return a.get() != b; |
| 180 } |
| 181 |
| 182 template <typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>&
b) |
| 183 { |
| 184 return a != b.get(); |
| 185 } |
| 186 |
| 187 template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>&
p) |
| 188 { |
| 189 return p.get(); |
| 190 } |
| 191 |
| 192 } // namespace WTF |
| 193 |
| 194 using WTF::OwnPtr; |
| 195 |
| 196 #endif // WTF_OwnPtr_h |
OLD | NEW |