52 lines
688 B
C
52 lines
688 B
C
|
|
#pragma once
|
|||
|
|
#include <atomic>
|
|||
|
|
|
|||
|
|
class IVrUnkown
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
IVrUnkown() :m_nRefCount(1) {}
|
|||
|
|
virtual ~IVrUnkown() {}
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD><EFBFBD> +1
|
|||
|
|
void AddRef()
|
|||
|
|
{
|
|||
|
|
std::atomic_fetch_add(&m_nRefCount, 1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD><EFBFBD> -1
|
|||
|
|
int Release()
|
|||
|
|
{
|
|||
|
|
int nRef = std::atomic_fetch_sub(&m_nRefCount, 1); //atomic_fetch_sub ִ<>з<EFBFBD><D0B7><EFBFBD>ԭֵ
|
|||
|
|
if (1 == nRef) //Ĭ<><C4AC><EFBFBD><EFBFBD>1 <20><><EFBFBD><EFBFBD>1ʱɾ<CAB1><C9BE>
|
|||
|
|
{
|
|||
|
|
delete this;
|
|||
|
|
}
|
|||
|
|
return nRef - 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
std::atomic<int> m_nRefCount; //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
template<typename T>
|
|||
|
|
void VrSafeAddRef(T* pObj)
|
|||
|
|
{
|
|||
|
|
if (pObj)
|
|||
|
|
{
|
|||
|
|
pObj->AddRef();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
template<typename T>
|
|||
|
|
void VrSafeReleaseRef(T** pObj)
|
|||
|
|
{
|
|||
|
|
if (pObj && *pObj)
|
|||
|
|
{
|
|||
|
|
T* p = *pObj;
|
|||
|
|
if(0 == p->Release())
|
|||
|
|
{
|
|||
|
|
*pObj = nullptr;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|