diff --git a/Source/Engine/Content/Asset.cpp b/Source/Engine/Content/Asset.cpp index 7fcd2f405..d2e150ac5 100644 --- a/Source/Engine/Content/Asset.cpp +++ b/Source/Engine/Content/Asset.cpp @@ -13,6 +13,91 @@ #include "Engine/Threading/ConcurrentTaskQueue.h" #include +AssetReferenceBase::~AssetReferenceBase() +{ + if (_asset) + { + _asset->OnLoaded.Unbind(this); + _asset->OnUnloaded.Unbind(this); + _asset->RemoveReference(); + _asset = nullptr; + } +} + +String AssetReferenceBase::ToString() const +{ + return _asset ? _asset->ToString() : TEXT(""); +} + +void AssetReferenceBase::OnSet(Asset* asset) +{ + auto e = _asset; + if (e != asset) + { + if (e) + { + e->OnLoaded.Unbind(this); + e->OnUnloaded.Unbind(this); + e->RemoveReference(); + } + _asset = e = asset; + if (e) + { + e->AddReference(); + e->OnLoaded.Bind(this); + e->OnUnloaded.Bind(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(this); +} + +String WeakAssetReferenceBase::ToString() const +{ + return _asset ? _asset->ToString() : TEXT(""); +} + +void WeakAssetReferenceBase::OnSet(Asset* asset) +{ + auto e = _asset; + if (e != asset) + { + if (e) + e->OnUnloaded.Unbind(this); + _asset = e = asset; + if (e) + e->OnUnloaded.Bind(this); + } +} + +void WeakAssetReferenceBase::OnUnloaded(Asset* asset) +{ + ASSERT(_asset == asset); + Unload(); + asset->OnUnloaded.Unbind(this); + asset = nullptr; +} + Asset::Asset(const SpawnParams& params, const AssetInfo* info) : ManagedScriptingObject(params) , _refCount(0) diff --git a/Source/Engine/Content/AssetReference.h b/Source/Engine/Content/AssetReference.h index 0956e82be..68adf4a25 100644 --- a/Source/Engine/Content/AssetReference.h +++ b/Source/Engine/Content/AssetReference.h @@ -15,7 +15,7 @@ public: protected: - Asset* _asset; + Asset* _asset = nullptr; public: @@ -39,31 +39,18 @@ public: /// /// Initializes a new instance of the class. /// - AssetReferenceBase() - : _asset(nullptr) - { - } + AssetReferenceBase() = default; /// /// Finalizes an instance of the class. /// - ~AssetReferenceBase() - { - if (_asset) - { - _asset->OnLoaded.Unbind(this); - _asset->OnUnloaded.Unbind(this); - _asset->RemoveReference(); - _asset = nullptr; - } - } + ~AssetReferenceBase(); public: /// /// Gets the asset ID or Guid::Empty if not set. /// - /// The asset ID or Guid::Empty if not set. FORCE_INLINE Guid GetID() const { return _asset ? _asset->GetID() : Guid::Empty; @@ -72,7 +59,6 @@ public: /// /// Gets managed instance object (or null if no asset set). /// - /// Mono managed object FORCE_INLINE MonoObject* GetManagedInstance() const { return _asset ? _asset->GetOrCreateManagedInstance() : nullptr; @@ -81,55 +67,13 @@ public: /// /// Gets the asset property value as string. /// - /// The string. - String ToString() const - { - static String NullStr = TEXT(""); - return _asset ? _asset->ToString() : NullStr; - } + String ToString() const; protected: - void OnSet(Asset* asset) - { - auto e = _asset; - if (e != asset) - { - if (e) - { - e->OnLoaded.Unbind(this); - e->OnUnloaded.Unbind(this); - e->RemoveReference(); - } - _asset = e = asset; - if (e) - { - e->AddReference(); - e->OnLoaded.Bind(this); - e->OnUnloaded.Bind(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); }; /// @@ -220,7 +164,6 @@ public: /// /// Implicit conversion to the bool. /// - /// The asset. FORCE_INLINE operator T*() const { return (T*)_asset; @@ -229,7 +172,6 @@ public: /// /// Implicit conversion to the asset. /// - /// True if asset has been set, otherwise false. FORCE_INLINE operator bool() const { return _asset != nullptr; @@ -238,7 +180,6 @@ public: /// /// Implicit conversion to the asset. /// - /// The asset. FORCE_INLINE T* operator->() const { return (T*)_asset; @@ -247,7 +188,6 @@ public: /// /// Gets the asset. /// - /// The asset. FORCE_INLINE T* Get() const { return (T*)_asset; @@ -256,7 +196,6 @@ public: /// /// Gets the asset as a given type (static cast). /// - /// The asset. template FORCE_INLINE U* As() const { diff --git a/Source/Engine/Content/WeakAssetReference.h b/Source/Engine/Content/WeakAssetReference.h index 49401bf25..7e4202fe4 100644 --- a/Source/Engine/Content/WeakAssetReference.h +++ b/Source/Engine/Content/WeakAssetReference.h @@ -15,7 +15,7 @@ public: protected: - Asset* _asset; + Asset* _asset = nullptr; public: @@ -29,29 +29,18 @@ public: /// /// Initializes a new instance of the class. /// - WeakAssetReferenceBase() - : _asset(nullptr) - { - } + WeakAssetReferenceBase() = default; /// /// Finalizes an instance of the class. /// - ~WeakAssetReferenceBase() - { - if (_asset) - { - _asset->OnUnloaded.Unbind(this); - _asset = nullptr; - } - } + ~WeakAssetReferenceBase(); public: /// /// Gets the asset ID or Guid::Empty if not set. /// - /// The asset ID or Guid::Empty if not set. FORCE_INLINE Guid GetID() const { return _asset ? _asset->GetID() : Guid::Empty; @@ -60,7 +49,6 @@ public: /// /// Gets managed instance object (or null if no asset set). /// - /// Mono managed object FORCE_INLINE MonoObject* GetManagedInstance() const { return _asset ? _asset->GetOrCreateManagedInstance() : nullptr; @@ -69,35 +57,12 @@ public: /// /// Gets the asset property value as string. /// - /// The string. - String ToString() const - { - static String NullStr = TEXT(""); - return _asset ? _asset->ToString() : NullStr; - } + String ToString() const; protected: - void OnSet(Asset* asset) - { - auto e = _asset; - if (e != asset) - { - if (e) - e->OnUnloaded.Unbind(this); - _asset = e = asset; - if (e) - e->OnUnloaded.Bind(this); - } - } - - void OnAssetUnloaded(Asset* asset) - { - ASSERT(_asset == asset); - Unload(); - asset->OnUnloaded.Unbind(this); - asset = nullptr; - } + void OnSet(Asset* asset); + void OnUnloaded(Asset* asset); }; /// @@ -175,7 +140,6 @@ public: /// /// Implicit conversion to the bool. /// - /// The asset. FORCE_INLINE operator T*() const { return (T*)_asset; @@ -184,7 +148,6 @@ public: /// /// Implicit conversion to the asset. /// - /// True if asset has been set, otherwise false. FORCE_INLINE operator bool() const { return _asset != nullptr; @@ -193,7 +156,6 @@ public: /// /// Implicit conversion to the asset. /// - /// The asset. FORCE_INLINE T* operator->() const { return (T*)_asset; @@ -202,7 +164,6 @@ public: /// /// Gets the asset. /// - /// The asset. FORCE_INLINE T* Get() const { return (T*)_asset; @@ -211,7 +172,6 @@ public: /// /// Gets the asset as a given type (static cast). /// - /// The asset. template FORCE_INLINE U* As() const { diff --git a/Source/Engine/Scripting/ScriptingObject.cpp b/Source/Engine/Scripting/ScriptingObject.cpp index dc9e27865..5208b1560 100644 --- a/Source/Engine/Scripting/ScriptingObject.cpp +++ b/Source/Engine/Scripting/ScriptingObject.cpp @@ -53,6 +53,17 @@ MonoObject* ScriptingObject::GetManagedInstance() const return _gcHandle ? mono_gchandle_get_target(_gcHandle) : nullptr; } +MonoObject* ScriptingObject::GetOrCreateManagedInstance() const +{ + MonoObject* managedInstance = GetManagedInstance(); + if (!managedInstance) + { + const_cast(this)->CreateManaged(); + managedInstance = GetManagedInstance(); + } + return managedInstance; +} + MClass* ScriptingObject::GetClass() const { return _type ? _type.GetType().ManagedClass : nullptr; diff --git a/Source/Engine/Scripting/ScriptingObject.h b/Source/Engine/Scripting/ScriptingObject.h index 9eb375fc5..1d2365d64 100644 --- a/Source/Engine/Scripting/ScriptingObject.h +++ b/Source/Engine/Scripting/ScriptingObject.h @@ -68,22 +68,11 @@ public: /// /// Gets the managed instance object or creates it if missing. /// - /// The Mono managed object. - FORCE_INLINE MonoObject* GetOrCreateManagedInstance() const - { - MonoObject* managedInstance = GetManagedInstance(); - if (!managedInstance) - { - const_cast(this)->CreateManaged(); - managedInstance = GetManagedInstance(); - } - return managedInstance; - } + MonoObject* GetOrCreateManagedInstance() const; /// /// Determines whether managed instance is alive. /// - /// True if managed object has been created and exists, otherwise false. FORCE_INLINE bool HasManagedInstance() const { return GetManagedInstance() != nullptr; @@ -92,7 +81,6 @@ public: /// /// Gets the unique object ID. /// - /// The unique object ID. FORCE_INLINE const Guid& GetID() const { return _id; @@ -101,7 +89,6 @@ public: /// /// Gets the scripting type handle of this object. /// - /// The scripting type handle. FORCE_INLINE const ScriptingTypeHandle& GetTypeHandle() const { return _type; @@ -110,7 +97,6 @@ public: /// /// Gets the scripting type of this object. /// - /// The scripting type. FORCE_INLINE const ScriptingType& GetType() const { return _type.GetType(); @@ -119,7 +105,6 @@ public: /// /// Gets the type class of this object. /// - /// The Mono class. MClass* GetClass() const; public: