[nostalgia/core] Add gc function to AssetManager

This commit is contained in:
Gary Talent 2021-11-01 18:51:57 -05:00
parent e3a48d0045
commit 9fd14b0744

View File

@ -15,12 +15,15 @@
namespace nostalgia::core { namespace nostalgia::core {
class AssetManager;
template<typename T> template<typename T>
class AssetRef; class AssetRef;
template<typename T> template<typename T>
struct AssetContainer { struct AssetContainer {
friend AssetManager;
friend AssetRef<T>; friend AssetRef<T>;
private: private:
@ -49,6 +52,10 @@ struct AssetContainer {
--m_references; --m_references;
} }
constexpr int references() const noexcept {
return m_references;
}
}; };
template<typename T> template<typename T>
@ -128,6 +135,8 @@ class AssetManager {
class AssetTypeManagerBase { class AssetTypeManagerBase {
public: public:
virtual ~AssetTypeManagerBase() = default; virtual ~AssetTypeManagerBase() = default;
virtual void gc() noexcept = 0;
}; };
template<typename T> template<typename T>
@ -146,6 +155,15 @@ class AssetManager {
auto &p = m_cache[path] = ox::make_unique<AssetContainer<T>>(obj); auto &p = m_cache[path] = ox::make_unique<AssetContainer<T>>(obj);
return AssetRef<T>(p.get()); return AssetRef<T>(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<ox::String, ox::UniquePtr<AssetTypeManagerBase>> m_assetTypeManagers; ox::HashMap<ox::String, ox::UniquePtr<AssetTypeManagerBase>> m_assetTypeManagers;
@ -178,6 +196,13 @@ class AssetManager {
auto m = getTypeManager<T>(); auto m = getTypeManager<T>();
return m->setAsset(path, obj); return m->setAsset(path, obj);
} }
void gc() noexcept {
for (const auto &amk : m_assetTypeManagers.keys()) {
auto &am = m_assetTypeManagers[amk];
am->gc();
}
}
}; };
} }