[nostalgia/core] Add gc function to AssetManager
This commit is contained in:
parent
e3a48d0045
commit
9fd14b0744
@ -15,12 +15,15 @@
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
class AssetManager;
|
||||
|
||||
template<typename T>
|
||||
class AssetRef;
|
||||
|
||||
template<typename T>
|
||||
struct AssetContainer {
|
||||
|
||||
friend AssetManager;
|
||||
friend AssetRef<T>;
|
||||
|
||||
private:
|
||||
@ -49,6 +52,10 @@ struct AssetContainer {
|
||||
--m_references;
|
||||
}
|
||||
|
||||
constexpr int references() const noexcept {
|
||||
return m_references;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@ -128,6 +135,8 @@ class AssetManager {
|
||||
class AssetTypeManagerBase {
|
||||
public:
|
||||
virtual ~AssetTypeManagerBase() = default;
|
||||
|
||||
virtual void gc() noexcept = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@ -146,6 +155,15 @@ class AssetManager {
|
||||
auto &p = m_cache[path] = ox::make_unique<AssetContainer<T>>(obj);
|
||||
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;
|
||||
@ -178,6 +196,13 @@ class AssetManager {
|
||||
auto m = getTypeManager<T>();
|
||||
return m->setAsset(path, obj);
|
||||
}
|
||||
|
||||
void gc() noexcept {
|
||||
for (const auto &amk : m_assetTypeManagers.keys()) {
|
||||
auto &am = m_assetTypeManagers[amk];
|
||||
am->gc();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user