WTF
PassRefPtr.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef WTF_PassRefPtr_h
00023 #define WTF_PassRefPtr_h
00024
00025 #include "AlwaysInline.h"
00026
00027 namespace WTF {
00028
00029 template<typename T> class RefPtr;
00030 template<typename T> class PassRefPtr;
00031 template <typename T> PassRefPtr<T> adoptRef(T*);
00032
00033 template<typename T> class PassRefPtr {
00034 public:
00035 PassRefPtr() : m_ptr(0) {}
00036 PassRefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
00037
00038
00039
00040
00041 PassRefPtr(const PassRefPtr& o) : m_ptr(o.releaseRef()) {}
00042 template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.releaseRef()) { }
00043
00044 ALWAYS_INLINE ~PassRefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
00045
00046 template <class U>
00047 PassRefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
00048
00049 T* get() const { return m_ptr; }
00050
00051 void clear() { if (T* ptr = m_ptr) ptr->deref(); m_ptr = 0; }
00052 T* releaseRef() const { T* tmp = m_ptr; m_ptr = 0; return tmp; }
00053
00054 T& operator*() const { return *m_ptr; }
00055 T* operator->() const { return m_ptr; }
00056
00057 bool operator!() const { return !m_ptr; }
00058
00059
00060 typedef T* PassRefPtr::*UnspecifiedBoolType;
00061 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : 0; }
00062
00063 PassRefPtr& operator=(T*);
00064 PassRefPtr& operator=(const PassRefPtr&);
00065 template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
00066 template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
00067
00068 friend PassRefPtr adoptRef<T>(T*);
00069 private:
00070
00071 PassRefPtr(T* ptr, bool) : m_ptr(ptr) {}
00072 mutable T* m_ptr;
00073 };
00074
00075 template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const RefPtr<U>& o)
00076 {
00077 T* optr = o.get();
00078 if (optr)
00079 optr->ref();
00080 T* ptr = m_ptr;
00081 m_ptr = optr;
00082 if (ptr)
00083 ptr->deref();
00084 return *this;
00085 }
00086
00087 template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(T* optr)
00088 {
00089 if (optr)
00090 optr->ref();
00091 T* ptr = m_ptr;
00092 m_ptr = optr;
00093 if (ptr)
00094 ptr->deref();
00095 return *this;
00096 }
00097
00098 template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
00099 {
00100 T* ptr = m_ptr;
00101 m_ptr = ref.releaseRef();
00102 if (ptr)
00103 ptr->deref();
00104 return *this;
00105 }
00106
00107 template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
00108 {
00109 T* ptr = m_ptr;
00110 m_ptr = ref.releaseRef();
00111 if (ptr)
00112 ptr->deref();
00113 return *this;
00114 }
00115
00116 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
00117 {
00118 return a.get() == b.get();
00119 }
00120
00121 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b)
00122 {
00123 return a.get() == b.get();
00124 }
00125
00126 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b)
00127 {
00128 return a.get() == b.get();
00129 }
00130
00131 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b)
00132 {
00133 return a.get() == b;
00134 }
00135
00136 template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b)
00137 {
00138 return a == b.get();
00139 }
00140
00141 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
00142 {
00143 return a.get() != b.get();
00144 }
00145
00146 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b)
00147 {
00148 return a.get() != b.get();
00149 }
00150
00151 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b)
00152 {
00153 return a.get() != b.get();
00154 }
00155
00156 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
00157 {
00158 return a.get() != b;
00159 }
00160
00161 template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b)
00162 {
00163 return a != b.get();
00164 }
00165
00166 template <typename T> inline PassRefPtr<T> adoptRef(T* p)
00167 {
00168 return PassRefPtr<T>(p, true);
00169 }
00170
00171 template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p)
00172 {
00173 return adoptRef(static_cast<T*>(p.releaseRef()));
00174 }
00175
00176 template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p)
00177 {
00178 return adoptRef(const_cast<T*>(p.releaseRef()));
00179 }
00180
00181 template <typename T> inline T* getPtr(const PassRefPtr<T>& p)
00182 {
00183 return p.get();
00184 }
00185
00186 }
00187
00188 using WTF::PassRefPtr;
00189 using WTF::adoptRef;
00190 using WTF::static_pointer_cast;
00191 using WTF::const_pointer_cast;
00192
00193 #endif // WTF_PassRefPtr_h