metaforce/Runtime/CToken.hpp

231 lines
5.4 KiB
C++
Raw Normal View History

2015-08-20 17:06:39 -07:00
#ifndef __RETRO_CTOKEN_HPP__
#define __RETRO_CTOKEN_HPP__
2015-08-21 18:58:41 -07:00
#include <memory>
2015-08-22 23:42:29 -07:00
#include "IObj.hpp"
2015-08-21 18:58:41 -07:00
#include "RetroTypes.hpp"
2015-08-22 23:42:29 -07:00
#include "IVParamObj.hpp"
2015-08-21 18:58:41 -07:00
#include "IObjectStore.hpp"
2015-08-22 23:42:29 -07:00
#include "IFactory.hpp"
2015-08-21 18:58:41 -07:00
2015-08-20 17:06:39 -07:00
namespace Retro
{
2015-08-21 18:58:41 -07:00
class IObjectStore;
class IObj;
class CObjectReference
{
friend class CToken;
2015-08-21 18:58:41 -07:00
u16 x0_refCount = 0;
u16 x2_lockCount = 0;
bool x3_loading = false; /* Rightmost bit of lockCount */
2015-08-21 18:58:41 -07:00
SObjectTag x4_objTag;
IObjectStore* xC_objectStore = nullptr;
IObj* x10_object = nullptr;
2015-08-21 18:58:41 -07:00
CVParamTransfer x14_params;
public:
CObjectReference(IObjectStore& objStore, std::unique_ptr<IObj>&& obj,
const SObjectTag& objTag, CVParamTransfer buildParams)
: x4_objTag(objTag), xC_objectStore(&objStore),
x10_object(obj.release()), x14_params(buildParams) {}
CObjectReference(std::unique_ptr<IObj>&& obj)
: x10_object(obj.release()) {}
2015-08-21 18:58:41 -07:00
bool IsLoading() const {return x3_loading;}
void Unlock()
{
--x2_lockCount;
if (x2_lockCount)
return;
if (x10_object && xC_objectStore)
Unload();
else if (IsLoading())
CancelLoad();
}
void Lock()
{
++x2_lockCount;
if (!x10_object && !x3_loading)
{
IFactory& fac = xC_objectStore->GetFactory();
fac.BuildAsync(x4_objTag, x14_params, &x10_object);
x3_loading = true;
}
}
u16 RemoveReference()
2015-08-22 23:42:29 -07:00
{
--x0_refCount;
if (x0_refCount == 0)
{
if (x10_object)
Unload();
if (IsLoading())
CancelLoad();
xC_objectStore->ObjectUnreferenced(x4_objTag);
}
return x0_refCount;
}
void CancelLoad()
{
if (xC_objectStore && IsLoading())
{
xC_objectStore->GetFactory().CancelBuild(x4_objTag);
x3_loading = false;
}
2015-08-22 23:42:29 -07:00
}
void Unload()
{
delete x10_object;
x10_object = nullptr;
x3_loading = false;
}
IObj* GetObject()
{
if (!x10_object)
{
IFactory& factory = xC_objectStore->GetFactory();
x10_object = factory.Build(x4_objTag, x14_params).release();
}
2015-08-22 23:42:29 -07:00
x3_loading = false;
return x10_object;
2015-08-22 23:42:29 -07:00
}
2016-02-06 23:25:34 -08:00
const SObjectTag& GetObjectTag() const
{
return x4_objTag;
}
~CObjectReference()
2015-08-22 23:42:29 -07:00
{
if (x10_object)
delete x10_object;
else if (x3_loading)
xC_objectStore->GetFactory().CancelBuild(x4_objTag);
2015-08-22 23:42:29 -07:00
}
2015-08-21 18:58:41 -07:00
};
2015-08-20 17:06:39 -07:00
class CToken
{
CObjectReference* x0_objRef = nullptr;
2015-08-21 18:58:41 -07:00
bool x4_lockHeld = false;
public:
2016-02-06 23:25:34 -08:00
operator bool() const {return x0_objRef != nullptr;}
2015-08-22 23:42:29 -07:00
void Unlock()
2015-08-21 18:58:41 -07:00
{
if (x0_objRef && x4_lockHeld)
2015-08-22 23:42:29 -07:00
{
x0_objRef->Unlock();
2015-08-22 23:42:29 -07:00
x4_lockHeld = false;
}
}
void Lock()
{
if (x0_objRef && !x4_lockHeld)
2015-08-22 23:42:29 -07:00
{
x0_objRef->Lock();
2015-08-22 23:42:29 -07:00
x4_lockHeld = true;
}
}
void RemoveRef()
{
if (x0_objRef && x0_objRef->RemoveReference() == 0)
{
delete x0_objRef;
x0_objRef = nullptr;
}
2015-08-21 18:58:41 -07:00
}
IObj* GetObj()
2015-08-22 23:42:29 -07:00
{
if (!x0_objRef)
return nullptr;
2015-08-22 23:42:29 -07:00
Lock();
return x0_objRef->GetObject();
2015-08-22 23:42:29 -07:00
}
CToken& operator=(const CToken& other)
2015-08-22 23:42:29 -07:00
{
Unlock();
RemoveRef();
x0_objRef = other.x0_objRef;
if (x0_objRef)
{
++x0_objRef->x0_refCount;
if (other.x4_lockHeld)
Lock();
}
return *this;
}
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;
}
2016-02-06 23:25:34 -08:00
CToken() = default;
CToken(const CToken& other)
: x0_objRef(other.x0_objRef)
{
++x0_objRef->x0_refCount;
}
CToken(CToken&& other)
: x0_objRef(other.x0_objRef), x4_lockHeld(other.x4_lockHeld)
{
other.x0_objRef = nullptr;
other.x4_lockHeld = false;
}
CToken(IObj* obj)
{
x0_objRef = new CObjectReference(std::unique_ptr<IObj>(obj));
++x0_objRef->x0_refCount;
Lock();
}
CToken(CObjectReference* obj)
{
x0_objRef = obj;
++x0_objRef->x0_refCount;
2015-08-22 23:42:29 -07:00
}
2016-02-06 23:25:34 -08:00
const SObjectTag* GetObjectTag() const
{
if (!x0_objRef)
return nullptr;
return &x0_objRef->GetObjectTag();
}
2015-08-22 23:42:29 -07:00
~CToken()
{
if (x0_objRef)
{
if (x4_lockHeld)
x0_objRef->Unlock();
RemoveRef();
}
2015-08-22 23:42:29 -07:00
}
2015-08-20 17:06:39 -07:00
};
template<class T>
class TToken : public CToken
2015-08-20 17:06:39 -07:00
{
public:
static std::unique_ptr<TObjOwnerDerivedFromIObj<T>> GetIObjObjectFor(std::unique_ptr<T>&& obj)
{
return TObjOwnerDerivedFromIObj<T>::GetNewDerivedObject(std::move(obj));
}
2016-02-06 23:25:34 -08:00
TToken() = default;
TToken(const CToken& other) : CToken(other) {}
TToken(T* obj)
: CToken(GetIObjObjectFor(std::unique_ptr<T>(obj))) {}
TToken& operator=(T* obj) {*this = CToken(GetIObjObjectFor(obj)); return this;}
T* GetObj() {return static_cast<TObjOwnerDerivedFromIObj<T>*>(CToken::GetObj())->GetObj();}
};
template<class T>
class TLockedToken : public TToken<T>
{
public:
TLockedToken(const CToken& other) : TToken<T>(other) {CToken::Lock();}
2015-08-20 17:06:39 -07:00
};
}
#endif // __RETRO_CTOKEN_HPP__