Cleanup asset references code

This commit is contained in:
Wojtek Figat
2021-06-10 11:01:35 +02:00
parent 9fd62cf9aa
commit 907f289ea7
5 changed files with 110 additions and 130 deletions

View File

@@ -13,6 +13,91 @@
#include "Engine/Threading/ConcurrentTaskQueue.h"
#include <ThirdParty/mono-2.0/mono/metadata/mono-gc.h>
AssetReferenceBase::~AssetReferenceBase()
{
if (_asset)
{
_asset->OnLoaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnLoaded>(this);
_asset->OnUnloaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnUnloaded>(this);
_asset->RemoveReference();
_asset = nullptr;
}
}
String AssetReferenceBase::ToString() const
{
return _asset ? _asset->ToString() : TEXT("<null>");
}
void AssetReferenceBase::OnSet(Asset* asset)
{
auto e = _asset;
if (e != asset)
{
if (e)
{
e->OnLoaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnLoaded>(this);
e->OnUnloaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnUnloaded>(this);
e->RemoveReference();
}
_asset = e = asset;
if (e)
{
e->AddReference();
e->OnLoaded.Bind<AssetReferenceBase, &AssetReferenceBase::OnLoaded>(this);
e->OnUnloaded.Bind<AssetReferenceBase, &AssetReferenceBase::OnUnloaded>(this);
}
Changed();
if (e && e->IsLoaded())
Loaded();
}
}
void AssetReferenceBase::OnLoaded(Asset* asset)
{
ASSERT(_asset == asset);
Loaded();
}
void AssetReferenceBase::OnUnloaded(Asset* asset)
{
ASSERT(_asset == asset);
Unload();
OnSet(nullptr);
}
WeakAssetReferenceBase::~WeakAssetReferenceBase()
{
if (_asset)
_asset->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnUnloaded>(this);
}
String WeakAssetReferenceBase::ToString() const
{
return _asset ? _asset->ToString() : TEXT("<null>");
}
void WeakAssetReferenceBase::OnSet(Asset* asset)
{
auto e = _asset;
if (e != asset)
{
if (e)
e->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnUnloaded>(this);
_asset = e = asset;
if (e)
e->OnUnloaded.Bind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnUnloaded>(this);
}
}
void WeakAssetReferenceBase::OnUnloaded(Asset* asset)
{
ASSERT(_asset == asset);
Unload();
asset->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnUnloaded>(this);
asset = nullptr;
}
Asset::Asset(const SpawnParams& params, const AssetInfo* info)
: ManagedScriptingObject(params)
, _refCount(0)

View File

