| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 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 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef WTF_PassOwnPtr_h | 27 #ifndef WTF_PassOwnPtr_h |
| 28 #define WTF_PassOwnPtr_h | 28 #define WTF_PassOwnPtr_h |
| 29 | 29 |
| 30 #include "wtf/OwnPtrCommon.h" | 30 #include "wtf/OwnPtrCommon.h" |
| 31 | 31 |
| 32 namespace WTF { | 32 namespace WTF { |
| 33 | 33 |
| 34 template <typename T> class OwnPtr; | 34 template <typename T> |
| 35 template <typename T> class PassOwnPtr; | 35 class OwnPtr; |
| 36 template <typename T> PassOwnPtr<T> adoptPtr(T*); | 36 template <typename T> |
| 37 template <typename T> PassOwnPtr<T[]> adoptArrayPtr(T*); | 37 class PassOwnPtr; |
| 38 template <typename T> |
| 39 PassOwnPtr<T> adoptPtr(T*); |
| 40 template <typename T> |
| 41 PassOwnPtr<T[]> adoptArrayPtr(T*); |
| 38 | 42 |
| 39 template <typename T> class PassOwnPtr { | 43 template <typename T> |
| 40 public: | 44 class PassOwnPtr { |
| 41 typedef typename RemoveExtent<T>::Type ValueType; | 45 public: |
| 42 typedef ValueType* PtrType; | 46 typedef typename RemoveExtent<T>::Type ValueType; |
| 47 typedef ValueType* PtrType; |
| 43 | 48 |
| 44 PassOwnPtr() : m_ptr(nullptr) {} | 49 PassOwnPtr() |
| 45 PassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | 50 : m_ptr(nullptr) {} |
| 51 PassOwnPtr(std::nullptr_t) |
| 52 : m_ptr(nullptr) {} |
| 46 | 53 |
| 47 // It somewhat breaks the type system to allow transfer of ownership out of | 54 // It somewhat breaks the type system to allow transfer of ownership out of |
| 48 // a const PassOwnPtr. However, it makes it much easier to work with | 55 // a const PassOwnPtr. However, it makes it much easier to work with |
| 49 // PassOwnPtr temporaries, and we don't have a need to use real const | 56 // PassOwnPtr temporaries, and we don't have a need to use real const |
| 50 // PassOwnPtrs anyway. | 57 // PassOwnPtrs anyway. |
| 51 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) {} | 58 PassOwnPtr(const PassOwnPtr& o) |
| 52 template <typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleA
rgDecl(U, T)); | 59 : m_ptr(o.leakPtr()) {} |
| 60 template <typename U> |
| 61 PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDecl(U, T)); |
| 53 | 62 |
| 54 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } | 63 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } |
| 55 | 64 |
| 56 PtrType get() const { return m_ptr; } | 65 PtrType get() const { return m_ptr; } |
| 57 | 66 |
| 58 PtrType leakPtr() const WARN_UNUSED_RETURN; | 67 PtrType leakPtr() const WARN_UNUSED_RETURN; |
| 59 | 68 |
| 60 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 69 ValueType& operator*() const { |
| 61 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 70 ASSERT(m_ptr); |
| 71 return *m_ptr; |
| 72 } |
| 73 PtrType operator->() const { |
| 74 ASSERT(m_ptr); |
| 75 return m_ptr; |
| 76 } |
| 62 | 77 |
| 63 bool operator!() const { return !m_ptr; } | 78 bool operator!() const { return !m_ptr; } |
| 64 | 79 |
| 65 // This conversion operator allows implicit conversion to bool but not to | 80 // This conversion operator allows implicit conversion to bool but not to |
| 66 // other integer types. | 81 // other integer types. |
| 67 typedef PtrType PassOwnPtr::*UnspecifiedBoolType; | 82 typedef PtrType PassOwnPtr::*UnspecifiedBoolType; |
| 68 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0
; } | 83 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0;
} |
| 69 | 84 |
| 70 template <typename U> friend PassOwnPtr<U> adoptPtr(U*); | 85 template <typename U> |
| 71 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*); | 86 friend PassOwnPtr<U> adoptPtr(U*); |
| 72 template <typename U> friend class OwnPtr; | 87 template <typename U> |
| 88 friend PassOwnPtr<U[]> adoptArrayPtr(U*); |
| 89 template <typename U> |
| 90 friend class OwnPtr; |
| 73 | 91 |
| 74 private: | 92 private: |
| 75 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} | 93 explicit PassOwnPtr(PtrType ptr) |
| 94 : m_ptr(ptr) {} |
| 76 | 95 |
| 77 PassOwnPtr& operator=(const PassOwnPtr&) | 96 PassOwnPtr& operator=(const PassOwnPtr&) { |
| 78 { | 97 static_assert(!sizeof(T*), "PassOwnPtr should never be assigned to"); |
| 79 static_assert(!sizeof(T*), "PassOwnPtr should never be assigned to"); | 98 return *this; |
| 80 return *this; | 99 } |
| 81 } | |
| 82 | 100 |
| 83 // We should never have two OwnPtrs for the same underlying object | 101 // We should never have two OwnPtrs for the same underlying object |
| 84 // (otherwise we'll get double-destruction), so these equality operators | 102 // (otherwise we'll get double-destruction), so these equality operators |
| 85 // should never be needed. | 103 // should never be needed. |
| 86 template <typename U> bool operator==(const PassOwnPtr<U>&) const | 104 template <typename U> |
| 87 { | 105 bool operator==(const PassOwnPtr<U>&) const { |
| 88 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 106 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 89 return false; | 107 return false; |
| 90 } | 108 } |
| 91 template <typename U> bool operator!=(const PassOwnPtr<U>&) const | 109 template <typename U> |
| 92 { | 110 bool operator!=(const PassOwnPtr<U>&) const { |
| 93 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 111 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 94 return false; | 112 return false; |
| 95 } | 113 } |
| 96 template <typename U> bool operator==(const OwnPtr<U>&) const | 114 template <typename U> |
| 97 { | 115 bool operator==(const OwnPtr<U>&) const { |
| 98 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 116 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 99 return false; | 117 return false; |
| 100 } | 118 } |
| 101 template <typename U> bool operator!=(const OwnPtr<U>&) const | 119 template <typename U> |
| 102 { | 120 bool operator!=(const OwnPtr<U>&) const { |
| 103 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 121 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 104 return false; | 122 return false; |
| 105 } | 123 } |
| 106 | 124 |
| 107 mutable PtrType m_ptr; | 125 mutable PtrType m_ptr; |
| 108 }; | 126 }; |
| 109 | 127 |
| 110 template <typename T> | 128 template <typename T> |
| 111 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, E
nsurePtrConvertibleArgDefn(U, T)) | 129 template <typename U> |
| 112 : m_ptr(o.leakPtr()) | 130 inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, EnsurePtrConvertibleArg
Defn(U, T)) |
| 113 { | 131 : m_ptr(o.leakPtr()) { |
| 114 static_assert(!IsArray<T>::value, "pointers to array must never be converted
"); | 132 static_assert(!IsArray<T>::value, "pointers to array must never be converted")
; |
| 115 } | 133 } |
| 116 | 134 |
| 117 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak
Ptr() const | 135 template <typename T> |
| 118 { | 136 inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leakPtr() const { |
| 119 PtrType ptr = m_ptr; | 137 PtrType ptr = m_ptr; |
| 120 m_ptr = nullptr; | 138 m_ptr = nullptr; |
| 121 return ptr; | 139 return ptr; |
| 122 } | 140 } |
| 123 | 141 |
| 124 template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a,
U* b) | 142 template <typename T, typename U> |
| 125 { | 143 inline bool operator==(const PassOwnPtr<T>& a, U* b) { |
| 126 return a.get() == b; | 144 return a.get() == b; |
| 127 } | 145 } |
| 128 | 146 |
| 129 template <typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<
U>& b) | 147 template <typename T, typename U> |
| 130 { | 148 inline bool operator==(T* a, const PassOwnPtr<U>& b) { |
| 131 return a == b.get(); | 149 return a == b.get(); |
| 132 } | 150 } |
| 133 | 151 |
| 134 template <typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a,
U* b) | 152 template <typename T, typename U> |
| 135 { | 153 inline bool operator!=(const PassOwnPtr<T>& a, U* b) { |
| 136 return a.get() != b; | 154 return a.get() != b; |
| 137 } | 155 } |
| 138 | 156 |
| 139 template <typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<
U>& b) | 157 template <typename T, typename U> |
| 140 { | 158 inline bool operator!=(T* a, const PassOwnPtr<U>& b) { |
| 141 return a != b.get(); | 159 return a != b.get(); |
| 142 } | 160 } |
| 143 | 161 |
| 144 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) | 162 template <typename T> |
| 145 { | 163 inline PassOwnPtr<T> adoptPtr(T* ptr) { |
| 146 return PassOwnPtr<T>(ptr); | 164 return PassOwnPtr<T>(ptr); |
| 147 } | 165 } |
| 148 | 166 |
| 149 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) | 167 template <typename T> |
| 150 { | 168 inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) { |
| 151 return PassOwnPtr<T[]>(ptr); | 169 return PassOwnPtr<T[]>(ptr); |
| 152 } | 170 } |
| 153 | 171 |
| 154 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const
PassOwnPtr<U>& p) | 172 template <typename T, typename U> |
| 155 { | 173 inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p) { |
| 156 static_assert(!IsArray<T>::value, "pointers to array must never be converted
"); | 174 static_assert(!IsArray<T>::value, "pointers to array must never be converted")
; |
| 157 return adoptPtr(static_cast<T*>(p.leakPtr())); | 175 return adoptPtr(static_cast<T*>(p.leakPtr())); |
| 158 } | 176 } |
| 159 | 177 |
| 160 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) | 178 template <typename T> |
| 161 { | 179 inline T* getPtr(const PassOwnPtr<T>& p) { |
| 162 return p.get(); | 180 return p.get(); |
| 163 } | 181 } |
| 164 | 182 |
| 165 } // namespace WTF | 183 } // namespace WTF |
| 166 | 184 |
| 167 using WTF::PassOwnPtr; | 185 using WTF::PassOwnPtr; |
| 168 using WTF::adoptPtr; | 186 using WTF::adoptPtr; |
| 169 using WTF::adoptArrayPtr; | 187 using WTF::adoptArrayPtr; |
| 170 using WTF::static_pointer_cast; | 188 using WTF::static_pointer_cast; |
| 171 | 189 |
| 172 #endif // WTF_PassOwnPtr_h | 190 #endif // WTF_PassOwnPtr_h |
| OLD | NEW |