1 /* 2 * Copyright (C) 2006, 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_OwnArrayPtr_h 22 #define WTF_OwnArrayPtr_h 23 24 #include "wtf/Assertions.h" 25 #include "wtf/Noncopyable.h" 26 #include "wtf/NullPtr.h" 27 #include "wtf/PassOwnArrayPtr.h" 28 #include <algorithm> 29 30 namespace WTF { 31 32 template<typename T> class PassOwnArrayPtr; 33 template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*); 34 35 template <typename T> class OwnArrayPtr { 36 public: 37 typedef T* PtrType; 38 39 OwnArrayPtr() : m_ptr(0) { } 40 41 // See comment in PassOwnArrayPtr.h for why this takes a const reference. 42 template<typename U> OwnArrayPtr(const PassOwnArrayPtr<U>& o); 43 44 // This copy constructor is used implicitly by gcc when it generates 45 // transients for assigning a PassOwnArrayPtr<T> object to a stack-allocated 46 // OwnArrayPtr<T> object. It should never be called explicitly and gcc 47 // should optimize away the constructor when generating code. 48 OwnArrayPtr(const OwnArrayPtr<T>&); 49 50 ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); } 51 52 PtrType get() const { return m_ptr; } 53 54 void clear(); 55 PassOwnArrayPtr<T> release(); 56 PtrType leakPtr() WARN_UNUSED_RETURN; 57 58 T& operator*() const { ASSERT(m_ptr); return *m_ptr; } 59 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } 60 61 T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; } 62 63 bool operator!() const { return !m_ptr; } 64 65 // This conversion operator allows implicit conversion to bool but not to other integer types. 66 typedef T* OwnArrayPtr::*UnspecifiedBoolType; 67 operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; } 68 69 OwnArrayPtr& operator=(const PassOwnArrayPtr<T>&); 70 OwnArrayPtr& operator=(std::nullptr_t) { clear(); return *this; } 71 template<typename U> OwnArrayPtr& operator=(const PassOwnArrayPtr<U>&); 72 73 void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); } 74 75 private: 76 PtrType m_ptr; 77 }; 78 79 template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o) 80 : m_ptr(o.leakPtr()) 81 { 82 } 83 84 template<typename T> inline void OwnArrayPtr<T>::clear() 85 { 86 PtrType ptr = m_ptr; 87 m_ptr = 0; 88 deleteOwnedArrayPtr(ptr); 89 } 90 91 template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release() 92 { 93 PtrType ptr = m_ptr; 94 m_ptr = 0; 95 return adoptArrayPtr(ptr); 96 } 97 98 template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr() 99 { 100 PtrType ptr = m_ptr; 101 m_ptr = 0; 102 return ptr; 103 } 104 105 template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o) 106 { 107 PtrType ptr = m_ptr; 108 m_ptr = o.leakPtr(); 109 ASSERT(!ptr || m_ptr != ptr); 110 deleteOwnedArrayPtr(ptr); 111 return *this; 112 } 113 114 template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o) 115 { 116 PtrType ptr = m_ptr; 117 m_ptr = o.leakPtr(); 118 ASSERT(!ptr || m_ptr != ptr); 119 deleteOwnedArrayPtr(ptr); 120 return *this; 121 } 122 123 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b) 124 { 125 a.swap(b); 126 } 127 128 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b) 129 { 130 return a.get() == b; 131 } 132 133 template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b) 134 { 135 return a == b.get(); 136 } 137 138 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b) 139 { 140 return a.get() != b; 141 } 142 143 template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b) 144 { 145 return a != b.get(); 146 } 147 148 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p) 149 { 150 return p.get(); 151 } 152 153 } // namespace WTF 154 155 using WTF::OwnArrayPtr; 156 157 #endif // WTF_OwnArrayPtr_h 158