Bomberman
Cache.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2021
3 ** Bomberman
4 ** File description:
5 ** Cache
6 */
7 
8 #pragma once
9 #include <memory>
10 #include <unordered_map>
11 #include <functional>
12 #include <utility>
13 #include <raylib.h>
14 #include "Exceptions/RayError.hpp"
15 #include <vector>
16 #include <algorithm>
17 #include <string>
18 #include <iostream>
19 
20 namespace RAY {
22  template<typename T>
23  class Cache {
24  public:
26  Cache(std::function<T(const char *)> dataLoader, std::function<void(T)> dataUnloader):
27  _dataLoader(dataLoader), _dataUnloader(dataUnloader)
28  {};
29 
31  ~Cache() = default;
32 
34  Cache(const Cache &) = default;
35 
37  Cache &operator=(const Cache &) = default;
38 
42  std::shared_ptr<T>fetch(const std::string &path, bool lonely = false)
43  {
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);
47 
48  if (!matchingDataVector.empty()) {
49  for (std::shared_ptr<T> &i: matchingDataVector) {
50  if (!lonely)
51  return i;
52  if (lonely && i.use_count() == 1)
53  return i;
54  }
55  }
56  matchingDataVector.push_back(std::shared_ptr<T>(
57  new T(this->_dataLoader(path.c_str())), [this](T *p) {
58  this->_dataUnloader(*p);
59  delete p;
60  }));
61  return matchingDataVector.back();
62  };
63  private:
65  std::function<T(const char *)> _dataLoader;
66 
68  std::function<void(T)> _dataUnloader;
69 
71  std::unordered_map<std::string, std::vector<std::shared_ptr<T>>> _cache;
72  };
73 
74  template<>
75  class Cache<::ModelAnimation> {
76  private:
78  std::function<::ModelAnimation *(const char *, int *)> _dataLoader;
79 
81  std::function<void(::ModelAnimation *, unsigned int)> _dataUnloader;
82 
83  typedef struct {
84  std::shared_ptr<::ModelAnimation> animations;
86  } AnimationsHolder;
87 
89  std::unordered_map<std::string, AnimationsHolder> _cache;
90  public:
91  Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
92  _dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
93  {};
94  std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
95  {
96  if (this->_cache.find(path) != this->_cache.end()) {
97  *counter = this->_cache[path].animationsCount;
98  } else {
99  ::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter);
100  int animCount = *counter;
101  AnimationsHolder holder = {std::shared_ptr<::ModelAnimation>(
102  animations, [this, animCount](::ModelAnimation *p) {
103  this->_dataUnloader(p, animCount);
104  }),animCount};
105 
106  this->_cache.emplace(path, holder);
107  }
108  return this->_cache[path].animations;
109  };
110  };
111  template<>
112  class Cache<::Shader>
113  {
114  public:
115  Cache(std::function<::Shader(const char *, const char *)> dataLoader,
116  std::function<void(::Shader)> dataUnloader) :
117  _dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
118  {};
119 
120  std::shared_ptr<::Shader> fetch(const std::string &vertexFile, const std::string &fragmentFile, bool lonely = false)
121  {
122  const std::string index = vertexFile + fragmentFile;
123 
124  if (vertexFile.empty() && fragmentFile.empty()) {
126  }
127  if (!this->_cache.contains(index)) {
128  this->_cache.emplace(index, std::vector<std::shared_ptr<::Shader>>());
129  }
130  std::vector<std::shared_ptr<::Shader>> &matchingDataVector = this->_cache.at(index);
131 
132  if (!matchingDataVector.empty()) {
133  for (std::shared_ptr<::Shader> &i: matchingDataVector) {
134  if (!lonely)
135  return i;
136  if (lonely && i.use_count() == 1)
137  return i;
138  }
139  }
140 
141  matchingDataVector.emplace_back(std::shared_ptr<::Shader>(
142  new ::Shader(
143  this->_dataLoader(vertexFile.empty() ? nullptr : vertexFile.c_str(), fragmentFile.c_str())),
144  [this](::Shader *p) {
145  this->_dataUnloader(*p);
146  }));
147  return matchingDataVector.back();
148  };
149  private:
151  std::function<::Shader(const char *, const char *)> _dataLoader;
152 
154  std::function<void(::Shader)> _dataUnloader;
155 
157  std::unordered_map<std::string, std::vector<std::shared_ptr<::Shader>>> _cache;
158  };
159 }
RAY::Cache<::ModelAnimation >::fetch
std::shared_ptr<::ModelAnimation > fetch(const std::string &path, int *counter)
Definition: Cache.hpp:94
RAY::Cache<::ModelAnimation >::AnimationsHolder::animations
std::shared_ptr<::ModelAnimation > animations
Definition: Cache.hpp:84
RAY::Cache<::ModelAnimation >::AnimationsHolder::animationsCount
int animationsCount
Definition: Cache.hpp:85
RAY::ModelAnimation
A Holder for Model Animations.
Definition: ModelAnimation.hpp:16
RAY::Cache<::ModelAnimation >::_dataUnloader
std::function< void(::ModelAnimation *, unsigned int)> _dataUnloader
function to call when the ray data will be unloaded
Definition: Cache.hpp:81
RAY::Cache::~Cache
~Cache()=default
Default destructor, will destroy ray's data.
RAY::Cache<::Shader >::fetch
std::shared_ptr<::Shader > fetch(const std::string &vertexFile, const std::string &fragmentFile, bool lonely=false)
Definition: Cache.hpp:120
RAY::Cache<::Shader >::Cache
Cache(std::function<::Shader(const char *, const char *)> dataLoader, std::function< void(::Shader)> dataUnloader)
Definition: Cache.hpp:115
RAY::Exception::WrongInputError
exception used when an non-supported operation is done
Definition: RayError.hpp:65
RAY
Definition: IAudio.hpp:12
RAY::Cache<::Shader >::_dataUnloader
std::function< void(::Shader)> _dataUnloader
function to call when the ray data will be unloaded
Definition: Cache.hpp:154
RAY::Cache::fetch
std::shared_ptr< T > fetch(const std::string &path, bool lonely=false)
Definition: Cache.hpp:42
RAY::Cache::operator=
Cache & operator=(const Cache &)=default
a cache is assignable
RAY::Cache<::Shader >::_cache
std::unordered_map< std::string, std::vector< std::shared_ptr<::Shader > > > _cache
map storing shared ptr of caches
Definition: Cache.hpp:157
RAY::Cache::Cache
Cache(std::function< T(const char *)> dataLoader, std::function< void(T)> dataUnloader)
Definition: Cache.hpp:26
RAY::Cache::_cache
std::unordered_map< std::string, std::vector< std::shared_ptr< T > > > _cache
map storing shared ptr of caches
Definition: Cache.hpp:71
std
Definition: View.hpp:210
RAY::Cache
A templated class used to cache ressources, indexed with a string.
Definition: Cache.hpp:23
RAY::Cache<::ModelAnimation >::_cache
std::unordered_map< std::string, AnimationsHolder > _cache
map storing shared ptr of caches
Definition: Cache.hpp:89
RAY::Cache<::ModelAnimation >::Cache
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function< void(::ModelAnimation *, unsigned int)>dataUnloader)
Definition: Cache.hpp:91
RAY::Cache::_dataUnloader
std::function< void(T)> _dataUnloader
function to call when the ray data will be unloaded
Definition: Cache.hpp:68
RAY::Shader
Definition: Shaders.hpp:16
RAY::Cache::_dataLoader
std::function< T(const char *)> _dataLoader
function to call to load data
Definition: Cache.hpp:62
RayError.hpp