00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00046 #ifndef OW_INTRUSIVE_REFERENCE_HPP_INCLUDE_GUARD_
00047 #define OW_INTRUSIVE_REFERENCE_HPP_INCLUDE_GUARD_
00048
00049 #include "OW_config.h"
00050
00051 namespace OW_NAMESPACE
00052 {
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 template<class T> class IntrusiveReference
00071 {
00072 private:
00073 typedef IntrusiveReference this_type;
00074 public:
00075 typedef T element_type;
00076
00077 IntrusiveReference(): m_pObj(0)
00078 {
00079 }
00080 IntrusiveReference(T * p, bool add_ref = true): m_pObj(p)
00081 {
00082 if (m_pObj != 0 && add_ref) IntrusiveReferenceAddRef(m_pObj);
00083 }
00084 template<class U> IntrusiveReference(IntrusiveReference<U> const & rhs): m_pObj(rhs.getPtr())
00085 {
00086 if (m_pObj != 0) IntrusiveReferenceAddRef(m_pObj);
00087 }
00088 IntrusiveReference(IntrusiveReference const & rhs): m_pObj(rhs.m_pObj)
00089 {
00090 if (m_pObj != 0) IntrusiveReferenceAddRef(m_pObj);
00091 }
00092 ~IntrusiveReference()
00093 {
00094 if (m_pObj != 0) IntrusiveReferenceRelease(m_pObj);
00095 }
00096 template<class U> IntrusiveReference & operator=(IntrusiveReference<U> const & rhs)
00097 {
00098 this_type(rhs).swap(*this);
00099 return *this;
00100 }
00101 IntrusiveReference & operator=(IntrusiveReference const & rhs)
00102 {
00103 this_type(rhs).swap(*this);
00104 return *this;
00105 }
00106 IntrusiveReference & operator=(T * rhs)
00107 {
00108 this_type(rhs).swap(*this);
00109 return *this;
00110 }
00111 T * getPtr() const
00112 {
00113 return m_pObj;
00114 }
00115 T & operator*() const
00116 {
00117 return *m_pObj;
00118 }
00119 T * operator->() const
00120 {
00121 return m_pObj;
00122 }
00123 typedef T * this_type::*safe_bool;
00124 operator safe_bool() const
00125 {
00126 return m_pObj == 0? 0: &this_type::m_pObj;
00127 }
00128 bool operator! () const
00129 {
00130 return m_pObj == 0;
00131 }
00132 OW_DEPRECATED bool isNull() const
00133 {
00134 return m_pObj == 0;
00135 }
00136
00137 void swap(IntrusiveReference & rhs)
00138 {
00139 T * tmp = m_pObj;
00140 m_pObj = rhs.m_pObj;
00141 rhs.m_pObj = tmp;
00142 }
00143
00144 template <class U>
00145 IntrusiveReference<U> cast_to() const
00146 {
00147 return IntrusiveReference<U>(dynamic_cast<U*>(m_pObj));
00148 }
00149
00150 private:
00151 T * m_pObj;
00152 };
00153 template<class T, class U> inline bool operator==(IntrusiveReference<T> const & a, IntrusiveReference<U> const & b)
00154 {
00155 return a.getPtr() == b.getPtr();
00156 }
00157 template<class T, class U> inline bool operator!=(IntrusiveReference<T> const & a, IntrusiveReference<U> const & b)
00158 {
00159 return a.getPtr() != b.getPtr();
00160 }
00161 template<class T> inline bool operator==(IntrusiveReference<T> const & a, T * b)
00162 {
00163 return a.getPtr() == b;
00164 }
00165 template<class T> inline bool operator!=(IntrusiveReference<T> const & a, T * b)
00166 {
00167 return a.getPtr() != b;
00168 }
00169 template<class T> inline bool operator==(T * a, IntrusiveReference<T> const & b)
00170 {
00171 return a == b.getPtr();
00172 }
00173 template<class T> inline bool operator!=(T * a, IntrusiveReference<T> const & b)
00174 {
00175 return a != b.getPtr();
00176 }
00177 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00178
00179 template<class T> inline bool operator!=(IntrusiveReference<T> const & a, IntrusiveReference<T> const & b)
00180 {
00181 return a.getPtr() != b.getPtr();
00182 }
00183 #endif
00184 template<class T> inline bool operator<(IntrusiveReference<T> const & a, IntrusiveReference<T> const & b)
00185 {
00186 return a.getPtr() < b.getPtr();
00187 }
00188 template<class T> void swap(IntrusiveReference<T> & lhs, IntrusiveReference<T> & rhs)
00189 {
00190 lhs.swap(rhs);
00191 }
00192 template<class T, class U> IntrusiveReference<T> static_pointer_cast(IntrusiveReference<U> const & p)
00193 {
00194 return static_cast<T *>(p.getPtr());
00195 }
00196 template<class T, class U> IntrusiveReference<T> const_pointer_cast(IntrusiveReference<U> const & p)
00197 {
00198 return const_cast<T *>(p.getPtr());
00199 }
00200 template<class T, class U> IntrusiveReference<T> dynamic_pointer_cast(IntrusiveReference<U> const & p)
00201 {
00202 return dynamic_cast<T *>(p.getPtr());
00203 }
00204
00205 }
00206
00207 #endif