OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
3 * Copyright (C) 2013 Intel Corporation. All rights reserved. | 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
(...skipping 25 matching lines...) Expand all Loading... |
36 template <typename T> class OwnPtr { | 36 template <typename T> class OwnPtr { |
37 USING_FAST_MALLOC(OwnPtr); | 37 USING_FAST_MALLOC(OwnPtr); |
38 WTF_MAKE_NONCOPYABLE(OwnPtr); | 38 WTF_MAKE_NONCOPYABLE(OwnPtr); |
39 public: | 39 public: |
40 typedef typename std::remove_extent<T>::type ValueType; | 40 typedef typename std::remove_extent<T>::type ValueType; |
41 typedef ValueType* PtrType; | 41 typedef ValueType* PtrType; |
42 | 42 |
43 OwnPtr() : m_ptr(nullptr) {} | 43 OwnPtr() : m_ptr(nullptr) {} |
44 OwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | 44 OwnPtr(std::nullptr_t) : m_ptr(nullptr) {} |
45 | 45 |
46 // See comment in PassOwnPtr.h for why this takes a const reference. | 46 OwnPtr(PassOwnPtr<T>&&); |
47 OwnPtr(const PassOwnPtr<T>&); | 47 template <typename U> OwnPtr(PassOwnPtr<U>&&, EnsurePtrConvertibleArgDecl(U,
T)); |
48 template <typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDe
cl(U, T)); | |
49 | 48 |
50 // Hash table deleted values, which are only constructed and never copied or | 49 // Hash table deleted values, which are only constructed and never copied or |
51 // destroyed. | 50 // destroyed. |
52 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {} | 51 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {} |
53 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } | 52 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } |
54 | 53 |
55 ~OwnPtr() | 54 ~OwnPtr() |
56 { | 55 { |
57 OwnedPtrDeleter<T>::deletePtr(m_ptr); | 56 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
58 m_ptr = nullptr; | 57 m_ptr = nullptr; |
59 } | 58 } |
60 | 59 |
61 PtrType get() const { return m_ptr; } | 60 PtrType get() const { return m_ptr; } |
62 | 61 |
63 void clear(); | 62 void clear(); |
64 PassOwnPtr<T> release(); | 63 PassOwnPtr<T> release(); |
65 PtrType leakPtr() WARN_UNUSED_RETURN; | 64 PtrType leakPtr() WARN_UNUSED_RETURN; |
66 | 65 |
67 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 66 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
68 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 67 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
69 | 68 |
70 ValueType& operator[](std::ptrdiff_t i) const; | 69 ValueType& operator[](std::ptrdiff_t i) const; |
71 | 70 |
72 bool operator!() const { return !m_ptr; } | 71 bool operator!() const { return !m_ptr; } |
73 explicit operator bool() const { return m_ptr; } | 72 explicit operator bool() const { return m_ptr; } |
74 | 73 |
75 OwnPtr& operator=(const PassOwnPtr<T>&); | 74 OwnPtr& operator=(PassOwnPtr<T>&&); |
76 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } | 75 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } |
77 template <typename U> OwnPtr& operator=(const PassOwnPtr<U>&); | 76 template <typename U> OwnPtr& operator=(PassOwnPtr<U>&&); |
78 | 77 |
79 OwnPtr(OwnPtr&&); | 78 OwnPtr(OwnPtr&&); |
80 template <typename U> OwnPtr(OwnPtr<U>&&); | 79 template <typename U> OwnPtr(OwnPtr<U>&&); |
81 | 80 |
82 OwnPtr& operator=(OwnPtr&&); | 81 OwnPtr& operator=(OwnPtr&&); |
83 template <typename U> OwnPtr& operator=(OwnPtr<U>&&); | 82 template <typename U> OwnPtr& operator=(OwnPtr<U>&&); |
84 | 83 |
85 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | 84 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
86 | 85 |
87 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 86 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
(...skipping 19 matching lines...) Expand all Loading... |
107 } | 106 } |
108 template <typename U> bool operator!=(const PassOwnPtr<U>&) const | 107 template <typename U> bool operator!=(const PassOwnPtr<U>&) const |
109 { | 108 { |
110 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 109 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
111 return false; | 110 return false; |
112 } | 111 } |
113 | 112 |
114 PtrType m_ptr; | 113 PtrType m_ptr; |
115 }; | 114 }; |
116 | 115 |
117 template <typename T> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<T>& o) | 116 template <typename T> inline OwnPtr<T>::OwnPtr(PassOwnPtr<T>&& o) |
118 : m_ptr(o.leakPtr()) | 117 : m_ptr(o.leakPtr()) |
119 { | 118 { |
120 } | 119 } |
121 | 120 |
122 template <typename T> | 121 template <typename T> |
123 template <typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o, EnsurePtr
ConvertibleArgDefn(U, T)) | 122 template <typename U> inline OwnPtr<T>::OwnPtr(PassOwnPtr<U>&& o, EnsurePtrConve
rtibleArgDefn(U, T)) |
124 : m_ptr(o.leakPtr()) | 123 : m_ptr(o.leakPtr()) |
125 { | 124 { |
126 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 125 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
127 } | 126 } |
128 | 127 |
129 template <typename T> inline void OwnPtr<T>::clear() | 128 template <typename T> inline void OwnPtr<T>::clear() |
130 { | 129 { |
131 PtrType ptr = m_ptr; | 130 PtrType ptr = m_ptr; |
132 m_ptr = nullptr; | 131 m_ptr = nullptr; |
133 OwnedPtrDeleter<T>::deletePtr(ptr); | 132 OwnedPtrDeleter<T>::deletePtr(ptr); |
(...skipping 14 matching lines...) Expand all Loading... |
148 } | 147 } |
149 | 148 |
150 template <typename T> inline typename OwnPtr<T>::ValueType& OwnPtr<T>::operator[
](std::ptrdiff_t i) const | 149 template <typename T> inline typename OwnPtr<T>::ValueType& OwnPtr<T>::operator[
](std::ptrdiff_t i) const |
151 { | 150 { |
152 static_assert(std::is_array<T>::value, "elements access is possible for arra
ys only"); | 151 static_assert(std::is_array<T>::value, "elements access is possible for arra
ys only"); |
153 ASSERT(m_ptr); | 152 ASSERT(m_ptr); |
154 ASSERT(i >= 0); | 153 ASSERT(i >= 0); |
155 return m_ptr[i]; | 154 return m_ptr[i]; |
156 } | 155 } |
157 | 156 |
158 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>
& o) | 157 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(PassOwnPtr<T>&& o) |
159 { | 158 { |
160 PtrType ptr = m_ptr; | 159 PtrType ptr = m_ptr; |
161 m_ptr = o.leakPtr(); | 160 m_ptr = o.leakPtr(); |
162 ASSERT(!ptr || m_ptr != ptr); | 161 ASSERT(!ptr || m_ptr != ptr); |
163 OwnedPtrDeleter<T>::deletePtr(ptr); | 162 OwnedPtrDeleter<T>::deletePtr(ptr); |
164 return *this; | 163 return *this; |
165 } | 164 } |
166 | 165 |
167 template <typename T> | 166 template <typename T> |
168 template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>
& o) | 167 template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(PassOwnPtr<U>&& o) |
169 { | 168 { |
170 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 169 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
171 PtrType ptr = m_ptr; | 170 PtrType ptr = m_ptr; |
172 m_ptr = o.leakPtr(); | 171 m_ptr = o.leakPtr(); |
173 ASSERT(!ptr || m_ptr != ptr); | 172 ASSERT(!ptr || m_ptr != ptr); |
174 OwnedPtrDeleter<T>::deletePtr(ptr); | 173 OwnedPtrDeleter<T>::deletePtr(ptr); |
175 return *this; | 174 return *this; |
176 } | 175 } |
177 | 176 |
178 template <typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) | 177 template <typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>&
p) | 236 template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>&
p) |
238 { | 237 { |
239 return p.get(); | 238 return p.get(); |
240 } | 239 } |
241 | 240 |
242 } // namespace WTF | 241 } // namespace WTF |
243 | 242 |
244 using WTF::OwnPtr; | 243 using WTF::OwnPtr; |
245 | 244 |
246 #endif // WTF_OwnPtr_h | 245 #endif // WTF_OwnPtr_h |
OLD | NEW |