1 /* 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21 #ifndef WTF_OwnPtr_h 22 #define WTF_OwnPtr_h 23 24 #include "wtf/Assertions.h" 25 #include "wtf/Noncopyable.h" 26 #include "wtf/NullPtr.h" 27 #include "wtf/OwnPtrCommon.h" 28 #include "wtf/TypeTraits.h" 29 #include <algorithm> 30 #include <memory> 31 32 namespace WTF { 33 34 // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type. 35 36 template<typename T> class PassOwnPtr; 37 template<typename T> PassOwnPtr<T> adoptPtr(T*); 38 39 template<typename T> class OwnPtr { 40 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 41 // If rvalue references are not supported, the copy constructor is 42 // public so OwnPtr cannot be marked noncopyable. See note below. 43 WTF_MAKE_NONCOPYABLE(OwnPtr); 44 #endif 45 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); 46 public: 47 typedef typename RemovePointer<T>::Type ValueType; 48 typedef ValueType* PtrType; 49 50 OwnPtr() : m_ptr(0) { } 51 OwnPtr(std::nullptr_t) : m_ptr(0) { } 52 53 // See comment in PassOwnPtr.h for why this takes a const reference. 54 template<typename U> OwnPtr(const PassOwnPtr<U>& o); 55 56 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 57 // This copy constructor is used implicitly by gcc when it generates 58 // transients for assigning a PassOwnPtr<T> object to a stack-allocated 59 // OwnPtr<T> object. It should never be called explicitly and gcc 60 // should optimize away the constructor when generating code. 61 OwnPtr(const OwnPtr<ValueType>&); 62 #endif 63 64 ~OwnPtr() { deleteOwnedPtr(m_ptr); } 65 66 PtrType get() const { return m_ptr; } 67 68 void clear(); 69 PassOwnPtr<T> release(); 70 PtrType leakPtr() WARN_UNUSED_RETURN; 71 72 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } 73 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } 74 75 bool operator!() const { return !m_ptr; } 76 77 // This conversion operator allows implicit conversion to bool but not to other integer types. 78 typedef PtrType OwnPtr::*UnspecifiedBoolType; 79 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; } 80 81 OwnPtr& operator=(const PassOwnPtr<T>&); 82 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } 83 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&); 84 85 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 86 OwnPtr(OwnPtr&&); 87 template<typename U> OwnPtr(OwnPtr<U>&&); 88 89 OwnPtr& operator=(OwnPtr&&); 90 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); 91 #endif 92 93 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } 94 95 private: 96 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 97 // If rvalue references are supported, noncopyable takes care of this. 98 OwnPtr& operator=(const OwnPtr&); 99 #endif 100 101 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get 102 // double-destruction), so these equality operators should never be needed. 103 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } 104 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } 105 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } 106 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } 107 108 PtrType m_ptr; 109 }; 110 111 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o) 112 : m_ptr(o.leakPtr()) 113 { 114 } 115 116 template<typename T> inline void OwnPtr<T>::clear() 117 { 118 PtrType ptr = m_ptr; 119 m_ptr = 0; 120 deleteOwnedPtr(ptr); 121 } 122 123 template<typename T> inline PassOwnPtr<T> OwnPtr<T>::release() 124 { 125 PtrType ptr = m_ptr; 126 m_ptr = 0; 127 return adoptPtr(ptr); 128 } 129 130 template<typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr() 131 { 132 PtrType ptr = m_ptr; 133 m_ptr = 0; 134 return ptr; 135 } 136 137 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o) 138 { 139 PtrType ptr = m_ptr; 140 m_ptr = o.leakPtr(); 141 ASSERT(!ptr || m_ptr != ptr); 142 deleteOwnedPtr(ptr); 143 return *this; 144 } 145 146 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o) 147 { 148 PtrType ptr = m_ptr; 149 m_ptr = o.leakPtr(); 150 ASSERT(!ptr || m_ptr != ptr); 151 deleteOwnedPtr(ptr); 152 return *this; 153 } 154 155 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 156 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) 157 : m_ptr(o.leakPtr()) 158 { 159 } 160 161 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o) 162 : m_ptr(o.leakPtr()) 163 { 164 } 165 166 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<T>&& o) 167 { 168 PtrType ptr = m_ptr; 169 m_ptr = o.leakPtr(); 170 ASSERT(!ptr || m_ptr != ptr); 171 deleteOwnedPtr(ptr); 172 173 return *this; 174 } 175 176 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<U>&& o) 177 { 178 PtrType ptr = m_ptr; 179 m_ptr = o.leakPtr(); 180 ASSERT(!ptr || m_ptr != ptr); 181 deleteOwnedPtr(ptr); 182 183 return *this; 184 } 185 #endif 186 187 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) 188 { 189 a.swap(b); 190 } 191 192 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b) 193 { 194 return a.get() == b; 195 } 196 197 template<typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b) 198 { 199 return a == b.get(); 200 } 201 202 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b) 203 { 204 return a.get() != b; 205 } 206 207 template<typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b) 208 { 209 return a != b.get(); 210 } 211 212 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p) 213 { 214 return p.get(); 215 } 216 217 } // namespace WTF 218 219 using WTF::OwnPtr; 220 221 #endif // WTF_OwnPtr_h 222