![]() |
Humble Framework for SkyOS |
00001 00007 #ifndef HPTR_H 00008 #define HPTR_H 00009 00010 #ifndef HDEBUG_H 00011 # include "HDebug.h" 00012 #endif 00013 /* ---------------------------------------------------------------------- 00014 HPtr class -- clone of the standard auto_ptr() template class 00015 ---------------------------------------------------------------------- */ 00027 template <class T> 00028 class HPtr 00029 { 00030 /* 00031 ** Variables 00032 */ 00033 protected: 00034 T * m_pT; 00035 uint32 * m_puRefs; 00036 /* 00037 ** Methods 00038 */ 00039 protected: 00043 void 00044 addRef(void) 00045 { 00046 ASSERT_PTR(m_puRefs); 00047 ++(*m_puRefs); 00048 } 00052 void 00053 delRef(void) 00054 { 00055 ASSERT_PTR(m_puRefs); 00056 ASSERT( *m_puRefs > 0 ); 00057 00058 if (--(*m_puRefs) == 0) 00059 { 00060 delete m_puRefs; 00061 delete m_pT; 00062 } 00063 } 00064 public: 00070 uint32 00071 GetRefCount(void) const 00072 { return GOOD_PTR(m_puRefs) ? *m_puRefs : 0; } 00078 bool 00079 IsNotValid(void) const 00080 { return NULL_PTR(m_pT); } 00086 bool 00087 IsValid(void) const 00088 { return GOOD_PTR(m_pT); } 00089 // 00090 // OPERATORS 00091 // 00092 HPtr<T> & 00093 operator = (const HPtr<T> & rhs) throw() 00094 { 00095 if (this != rhs) 00096 { 00097 delRef(); 00098 m_pT = rhs.m_pT; 00099 m_puRefs = rhs.m_puRefs; 00100 addRef(); 00101 } 00102 00103 return *this; 00104 } 00105 00106 T & 00107 operator * () const throw() 00108 { 00109 ASSERT_PTR(m_pT); 00110 return *m_pT; 00111 } 00112 00113 T * 00114 operator -> () const throw() 00115 { 00116 ASSERT_PTR(m_pT); 00117 return m_pT; 00118 } 00119 // 00120 // CTOR / DTOR 00121 // 00122 explicit 00123 HPtr(T * p = NULL) : m_pT(p), m_puRefs(new uint32(0)) 00124 { addRef(); } 00125 HPtr(const HPtr<T> & p) throw() : m_pT(p.m_pT), m_puRefs(p.m_puRefs) 00126 { addRef(); } 00127 00128 ~HPtr() throw() 00129 { delRef(); } 00130 }; 00131 00132 #endif // HPTR_H 00133 /**************************************************************************** 00134 ** 00135 ** $History: HPtr.h $ 00136 * 00137 * ***************** Version 1 ***************** 00138 * User: Neusel Date: 11/23/04 Time: 8:24a 00139 * Created in $/SkyOS.root/pig/Humble 00140 ** 00141 ** ------------------------------------------------------------------------- 00142 ** 00143 ** End of HPtr.h 00144 ** 00145 ****************************************************************************/