| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef WTF_PassOwnPtr_h |
| 27 |
#define WTF_PassOwnPtr_h |
| 28 |
|
| 29 |
#include "Assertions.h" |
| 30 |
#include "OwnPtrCommon.h" |
| 31 |
#include "TypeTraits.h" |
| 32 |
|
| 33 |
namespace WTF { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
template <typename T> class OwnPtr; |
| 38 |
|
| 39 |
template <typename T> class PassOwnPtr { |
| 40 |
public: |
| 41 |
typedef typename RemovePointer<T>::Type ValueType; |
| 42 |
typedef ValueType* PtrType; |
| 43 |
|
| 44 |
PassOwnPtr(PtrType ptr = 0) : m_ptr(ptr) { } |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.release()) { } |
| 50 |
template <typename U> PassOwnPtr(const PassOwnPtr<U>& o) : m_ptr(o.release()) { } |
| 51 |
|
| 52 |
~PassOwnPtr() { deleteOwnedPtr(m_ptr); } |
| 53 |
|
| 54 |
PtrType get() const { return m_ptr; } |
| 55 |
|
| 56 |
void clear() { m_ptr = 0; } |
| 57 |
PtrType release() const { PtrType ptr = m_ptr; m_ptr = 0; return ptr; } |
| 58 |
|
| 59 |
ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
| 60 |
PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
| 61 |
|
| 62 |
bool operator!() const { return !m_ptr; } |
| 63 |
|
| 64 |
|
| 65 |
typedef PtrType PassOwnPtr::*UnspecifiedBoolType; |
| 66 |
operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0; } |
| 67 |
|
| 68 |
PassOwnPtr& operator=(T*); |
| 69 |
PassOwnPtr& operator=(const PassOwnPtr<T>&); |
| 70 |
template <typename U> PassOwnPtr& operator=(const PassOwnPtr<U>&); |
| 71 |
|
| 72 |
private: |
| 73 |
mutable PtrType m_ptr; |
| 74 |
}; |
| 75 |
|
| 76 |
template <typename T> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(T* optr) |
| 77 |
{ |
| 78 |
T* ptr = m_ptr; |
| 79 |
m_ptr = optr; |
| 80 |
ASSERT(!ptr || m_ptr != ptr); |
| 81 |
if (ptr) |
| 82 |
deleteOwnedPtr(ptr); |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
template <typename T> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(const PassOwnPtr<T>& optr) |
| 87 |
{ |
| 88 |
T* ptr = m_ptr; |
| 89 |
m_ptr = optr.release(); |
| 90 |
ASSERT(!ptr || m_ptr != ptr); |
| 91 |
if (ptr) |
| 92 |
deleteOwnedPtr(ptr); |
| 93 |
return *this; |
| 94 |
} |
| 95 |
|
| 96 |
template <typename T> template <typename U> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(const PassOwnPtr<U>& optr) |
| 97 |
{ |
| 98 |
T* ptr = m_ptr; |
| 99 |
m_ptr = optr.release(); |
| 100 |
ASSERT(!ptr || m_ptr != ptr); |
| 101 |
if (ptr) |
| 102 |
deleteOwnedPtr(ptr); |
| 103 |
return *this; |
| 104 |
} |
| 105 |
|
| 106 |
template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, const PassOwnPtr<U>& b) |
| 107 |
{ |
| 108 |
return a.get() == b.get(); |
| 109 |
} |
| 110 |
|
| 111 |
template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, const OwnPtr<U>& b) |
| 112 |
{ |
| 113 |
return a.get() == b.get(); |
| 114 |
} |
| 115 |
|
| 116 |
template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, const PassOwnPtr<U>& b) |
| 117 |
{ |
| 118 |
return a.get() == b.get(); |
| 119 |
} |
| 120 |
|
| 121 |
template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, U* b) |
| 122 |
{ |
| 123 |
return a.get() == b; |
| 124 |
} |
| 125 |
|
| 126 |
template <typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<U>& b) |
| 127 |
{ |
| 128 |
return a == b.get(); |
| 129 |
} |
| 130 |
|
| 131 |
template <typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, const PassOwnPtr<U>& b) |
| 132 |
{ |
| 133 |
return a.get() != b.get(); |
| 134 |
} |
| 135 |
|
| 136 |
template <typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, const OwnPtr<U>& b) |
| 137 |
{ |
| 138 |
return a.get() != b.get(); |
| 139 |
} |
| 140 |
|
| 141 |
template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, const PassOwnPtr<U>& b) |
| 142 |
{ |
| 143 |
return a.get() != b.get(); |
| 144 |
} |
| 145 |
|
| 146 |
template <typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, U* b) |
| 147 |
{ |
| 148 |
return a.get() != b; |
| 149 |
} |
| 150 |
|
| 151 |
template <typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<U>& b) |
| 152 |
{ |
| 153 |
return a != b.get(); |
| 154 |
} |
| 155 |
|
| 156 |
template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p) |
| 157 |
{ |
| 158 |
return PassOwnPtr<T>(static_cast<T*>(p.release())); |
| 159 |
} |
| 160 |
|
| 161 |
template <typename T, typename U> inline PassOwnPtr<T> const_pointer_cast(const PassOwnPtr<U>& p) |
| 162 |
{ |
| 163 |
return PassOwnPtr<T>(const_cast<T*>(p.release())); |
| 164 |
} |
| 165 |
|
| 166 |
template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) |
| 167 |
{ |
| 168 |
return p.get(); |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
using WTF::PassOwnPtr; |
| 174 |
using WTF::const_pointer_cast; |
| 175 |
using WTF::static_pointer_cast; |
| 176 |
|
| 177 |
#endif |