10 #include <unordered_map>
26 Cache(std::function<T(
const char *)> dataLoader, std::function<
void(T)> dataUnloader):
42 std::shared_ptr<T>
fetch(
const std::string &path,
bool lonely =
false)
44 if (!this->
_cache.contains(path))
45 this->
_cache.emplace(path, std::vector<std::shared_ptr<T>>());
46 std::vector<std::shared_ptr<T>> &matchingDataVector = this->
_cache.at(path);
48 if (!matchingDataVector.empty()) {
49 for (std::shared_ptr<T> &i: matchingDataVector) {
52 if (lonely && i.use_count() == 1)
56 matchingDataVector.push_back(std::shared_ptr<T>(
57 new T(this->
_dataLoader(path.c_str())), [
this](T *p) {
58 this->_dataUnloader(*p);
61 return matchingDataVector.back();
71 std::unordered_map<std::string, std::vector<std::shared_ptr<T>>>
_cache;
89 std::unordered_map<std::string, AnimationsHolder>
_cache;
94 std::shared_ptr<::ModelAnimation>
fetch(
const std::string &path,
int *counter)
96 if (this->_cache.find(path) != this->_cache.end()) {
97 *counter = this->_cache[path].animationsCount;
100 int animCount = *counter;
101 AnimationsHolder holder = {std::shared_ptr<::ModelAnimation>(
106 this->_cache.emplace(path, holder);
108 return this->_cache[path].animations;
115 Cache(std::function<::
Shader(
const char *,
const char *)> dataLoader,
116 std::function<
void(::
Shader)> dataUnloader) :
120 std::shared_ptr<::Shader>
fetch(
const std::string &vertexFile,
const std::string &fragmentFile,
bool lonely =
false)
122 const std::string index = vertexFile + fragmentFile;
124 if (vertexFile.empty() && fragmentFile.empty()) {
127 if (!this->_cache.contains(index)) {
128 this->_cache.emplace(index, std::vector<std::shared_ptr<::Shader>>());
130 std::vector<std::shared_ptr<::Shader>> &matchingDataVector = this->_cache.at(index);
132 if (!matchingDataVector.empty()) {
133 for (std::shared_ptr<::Shader> &i: matchingDataVector) {
136 if (lonely && i.use_count() == 1)
141 matchingDataVector.emplace_back(std::shared_ptr<::Shader>(
143 this->
_dataLoader(vertexFile.empty() ?
nullptr : vertexFile.c_str(), fragmentFile.c_str())),
145 this->_dataUnloader(*p);
147 return matchingDataVector.back();
157 std::unordered_map<std::string, std::vector<std::shared_ptr<::Shader>>>
_cache;