00001 #ifndef REFCNTDYNOBJPTR_HPP
00002 #define REFCNTDYNOBJPTR_HPP
00003
00004 #include "DoBase.hpp"
00005 #include "DoError.h"
00006
00015 template<class T>
00016 struct RefCntDynObjPtr {
00020 explicit RefCntDynObjPtr( T* pt=NULL, bool do_add_ref=true ) : m_pt(0) {
00021 if( !pt ) return;
00022 DoAssign( pt, do_add_ref );
00023 }
00024
00027 RefCntDynObjPtr& operator = (T* pt){
00028 DoRelease();
00029 DoAssign( pt, true );
00030 return *this;
00031 }
00032
00035 RefCntDynObjPtr& operator = (const RefCntDynObjPtr<T>& dop){
00036 return operator = ((T*)dop);
00037 }
00038
00039
00041 operator T* () { return m_pt; }
00043 operator T& () { return *m_pt; }
00045 T& operator * () { return *m_pt; }
00047 T* operator -> () { return m_pt; }
00049 T* Get(){ return m_pt; }
00050
00052 bool IsOk(){ return m_pt!=NULL; }
00053
00055 bool Release(){ T*pt=m_pt; DoRelease(); return pt && !m_pt; }
00056
00057 protected:
00058 bool DoAssign( T* pt, bool do_add_ref ){
00059 RefCountI *prci = DoCasterSelect<T,RefCountI,DoIsTypeA<T,RefCountI>::v>::Cast(pt,NULL);
00060 if( !prci ) return false;
00061 m_pt = pt;
00062 if( do_add_ref )
00063 prci->IncRef();
00064 return true;
00065 }
00066
00067 void DoRelease(){
00068 if( !m_pt ) return;
00069 RefCountI *prci = DoCasterSelect<T,RefCountI,DoIsTypeA<T,RefCountI>::v>::Cast(m_pt,NULL);
00070 DO_ASSERT_MSG( prci, "Should get RefCountI" );
00071 if( prci )
00072 prci->DecRef();
00073 m_pt = NULL;
00074 }
00075
00076 T* m_pt;
00077 };
00078
00079 #endif // REFCNTDYNOBJPTR_HPP
00080
00081