1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-19 16:40:26 +02:00
Beef/BeefLibs/corlib/src/IRefCounted.bf

58 lines
812 B
Beef
Raw Normal View History

2019-08-23 11:56:54 -07:00
using System.Threading;
using System.Diagnostics;
namespace System
{
interface IRefCounted
{
void AddRef();
void ReleaseRef();
}
class RefCounted : IRefCounted
{
protected int32 mRefCount = 1;
public ~this()
{
Debug.Assert(mRefCount == 0);
}
public int RefCount
{
get
{
return mRefCount;
}
}
public void DeleteUnchecked()
{
mRefCount = 0;
delete this;
}
public void AddRef()
{
Interlocked.Increment(ref mRefCount);
}
public void ReleaseRef()
{
int refCount = Interlocked.Decrement(ref mRefCount);
Debug.Assert(refCount >= 0);
if (refCount == 0)
{
delete this;
}
}
public int ReleaseRefNoDelete()
{
int refCount = Interlocked.Decrement(ref mRefCount);
Debug.Assert(refCount >= 0);
return refCount;
}
}
}