@@ -15,7 +15,7 @@ public:
protected:
Asset* _asset;
Asset* _asset = nullptr;
public:
@@ -39,31 +39,18 @@ public:
/// <summary>
/// Initializes a new instance of the <see cref="AssetReferenceBase"/> class.
/// </summary>
AssetReferenceBase()
: _asset(nullptr)
{
}
AssetReferenceBase() = default;
/// <summary>
/// Finalizes an instance of the <see cref="AssetReferenceBase"/> class.
/// </summary>
~AssetReferenceBase()
{
if (_asset)
{
_asset->OnLoaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnAssetLoaded>(this);
_asset->OnUnloaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnAssetUnloaded>(this);
_asset->RemoveReference();
_asset = nullptr;
}
}
~AssetReferenceBase();
public:
/// <summary>
/// Gets the asset ID or Guid::Empty if not set.
/// </summary>
/// <returns>The asset ID or Guid::Empty if not set.</returns>
FORCE_INLINE Guid GetID() const
{
return _asset ? _asset->GetID() : Guid::Empty;
@@ -72,7 +59,6 @@ public:
/// <summary>
/// Gets managed instance object (or null if no asset set).
/// </summary>
/// <returns>Mono managed object</returns>
FORCE_INLINE MonoObject* GetManagedInstance() const
{
return _asset ? _asset->GetOrCreateManagedInstance() : nullptr;
@@ -81,55 +67,13 @@ public:
/// <summary>
/// Gets the asset property value as string.
/// </summary>
/// <returns>The string.</returns>
String ToString() const
{
static String NullStr = TEXT("<null>");
return _asset ? _asset->ToString() : NullStr;
}
String ToString() const;
protected:
void OnSet(Asset* asset)
{
auto e = _asset;
if (e != asset)
{
if (e)
{
e->OnLoaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnAssetLoaded>(this);
e->OnUnloaded.Unbind<AssetReferenceBase, &AssetReferenceBase::OnAssetUnloaded>(this);
e->RemoveReference();
}
_asset = e = asset;
if (e)
{
e->AddReference();
e->OnLoaded.Bind<AssetReferenceBase, &AssetReferenceBase::OnAssetLoaded>(this);
e->OnUnloaded.Bind<AssetReferenceBase, &AssetReferenceBase::OnAssetUnloaded>(this);
}
Changed();
if (e && e->IsLoaded())
Loaded();
}
}
void OnAssetLoaded(Asset* asset)
{
if (_asset == asset)
{
Loaded();
}
}
void OnAssetUnloaded(Asset* asset)
{
if (_asset == asset)
{
Unload();
OnSet(nullptr);
}
}
void OnSet(Asset* asset);
void OnLoaded(Asset* asset);
void OnUnloaded(Asset* asset);
};
/// <summary>
@@ -220,7 +164,6 @@ public:
/// <summary>
/// Implicit conversion to the bool.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE operator T*() const
{
return (T*)_asset;
@@ -229,7 +172,6 @@ public:
/// <summary>
/// Implicit conversion to the asset.
/// </summary>
/// <returns>True if asset has been set, otherwise false.</returns>
FORCE_INLINE operator bool() const
{
return _asset != nullptr;
@@ -238,7 +180,6 @@ public:
/// <summary>
/// Implicit conversion to the asset.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE T* operator->() const
{
return (T*)_asset;
@@ -247,7 +188,6 @@ public:
/// <summary>
/// Gets the asset.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE T* Get() const
{
return (T*)_asset;
@@ -256,7 +196,6 @@ public:
/// <summary>
/// Gets the asset as a given type (static cast).
/// </summary>
/// <returns>The asset.</returns>
template<typename U>
FORCE_INLINE U* As() const
{

View File

@@ -15,7 +15,7 @@ public:
protected:
Asset* _asset;
Asset* _asset = nullptr;
public:
@@ -29,29 +29,18 @@ public:
/// <summary>
/// Initializes a new instance of the <see cref="WeakAssetReferenceBase"/> class.
/// </summary>
WeakAssetReferenceBase()
: _asset(nullptr)
{
}
WeakAssetReferenceBase() = default;
/// <summary>
/// Finalizes an instance of the <see cref="WeakAssetReferenceBase"/> class.
/// </summary>
~WeakAssetReferenceBase()
{
if (_asset)
{
_asset->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnAssetUnloaded>(this);
_asset = nullptr;
}
}
~WeakAssetReferenceBase();
public:
/// <summary>
/// Gets the asset ID or Guid::Empty if not set.
/// </summary>
/// <returns>The asset ID or Guid::Empty if not set.</returns>
FORCE_INLINE Guid GetID() const
{
return _asset ? _asset->GetID() : Guid::Empty;
@@ -60,7 +49,6 @@ public:
/// <summary>
/// Gets managed instance object (or null if no asset set).
/// </summary>
/// <returns>Mono managed object</returns>
FORCE_INLINE MonoObject* GetManagedInstance() const
{
return _asset ? _asset->GetOrCreateManagedInstance() : nullptr;
@@ -69,35 +57,12 @@ public:
/// <summary>
/// Gets the asset property value as string.
/// </summary>
/// <returns>The string.</returns>
String ToString() const
{
static String NullStr = TEXT("<null>");
return _asset ? _asset->ToString() : NullStr;
}
String ToString() const;
protected:
void OnSet(Asset* asset)
{
auto e = _asset;
if (e != asset)
{
if (e)
e->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnAssetUnloaded>(this);
_asset = e = asset;
if (e)
e->OnUnloaded.Bind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnAssetUnloaded>(this);
}
}
void OnAssetUnloaded(Asset* asset)
{
ASSERT(_asset == asset);
Unload();
asset->OnUnloaded.Unbind<WeakAssetReferenceBase, &WeakAssetReferenceBase::OnAssetUnloaded>(this);
asset = nullptr;
}
void OnSet(Asset* asset);
void OnUnloaded(Asset* asset);
};
/// <summary>
@@ -175,7 +140,6 @@ public:
/// <summary>
/// Implicit conversion to the bool.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE operator T*() const
{
return (T*)_asset;
@@ -184,7 +148,6 @@ public:
/// <summary>
/// Implicit conversion to the asset.
/// </summary>
/// <returns>True if asset has been set, otherwise false.</returns>
FORCE_INLINE operator bool() const
{
return _asset != nullptr;
@@ -193,7 +156,6 @@ public:
/// <summary>
/// Implicit conversion to the asset.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE T* operator->() const
{
return (T*)_asset;
@@ -202,7 +164,6 @@ public:
/// <summary>
/// Gets the asset.
/// </summary>
/// <returns>The asset.</returns>
FORCE_INLINE T* Get() const
{
return (T*)_asset;
@@ -211,7 +172,6 @@ public:
/// <summary>
/// Gets the asset as a given type (static cast).
/// </summary>
/// <returns>The asset.</returns>
template<typename U>
FORCE_INLINE U* As() const
{