metaforce/Runtime/CToken.cpp

195 lines
3.9 KiB
C++
Raw Normal View History

2016-09-20 22:41:51 -07:00
#include "CToken.hpp"
namespace urde
{
u16 CObjectReference::RemoveReference()
{
--x0_refCount;
if (x0_refCount == 0)
{
if (x10_object)
Unload();
if (IsLoading())
CancelLoad();
if (xC_objectStore)
xC_objectStore->ObjectUnreferenced(x4_objTag);
}
return x0_refCount;
}
CObjectReference::CObjectReference(IObjectStore& objStore, std::unique_ptr<IObj>&& obj,
const SObjectTag& objTag, CVParamTransfer buildParams)
: x4_objTag(objTag), xC_objectStore(&objStore),
x10_object(std::move(obj)), x14_params(buildParams) {}
2016-09-20 22:41:51 -07:00
CObjectReference::CObjectReference(std::unique_ptr<IObj>&& obj)
: x10_object(std::move(obj)) {}
2016-09-20 22:41:51 -07:00
void CObjectReference::Unlock()
{
--x2_lockCount;
if (x2_lockCount)
return;
if (x10_object && xC_objectStore)
Unload();
else if (IsLoading())
CancelLoad();
}
void CObjectReference::Lock()
{
++x2_lockCount;
if (!x10_object && !x3_loading)
{
IFactory& fac = xC_objectStore->GetFactory();
fac.BuildAsync(x4_objTag, x14_params, &x10_object, this);
2018-01-13 22:44:22 -08:00
x3_loading = !x10_object.operator bool();
2016-09-20 22:41:51 -07:00
}
}
void CObjectReference::CancelLoad()
{
if (xC_objectStore && IsLoading())
{
xC_objectStore->GetFactory().CancelBuild(x4_objTag);
x3_loading = false;
}
}
void CObjectReference::Unload()
{
x10_object.reset();
2016-09-20 22:41:51 -07:00
x3_loading = false;
}
IObj* CObjectReference::GetObject()
{
if (!x10_object)
{
IFactory& factory = xC_objectStore->GetFactory();
x10_object = factory.Build(x4_objTag, x14_params, this);
2016-09-20 22:41:51 -07:00
}
x3_loading = false;
return x10_object.get();
2016-09-20 22:41:51 -07:00
}
CObjectReference::~CObjectReference()
{
if (x10_object)
x10_object.reset();
2016-09-20 22:41:51 -07:00
else if (x3_loading)
xC_objectStore->GetFactory().CancelBuild(x4_objTag);
}
void CToken::RemoveRef()
{
if (x0_objRef && x0_objRef->RemoveReference() == 0)
{
std::default_delete<CObjectReference>()(x0_objRef);
x0_objRef = nullptr;
}
}
CToken::CToken(CObjectReference* obj)
{
x0_objRef = obj;
++x0_objRef->x0_refCount;
}
void CToken::Unlock()
{
if (x0_objRef && x4_lockHeld)
{
x0_objRef->Unlock();
x4_lockHeld = false;
}
}
void CToken::Lock()
{
if (x0_objRef && !x4_lockHeld)
{
x0_objRef->Lock();
x4_lockHeld = true;
}
}
bool CToken::IsLoaded() const
{
if (!x0_objRef)
return false;
return x0_objRef->IsLoaded();
}
IObj* CToken::GetObj()
{
if (!x0_objRef)
return nullptr;
Lock();
return x0_objRef->GetObject();
}
CToken& CToken::operator=(const CToken& other)
{
Unlock();
RemoveRef();
2016-09-20 22:41:51 -07:00
x0_objRef = other.x0_objRef;
if (x0_objRef)
{
++x0_objRef->x0_refCount;
if (other.x4_lockHeld)
Lock();
}
return *this;
}
CToken& CToken::operator=(CToken&& other)
{
Unlock();
RemoveRef();
x0_objRef = other.x0_objRef;
other.x0_objRef = nullptr;
x4_lockHeld = other.x4_lockHeld;
other.x4_lockHeld = false;
return *this;
}
CToken::CToken(const CToken& other)
: x0_objRef(other.x0_objRef)
{
if (x0_objRef)
2017-01-28 19:58:16 -08:00
{
2016-09-20 22:41:51 -07:00
++x0_objRef->x0_refCount;
2017-01-28 19:58:16 -08:00
if (other.x4_lockHeld)
Lock();
}
2016-09-20 22:41:51 -07:00
}
CToken::CToken(CToken&& other)
: x0_objRef(other.x0_objRef), x4_lockHeld(other.x4_lockHeld)
{
other.x0_objRef = nullptr;
other.x4_lockHeld = false;
}
CToken::CToken(IObj* obj)
{
x0_objRef = new CObjectReference(std::unique_ptr<IObj>(obj));
++x0_objRef->x0_refCount;
Lock();
}
CToken::CToken(std::unique_ptr<IObj>&& obj)
{
x0_objRef = new CObjectReference(std::move(obj));
++x0_objRef->x0_refCount;
Lock();
}
const SObjectTag* CToken::GetObjectTag() const
{
if (!x0_objRef)
return nullptr;
return &x0_objRef->GetObjectTag();
}
CToken::~CToken()
{
if (x0_objRef)
{
if (x4_lockHeld)
x0_objRef->Unlock();
RemoveRef();
}
}
}