Changeset 66139 in webkit for trunk/JavaScriptCore/wtf/OwnArrayPtr.h
- Timestamp:
- Aug 26, 2010, 2:38:11 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/OwnArrayPtr.h
r62674 r66139 22 22 #define WTF_OwnArrayPtr_h 23 23 24 #include "Assertions.h" 25 #include "Noncopyable.h" 26 #include "OwnArrayPtrCommon.h" 24 27 #include <algorithm> 25 #include <wtf/Assertions.h> 26 #include <wtf/Noncopyable.h> 28 29 // Remove this once we make all WebKit code compatible with stricter rules about OwnArrayPtr. 30 #define LOOSE_OWN_ARRAY_PTR 27 31 28 32 namespace WTF { 29 33 30 template <typename T> class OwnArrayPtr : public Noncopyable { 31 public: 32 explicit OwnArrayPtr(T* ptr = 0) : m_ptr(ptr) { } 33 ~OwnArrayPtr() { safeDelete(m_ptr); } 34 template<typename T> class PassOwnArrayPtr; 35 template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*); 34 36 35 T* get() const { return m_ptr; } 36 T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; } 37 template <typename T> class OwnArrayPtr : public Noncopyable { 38 public: 39 typedef T* PtrType; 37 40 38 // FIXME: This should be removed and replaced with PassOwnArrayPtr. 39 void set(T* ptr) 40 { 41 ASSERT(!ptr || m_ptr != ptr); 42 T* oldPtr = m_ptr; 43 m_ptr = ptr; 44 safeDelete(oldPtr); 45 } 41 OwnArrayPtr() : m_ptr(0) { } 46 42 47 void clear(); 43 // See comment in PassOwnArrayPtr.h for why this takes a const reference. 44 template<typename U> OwnArrayPtr(const PassOwnArrayPtr<U>& o); 48 45 49 T& operator*() const { ASSERT(m_ptr); return *m_ptr; } 50 T* operator->() const { ASSERT(m_ptr); return m_ptr; } 46 // This copy constructor is used implicitly by gcc when it generates 47 // transients for assigning a PassOwnArrayPtr<T> object to a stack-allocated 48 // OwnArrayPtr<T> object. It should never be called explicitly and gcc 49 // should optimize away the constructor when generating code. 50 OwnArrayPtr(const OwnArrayPtr<T>&); 51 51 52 T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }52 ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); } 53 53 54 bool operator!() const { return !m_ptr; }54 PtrType get() const { return m_ptr; } 55 55 56 // This conversion operator allows implicit conversion to bool but not to other integer types. 56 void clear(); 57 PassOwnArrayPtr<T> release(); 58 PtrType leakPtr() WARN_UNUSED_RETURN; 59 60 T& operator*() const { ASSERT(m_ptr); return *m_ptr; } 61 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } 62 63 T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; } 64 65 bool operator!() const { return !m_ptr; } 66 67 // This conversion operator allows implicit conversion to bool but not to other integer types. 57 68 #if COMPILER(WINSCW) 58 69 operator bool() const { return m_ptr; } 59 70 #else 60 61 71 typedef T* OwnArrayPtr::*UnspecifiedBoolType; 72 operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; } 62 73 #endif 63 74 64 void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); } 75 OwnArrayPtr& operator=(const PassOwnArrayPtr<T>&); 76 template<typename U> OwnArrayPtr& operator=(const PassOwnArrayPtr<U>&); 65 77 66 private: 67 static void safeDelete(T*); 78 void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); } 68 79 69 T* m_ptr; 70 }; 71 72 template<typename T> inline void OwnArrayPtr<T>::clear() 73 { 74 T* ptr = m_ptr; 75 m_ptr = 0; 76 safeDelete(ptr); 77 } 80 #ifdef LOOSE_OWN_ARRAY_PTR 81 explicit OwnArrayPtr(PtrType ptr) : m_ptr(ptr) { } 82 void set(PtrType); 83 #endif 78 84 79 template<typename T> inline void OwnArrayPtr<T>::safeDelete(T* ptr) 80 { 81 typedef char known[sizeof(T) ? 1 : -1]; 82 if (sizeof(known)) 83 delete [] ptr; 84 } 85 private: 86 PtrType m_ptr; 87 }; 85 88 86 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b) { a.swap(b); } 89 template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o) 90 : m_ptr(o.leakPtr()) 91 { 92 } 87 93 88 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p) 89 { 90 return p.get(); 91 } 94 template<typename T> inline void OwnArrayPtr<T>::clear() 95 { 96 PtrType ptr = m_ptr; 97 m_ptr = 0; 98 deleteOwnedArrayPtr(ptr); 99 } 100 101 template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release() 102 { 103 PtrType ptr = m_ptr; 104 m_ptr = 0; 105 return adoptArrayPtr(ptr); 106 } 107 108 template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr() 109 { 110 PtrType ptr = m_ptr; 111 m_ptr = 0; 112 return ptr; 113 } 114 115 #ifdef LOOSE_OWN_ARRAY_PTR 116 template<typename T> inline void OwnArrayPtr<T>::set(PtrType ptr) 117 { 118 ASSERT(!ptr || m_ptr != ptr); 119 PtrType oldPtr = m_ptr; 120 m_ptr = ptr; 121 deleteOwnedPtr(oldPtr); 122 } 123 #endif 124 125 template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o) 126 { 127 PtrType ptr = m_ptr; 128 m_ptr = o.leakPtr(); 129 ASSERT(!ptr || m_ptr != ptr); 130 deleteOwnedArrayPtr(ptr); 131 return *this; 132 } 133 134 template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o) 135 { 136 PtrType ptr = m_ptr; 137 m_ptr = o.leakPtr(); 138 ASSERT(!ptr || m_ptr != ptr); 139 deleteOwnedArrayPtr(ptr); 140 return *this; 141 } 142 143 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b) 144 { 145 a.swap(b); 146 } 147 148 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b) 149 { 150 return a.get() == b; 151 } 152 153 template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b) 154 { 155 return a == b.get(); 156 } 157 158 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b) 159 { 160 return a.get() != b; 161 } 162 163 template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b) 164 { 165 return a != b.get(); 166 } 167 168 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p) 169 { 170 return p.get(); 171 } 92 172 93 173 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.