分类: C/C++
2013-12-03 17:41:13
CRtReferenceControlT(){}
virtual ~CRtReferenceControlT(){}
DWORD AddReference(){
return ++m_Atomic;
}
DWORD ReleaseReference(){
DWORD dwRef = --m_Atomic;if (dwRef == 0)OnReferenceDestory();return dwRef;
}
DWORD GetReference(){
return m_Atomic.GetValue();
}
protected:
virtual void OnReferenceDestory()
{
delete this;
}
CRtAtomicOperationTm_Atomic;
CRtAutoPtr(T *aPtr = NULL): m_pRawPtr(aPtr){
if (m_pRawPtr)m_pRawPtr->AddReference();
}
CRtAutoPtr(const CRtAutoPtr& aAutoPtr): m_pRawPtr(aAutoPtr.m_pRawPtr){
if (m_pRawPtr)m_pRawPtr->AddReference();
}
~CRtAutoPtr(){
if (m_pRawPtr)m_pRawPtr->ReleaseReference();
}
CRtAutoPtr& operator = (const CRtAutoPtr& aAutoPtr){
return (*this = aAutoPtr.m_pRawPtr);
}
CRtAutoPtr& operator = (T* aPtr){
if (m_pRawPtr == aPtr)
return *this;
if (aPtr)
aPtr->AddReference();
if (m_pRawPtr)
m_pRawPtr->ReleaseReference();
m_pRawPtr = aPtr;return *this;
}
operator void* () const{
return m_pRawPtr;
}
T* operator -> () const{
RT_ASSERTE(m_pRawPtr);return m_pRawPtr;
}
T* Get() const{
return m_pRawPtr;
}
T* ParaIn() const{
return m_pRawPtr;
}
T*& ParaOut(){
if (m_pRawPtr) {
m_pRawPtr->ReleaseReference();m_pRawPtr = NULL;
}
return static_cast(m_pRawPtr);
}
T*& ParaInOut(){
return static_cast(m_pRawPtr);
}
T& operator * () const{
RT_ASSERTE(m_pRawPtr);return *m_pRawPtr;
}
private:
T *m_pRawPtr;