LeviLamina
Loading...
Searching...
No Matches
ResourceManager.h
1#pragma once
2#include "mc/_HeaderOutputPredefine.h"
3
4namespace cg {
5
6template <
7 typename ResourceType_t,
8 typename ResourceLocation_t,
9 template <typename...> class Container = std::unordered_map>
11public:
12 using ResourceType = ResourceType_t;
13 using ResourceLocation = ResourceLocation_t;
14 using ResourceHandle = ResourceType;
15
16protected:
18
19public:
20 ResourceType& operator[](const ResourceLocation& loc) { return mContainer[loc]; }
21
22 ResourceType& addResource(const ResourceLocation& loc, ResourceType res) {
23 auto [it, inserted] = mContainer.try_emplace(loc, std::move(res));
24 if (!inserted) it->second = std::move(res);
25 return it->second;
26 }
27
28 void clear() { mContainer.clear(); }
29
30 ResourceHandle getResource(const ResourceLocation& loc) const {
31 auto it = mContainer.find(loc);
32 if (it == mContainer.end()) return nullptr;
33 return it->second;
34 }
35
36 bool removeResource(const ResourceLocation& loc) { return mContainer.erase(loc) > 0; }
37
38 bool extract(const ResourceLocation& loc, ResourceType& out) {
39 auto it = mContainer.find(loc);
40 if (it == mContainer.end()) return false;
41 out = std::move(it->second);
42 mContainer.erase(it);
43 return true;
44 }
45};
46
47} // namespace cg
Definition Container.h:33
Definition ResourceManager.h:10