| 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 15 matching lines...) Expand all Loading... |
| 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/Allocator.h" | 30 #include "wtf/Allocator.h" |
| 31 #include "wtf/Noncopyable.h" | 31 #include "wtf/Noncopyable.h" |
| 32 #include "wtf/OwnPtrCommon.h" | 32 #include "wtf/OwnPtrCommon.h" |
| 33 | 33 |
| 34 namespace WTF { | 34 namespace WTF { |
| 35 | 35 |
| 36 template <typename T> class OwnPtr; | 36 template <typename T> |
| 37 template <typename T> class PassOwnPtr; | 37 class OwnPtr; |
| 38 template <typename T> PassOwnPtr<T> adoptPtr(T*); | 38 template <typename T> |
| 39 template <typename T> PassOwnPtr<T[]> adoptArrayPtr(T*); | 39 class PassOwnPtr; |
| 40 template <typename T> |
| 41 PassOwnPtr<T> adoptPtr(T*); |
| 42 template <typename T> |
| 43 PassOwnPtr<T[]> adoptArrayPtr(T*); |
| 40 | 44 |
| 41 template <typename T> class PassOwnPtr { | 45 template <typename T> |
| 42 DISALLOW_NEW(); | 46 class PassOwnPtr { |
| 43 public: | 47 DISALLOW_NEW(); |
| 44 typedef typename std::remove_extent<T>::type ValueType; | |
| 45 typedef ValueType* PtrType; | |
| 46 | 48 |
| 47 PassOwnPtr() : m_ptr(nullptr) {} | 49 public: |
| 48 PassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | 50 typedef typename std::remove_extent<T>::type ValueType; |
| 51 typedef ValueType* PtrType; |
| 49 | 52 |
| 50 // It somewhat breaks the type system to allow transfer of ownership out of | 53 PassOwnPtr() : m_ptr(nullptr) {} |
| 51 // a const PassOwnPtr. However, it makes it much easier to work with | 54 PassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} |
| 52 // PassOwnPtr temporaries, and we don't have a need to use real const | |
| 53 // PassOwnPtrs anyway. | |
| 54 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) {} | |
| 55 template <typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleA
rgDecl(U, T)); | |
| 56 | 55 |
| 57 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } | 56 // It somewhat breaks the type system to allow transfer of ownership out of |
| 57 // a const PassOwnPtr. However, it makes it much easier to work with |
| 58 // PassOwnPtr temporaries, and we don't have a need to use real const |
| 59 // PassOwnPtrs anyway. |
| 60 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) {} |
| 61 template <typename U> |
| 62 PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDecl(U, T)); |
| 58 | 63 |
| 59 PtrType get() const { return m_ptr; } | 64 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } |
| 60 | 65 |
| 61 PtrType leakPtr() const WARN_UNUSED_RETURN; | 66 PtrType get() const { return m_ptr; } |
| 62 | 67 |
| 63 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 68 PtrType leakPtr() const WARN_UNUSED_RETURN; |
| 64 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | |
| 65 | 69 |
| 66 bool operator!() const { return !m_ptr; } | 70 ValueType& operator*() const { |
| 71 ASSERT(m_ptr); |
| 72 return *m_ptr; |
| 73 } |
| 74 PtrType operator->() const { |
| 75 ASSERT(m_ptr); |
| 76 return m_ptr; |
| 77 } |
| 67 | 78 |
| 68 // This conversion operator allows implicit conversion to bool but not to | 79 bool operator!() const { return !m_ptr; } |
| 69 // other integer types. | |
| 70 typedef PtrType PassOwnPtr::*UnspecifiedBoolType; | |
| 71 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0
; } | |
| 72 | 80 |
| 73 template <typename U> friend PassOwnPtr<U> adoptPtr(U*); | 81 // This conversion operator allows implicit conversion to bool but not to |
| 74 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*); | 82 // other integer types. |
| 75 template <typename U> friend class OwnPtr; | 83 typedef PtrType PassOwnPtr::*UnspecifiedBoolType; |
| 84 operator UnspecifiedBoolType() const { |
| 85 return m_ptr ? &PassOwnPtr::m_ptr : 0; |
| 86 } |
| 76 | 87 |
| 77 private: | 88 template <typename U> |
| 78 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} | 89 friend PassOwnPtr<U> adoptPtr(U*); |
| 90 template <typename U> |
| 91 friend PassOwnPtr<U[]> adoptArrayPtr(U*); |
| 92 template <typename U> |
| 93 friend class OwnPtr; |
| 79 | 94 |
| 80 PassOwnPtr& operator=(const PassOwnPtr&) = delete; | 95 private: |
| 96 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} |
| 81 | 97 |
| 82 // We should never have two OwnPtrs for the same underlying object | 98 PassOwnPtr& operator=(const PassOwnPtr&) = delete; |
| 83 // (otherwise we'll get double-destruction), so these equality operators | |
| 84 // should never be needed. | |
| 85 template <typename U> bool operator==(const PassOwnPtr<U>&) const = delete; | |
| 86 template <typename U> bool operator!=(const PassOwnPtr<U>&) const = delete; | |
| 87 template <typename U> bool operator==(const OwnPtr<U>&) const = delete; | |
| 88 template <typename U> bool operator!=(const OwnPtr<U>&) const = delete; | |
| 89 | 99 |
| 90 mutable PtrType m_ptr; | 100 // We should never have two OwnPtrs for the same underlying object |
| 101 // (otherwise we'll get double-destruction), so these equality operators |
| 102 // should never be needed. |
| 103 template <typename U> |
| 104 bool operator==(const PassOwnPtr<U>&) const = delete; |
| 105 template <typename U> |
| 106 bool operator!=(const PassOwnPtr<U>&) const = delete; |
| 107 template <typename U> |
| 108 bool operator==(const OwnPtr<U>&) const = delete; |
| 109 template <typename U> |
| 110 bool operator!=(const OwnPtr<U>&) const = delete; |
| 111 |
| 112 mutable PtrType m_ptr; |
| 91 }; | 113 }; |
| 92 | 114 |
| 93 template <typename T> | 115 template <typename T> |
| 94 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, E
nsurePtrConvertibleArgDefn(U, T)) | 116 template <typename U> |
| 95 : m_ptr(o.leakPtr()) | 117 inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, |
| 96 { | 118 EnsurePtrConvertibleArgDefn(U, T)) |
| 97 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 119 : m_ptr(o.leakPtr()) { |
| 120 static_assert(!std::is_array<T>::value, |
| 121 "pointers to array must never be converted"); |
| 98 } | 122 } |
| 99 | 123 |
| 100 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak
Ptr() const | 124 template <typename T> |
| 101 { | 125 inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leakPtr() const { |
| 102 PtrType ptr = m_ptr; | 126 PtrType ptr = m_ptr; |
| 103 m_ptr = nullptr; | 127 m_ptr = nullptr; |
| 104 return ptr; | 128 return ptr; |
| 105 } | 129 } |
| 106 | 130 |
| 107 template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a,
U* b) | 131 template <typename T, typename U> |
| 108 { | 132 inline bool operator==(const PassOwnPtr<T>& a, U* b) { |
| 109 return a.get() == b; | 133 return a.get() == b; |
| 110 } | 134 } |
| 111 | 135 |
| 112 template <typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<
U>& b) | 136 template <typename T, typename U> |
| 113 { | 137 inline bool operator==(T* a, const PassOwnPtr<U>& b) { |
| 114 return a == b.get(); | 138 return a == b.get(); |
| 115 } | 139 } |
| 116 | 140 |
| 117 template <typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a,
U* b) | 141 template <typename T, typename U> |
| 118 { | 142 inline bool operator!=(const PassOwnPtr<T>& a, U* b) { |
| 119 return a.get() != b; | 143 return a.get() != b; |
| 120 } | 144 } |
| 121 | 145 |
| 122 template <typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<
U>& b) | 146 template <typename T, typename U> |
| 123 { | 147 inline bool operator!=(T* a, const PassOwnPtr<U>& b) { |
| 124 return a != b.get(); | 148 return a != b.get(); |
| 125 } | 149 } |
| 126 | 150 |
| 127 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) | 151 template <typename T> |
| 128 { | 152 inline PassOwnPtr<T> adoptPtr(T* ptr) { |
| 129 return PassOwnPtr<T>(ptr); | 153 return PassOwnPtr<T>(ptr); |
| 130 } | 154 } |
| 131 | 155 |
| 132 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) | 156 template <typename T> |
| 133 { | 157 inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) { |
| 134 return PassOwnPtr<T[]>(ptr); | 158 return PassOwnPtr<T[]>(ptr); |
| 135 } | 159 } |
| 136 | 160 |
| 137 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const
PassOwnPtr<U>& p) | 161 template <typename T, typename U> |
| 138 { | 162 inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p) { |
| 139 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 163 static_assert(!std::is_array<T>::value, |
| 140 return adoptPtr(static_cast<T*>(p.leakPtr())); | 164 "pointers to array must never be converted"); |
| 165 return adoptPtr(static_cast<T*>(p.leakPtr())); |
| 141 } | 166 } |
| 142 | 167 |
| 143 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) | 168 template <typename T> |
| 144 { | 169 inline T* getPtr(const PassOwnPtr<T>& p) { |
| 145 return p.get(); | 170 return p.get(); |
| 146 } | 171 } |
| 147 | 172 |
| 148 } // namespace WTF | 173 } // namespace WTF |
| 149 | 174 |
| 150 using WTF::PassOwnPtr; | 175 using WTF::PassOwnPtr; |
| 151 using WTF::adoptPtr; | 176 using WTF::adoptPtr; |
| 152 using WTF::adoptArrayPtr; | 177 using WTF::adoptArrayPtr; |
| 153 using WTF::static_pointer_cast; | 178 using WTF::static_pointer_cast; |
| 154 | 179 |
| 155 #endif // WTF_PassOwnPtr_h | 180 #endif // WTF_PassOwnPtr_h |
| OLD | NEW |