WTF
RefPtr.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
00023 #ifndef WTF_RefPtr_h
00024 #define WTF_RefPtr_h
00025
00026 #include <algorithm>
00027 #include "PassRefPtr.h"
00028
00029 namespace WTF {
00030
00031 template <typename T> class PassRefPtr;
00032
00033 template <typename T> class RefPtr
00034 {
00035 public:
00036 RefPtr() : m_ptr(0) {}
00037 RefPtr(T& ptr) : m_ptr(&ptr) {}
00038 RefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
00039 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
00040
00041 template <typename U> RefPtr(const PassRefPtr<U>&);
00042
00043 ~RefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
00044
00045 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }
00046
00047 T *get() const { return m_ptr; }
00048
00049 void assign(T& ptr) { m_ptr = &ptr; }
00050 void retain(T& ptr) { m_ptr = &ptr; ptr.ref(); }
00051 void reset(T& ptr) { T* optr = m_ptr; m_ptr = &ptr; if (optr) optr->deref(); }
00052 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(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 * (RefPtr::*UnspecifiedBoolType)() const;
00061 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::get : 0; }
00062
00066 RefPtr& operator=(const RefPtr&);
00067 RefPtr& operator=(T *);
00068 RefPtr& operator=(const PassRefPtr<T>&);
00069 RefPtr& operator=(T& ptr) { T* optr = m_ptr; m_ptr = &ptr; if (optr) optr->deref(); return *this; }
00070
00074 template <typename U> RefPtr& operator=(const RefPtr<U>&);
00075 template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
00076
00077 void swap(RefPtr&);
00078
00079 private:
00080 T *m_ptr;
00081 };
00082
00083 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
00084 : m_ptr(o.release())
00085 {
00086 }
00087
00088 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
00089 {
00090 T* optr = o.get();
00091 if (optr)
00092 optr->ref();
00093 T* ptr = m_ptr;
00094 m_ptr = optr;
00095 if (ptr)
00096 ptr->deref();
00097 return *this;
00098 }
00099
00100 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
00101 {
00102 T* optr = o.get();
00103 if (optr)
00104 optr->ref();
00105 T* ptr = m_ptr;
00106 m_ptr = optr;
00107 if (ptr)
00108 ptr->deref();
00109 return *this;
00110 }
00111
00112 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
00113 {
00114 if (optr)
00115 optr->ref();
00116 T* ptr = m_ptr;
00117 m_ptr = optr;
00118 if (ptr)
00119 ptr->deref();
00120 return *this;
00121 }
00122
00123 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
00124 {
00125 T* ptr = m_ptr;
00126 m_ptr = o.release();
00127 if (ptr)
00128 ptr->deref();
00129 return *this;
00130 }
00131
00132 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
00133 {
00134 T* ptr = m_ptr;
00135 m_ptr = o.release();
00136 if (ptr)
00137 ptr->deref();
00138 return *this;
00139 }
00140
00141 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
00142 {
00143 std::swap(m_ptr, o.m_ptr);
00144 }
00145
00146 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
00147 {
00148 a.swap(b);
00149 }
00150
00151 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
00152 {
00153 return a.get() == b.get();
00154 }
00155
00156 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
00157 {
00158 return a.get() == b;
00159 }
00160
00161 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
00162 {
00163 return a == b.get();
00164 }
00165
00166 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
00167 {
00168 return a.get() != b.get();
00169 }
00170
00171 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
00172 {
00173 return a.get() != b;
00174 }
00175
00176 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
00177 {
00178 return a != b.get();
00179 }
00180
00181 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
00182 {
00183 return RefPtr<T>(static_cast<T *>(p.get()));
00184 }
00185
00186 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
00187 {
00188 return RefPtr<T>(const_cast<T *>(p.get()));
00189 }
00190
00191 }
00192
00193 using WTF::RefPtr;
00194 using WTF::static_pointer_cast;
00195 using WTF::const_pointer_cast;
00196
00197 #endif // WTF_RefPtr_h