Bigfoot / Sonarqube (game) (push) Skipped
Bigfoot / Sonarqube (toolmode) (push) Skipped
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 12s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 11s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 9s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 10s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 15s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 9s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 10s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 11s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 10s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 9s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 10s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 11s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 10s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 9s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 9s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 11s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 12s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 10s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 9s
Bigfoot / Clang Format Checks (push) Failing after 12s
246 lines
5.2 KiB
C++
246 lines
5.2 KiB
C++
/*********************************************************************
|
|
* \file Reference.hpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date April 2026
|
|
*********************************************************************/
|
|
#ifndef BIGFOOT_ENGINE_ASSET_REFERENCE_HPP
|
|
#define BIGFOOT_ENGINE_ASSET_REFERENCE_HPP
|
|
#include <Engine/Asset/AssetConcepts.hpp>
|
|
#include <Engine/Asset/AssetLibrary.hpp>
|
|
|
|
#include <System/UUID/UUID.hpp>
|
|
|
|
#include <utility>
|
|
|
|
namespace Bigfoot
|
|
{
|
|
template<class ASSET>
|
|
AssetTypeID GetAssetTypeID();
|
|
|
|
template<>
|
|
AssetTypeID GetAssetTypeID<AssetBase>() = delete;
|
|
|
|
template<class DERIVED, class ASSET>
|
|
class ReferenceBase
|
|
{
|
|
public:
|
|
/**
|
|
* Get the UUID of the reference
|
|
*
|
|
* \return The UUID of the reference
|
|
*/
|
|
[[nodiscard]]
|
|
const UUID& GetUUID() const
|
|
{
|
|
return m_uuid;
|
|
}
|
|
|
|
/**
|
|
* Get the asset
|
|
*
|
|
* \return The asset, nullptr if it is not loaded
|
|
*/
|
|
[[nodiscard]]
|
|
const ASSET* Get() const
|
|
{
|
|
static_assert(AssetType<ASSET>, "ASSET must derive from AssetBase (and not be AssetBase itself)");
|
|
return m_handle ? static_cast<const ASSET*>(m_handle->GetAsset()) : nullptr;
|
|
}
|
|
|
|
/**
|
|
* Get the asset
|
|
*
|
|
* \return The asset, nullptr if it is not loaded
|
|
*/
|
|
[[nodiscard]]
|
|
ASSET* Get()
|
|
{
|
|
static_assert(AssetType<ASSET>, "ASSET must derive from AssetBase (and not be AssetBase itself)");
|
|
return m_handle ? static_cast<ASSET*>(m_handle->GetAsset()) : nullptr;
|
|
}
|
|
|
|
~ReferenceBase()
|
|
{
|
|
Release();
|
|
}
|
|
|
|
ReferenceBase& operator=(const ReferenceBase& p_reference)
|
|
{
|
|
if (this != &p_reference)
|
|
{
|
|
Release();
|
|
m_uuid = p_reference.m_uuid;
|
|
m_handle = p_reference.m_handle;
|
|
Acquire();
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
ReferenceBase& operator=(ReferenceBase&& p_reference) noexcept
|
|
{
|
|
if (this != &p_reference)
|
|
{
|
|
Release();
|
|
m_uuid = std::exchange(p_reference.m_uuid, UUID::NULL_UUID);
|
|
m_handle = std::exchange(p_reference.m_handle, nullptr);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]]
|
|
bool operator==(const ReferenceBase& p_reference) const
|
|
{
|
|
return m_uuid == p_reference.m_uuid;
|
|
}
|
|
|
|
private:
|
|
friend DERIVED;
|
|
|
|
ReferenceBase() = default;
|
|
|
|
ReferenceBase(const UUID& p_uuid):
|
|
m_uuid(p_uuid)
|
|
{
|
|
Acquire();
|
|
}
|
|
|
|
ReferenceBase(const ReferenceBase& p_reference):
|
|
m_uuid(p_reference.m_uuid),
|
|
m_handle(p_reference.m_handle)
|
|
{
|
|
Acquire();
|
|
}
|
|
|
|
ReferenceBase(ReferenceBase&& p_reference) noexcept:
|
|
m_uuid(std::exchange(p_reference.m_uuid, UUID::NULL_UUID)),
|
|
m_handle(std::exchange(p_reference.m_handle, nullptr))
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Get the associated handle container for the asset
|
|
* Register the reference inside the handle
|
|
*
|
|
*/
|
|
void Acquire()
|
|
{
|
|
if (!m_uuid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (AssetContainerBase* assetContainer = g_activeAssetLibrary->GetAssetContainer(GetAssetTypeID<ASSET>()))
|
|
{
|
|
if (!m_handle)
|
|
{
|
|
m_handle = assetContainer->AcquireContainerHandle(m_uuid);
|
|
}
|
|
DERIVED::IncrementCount(*m_handle);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Unregister the reference from the handle
|
|
*
|
|
*/
|
|
void Release()
|
|
{
|
|
if (m_handle)
|
|
{
|
|
DERIVED::DecrementCount(*m_handle);
|
|
m_handle = nullptr;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The UUID
|
|
*/
|
|
UUID m_uuid = UUID::NULL_UUID;
|
|
|
|
/**
|
|
* Handle for the asset
|
|
*/
|
|
ContainerHandle* m_handle = nullptr;
|
|
};
|
|
|
|
/*
|
|
* Owning reference
|
|
* Keeps the asset alive
|
|
*/
|
|
template<class ASSET>
|
|
class HardReference: public ReferenceBase<HardReference<ASSET>, ASSET>
|
|
{
|
|
using Base = ReferenceBase<HardReference<ASSET>, ASSET>;
|
|
|
|
public:
|
|
HardReference() = default;
|
|
|
|
HardReference(const UUID& p_uuid):
|
|
Base(p_uuid)
|
|
{
|
|
}
|
|
|
|
private:
|
|
/**
|
|
* Increment the hard ref count of the handle
|
|
*
|
|
* \param p_handle The handle for the asset
|
|
*/
|
|
static void IncrementCount(ContainerHandle& p_handle)
|
|
{
|
|
p_handle.IncrementHardRefCount();
|
|
}
|
|
|
|
/**
|
|
* Decrement the hard ref count of the handle
|
|
*
|
|
* \param p_handle The handle for the asset
|
|
*/
|
|
static void DecrementCount(ContainerHandle& p_handle)
|
|
{
|
|
p_handle.DecrementHardRefCount();
|
|
}
|
|
|
|
friend Base;
|
|
};
|
|
|
|
template<class ASSET>
|
|
class SoftReference: public ReferenceBase<SoftReference<ASSET>, ASSET>
|
|
{
|
|
using Base = ReferenceBase<SoftReference<ASSET>, ASSET>;
|
|
|
|
public:
|
|
SoftReference() = default;
|
|
|
|
SoftReference(const UUID& p_uuid):
|
|
Base(p_uuid)
|
|
{
|
|
}
|
|
|
|
private:
|
|
/**
|
|
* Increment the soft ref count of the handle
|
|
*
|
|
* \param p_handle The handle for the asset
|
|
*/
|
|
static void IncrementCount(ContainerHandle& p_handle)
|
|
{
|
|
p_handle.IncrementSoftRefCount();
|
|
}
|
|
|
|
/**
|
|
* Decrement the hard ref count of the handle
|
|
*
|
|
* \param p_handle The handle for the asset
|
|
*/
|
|
static void DecrementCount(ContainerHandle& p_handle)
|
|
{
|
|
p_handle.DecrementSoftRefCount();
|
|
}
|
|
|
|
friend Base;
|
|
};
|
|
} // namespace Bigfoot
|
|
#endif
|