GrabBag/VrCommon/Inc/IVrUnkown.h

52 lines
739 B
C
Raw Permalink Normal View History

2025-07-23 01:35:14 +08:00
#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;
}
}
}