diff --git a/src/nostalgia/core/assetmanager.hpp b/src/nostalgia/core/assetmanager.hpp index 9599d275..be15acdf 100644 --- a/src/nostalgia/core/assetmanager.hpp +++ b/src/nostalgia/core/assetmanager.hpp @@ -15,12 +15,15 @@ namespace nostalgia::core { +class AssetManager; + template class AssetRef; template struct AssetContainer { + friend AssetManager; friend AssetRef; private: @@ -49,6 +52,10 @@ struct AssetContainer { --m_references; } + constexpr int references() const noexcept { + return m_references; + } + }; template @@ -128,6 +135,8 @@ class AssetManager { class AssetTypeManagerBase { public: virtual ~AssetTypeManagerBase() = default; + + virtual void gc() noexcept = 0; }; template @@ -146,6 +155,15 @@ class AssetManager { auto &p = m_cache[path] = ox::make_unique>(obj); return AssetRef(p.get()); } + + void gc() noexcept final { + for (const auto &ack : m_cache.keys()) { + auto &ac = m_cache[ack]; + if (ac->references()) { + m_cache.erase(ack); + } + } + } }; ox::HashMap> m_assetTypeManagers; @@ -178,6 +196,13 @@ class AssetManager { auto m = getTypeManager(); return m->setAsset(path, obj); } + + void gc() noexcept { + for (const auto &amk : m_assetTypeManagers.keys()) { + auto &am = m_assetTypeManagers[amk]; + am->gc(); + } + } }; } \ No newline at end of file