Hummingbird Framework
 All Classes Functions Variables Modules Pages
ResourceManager.h
1 #ifndef HB_RESOURCE_MANAGER_H
2 #define HB_RESOURCE_MANAGER_H
3 #include <unordered_map>
4 #include "Log.h"
5 
6 namespace hb
7 {
17  template <typename ManagerType, typename ResourceType, typename ResourceId, typename Hash = std::hash<ResourceId>>
19  {
20  public:
25  static ManagerType* instance()
26  {
27  if (s_instance == nullptr)
28  s_instance = new ManagerType();
29  return s_instance;
30  }
35  {
36  m_resource_count = 0;
37  }
51  int addResource(const ResourceId& resource_id, const ResourceType& resource)
52  {
53  int ret = -1;
54  if (isLoaded(resource_id))
55  {
56  ret = m_id_table.find(resource_id)->second;
57  m_info_table.find(ret)->second.count++;
58  }
59  else
60  {
61  ret = m_resource_count++;
62  ResourceInfo ti;
63  ti.id = ret;
64  ti.count = 1;
65  ti.it = m_id_table.insert(std::pair<ResourceId, int>(resource_id, ret)).first;
66  ti.data = resource;
67  m_info_table.insert(std::pair<int, ResourceInfo>(ret, ti));
68  }
69  hb_assert(m_id_table.size() == m_info_table.size(), "Resource management broke bad");
70  return ret;
71  }
79  virtual void release(int id)
80  {
81  auto i = m_info_table.find(id);
82  if (i == m_info_table.end())
83  return;
84  ResourceInfo& ti = i->second;
85  if (--ti.count == 0)
86  {
87  m_id_table.erase(ti.it);
88  m_info_table.erase(id);
89  }
90  hb_assert(m_id_table.size() == m_info_table.size(), "Resource management broke bad");
91  }
98  void releaseAll(int id)
99  {
100  auto i = m_info_table.find(id);
101  if (i == m_info_table.end())
102  return;
103  ResourceInfo& ti = i->second;
104  m_id_table.erase(ti.it);
105  m_info_table.erase(id);
106  hb_assert(m_id_table.size() == m_info_table.size(), "Resource management broke bad");
107  }
113  const ResourceType& get(int id) const
114  {
115  auto it = m_info_table.find(id);
116  hb_assert(it != m_info_table.end(), "Resource with id " << id << "does not exist.");
117  return it->second.data;
118  }
124  ResourceType& get(int id)
125  {
126  auto it = m_info_table.find(id);
127  hb_assert(it != m_info_table.end(), "Resource with id " << id << "does not exist.");
128  return it->second.data;
129  }
135  const ResourceId& getId(int id) const
136  {
137  auto it = m_info_table.find(id);
138  hb_assert(it != m_info_table.end(), "Resource with id " << id << "does not exist.");
139  return it->second.it->first;
140  }
146  bool isLoaded(int id) const
147  {
148  return (m_info_table.find(id) != m_info_table.end());
149  }
155  bool isLoaded(const ResourceId& resource_id) const
156  {
157  return (m_id_table.find(resource_id) != m_id_table.end());
158  }
165  bool isLoaded(const ResourceId& resource_id, int& id) const
166  {
167  auto it = m_id_table.find(resource_id);
168  bool ret = (it != m_id_table.end());
169  if (ret)
170  id = it->second;
171  return ret;
172  }
178  int countResourceUsage(int id) const
179  {
180  int count = 0;
181  auto it = m_info_table.find(id);
182  if (it != m_info_table.end())
183  count = it->second.count;
184  return count;
185  }
190  int size() const
191  {
192  return m_id_table.size();
193  }
198  int resourceCount() const
199  {
200  return m_resource_count;
201  }
205  void clear()
206  {
207  m_resource_count = 0;
208  m_id_table.clear();
209  m_info_table.clear();
210  }
211 
212  private:
213  static ManagerType* s_instance;
214  struct ResourceInfo
215  {
216  int id, count;
217  typename std::unordered_map<ResourceId, int, Hash>::iterator it;
218  ResourceType data;
219  };
220 
221  int m_resource_count;
222  std::unordered_map<ResourceId, int, Hash> m_id_table;
223  std::unordered_map<int, ResourceInfo> m_info_table;
224  };
225 }
226 template <typename ManagerType, typename ResourceType, typename ResourceId, typename Hash>
228 #endif
void releaseAll(int id)
Release all resources with identifier id.
Definition: ResourceManager.h:98
bool isLoaded(int id) const
Returns wether the resource with identifier id is loaded.
Definition: ResourceManager.h:146
bool isLoaded(const ResourceId &resource_id, int &id) const
Returns wether the resource with ResourceId resource_id is loaded and if it is loaded, puts the ResourceManager id in id.
Definition: ResourceManager.h:165
const ResourceId & getId(int id) const
Get ResourceId of resource with identifier id.
Definition: ResourceManager.h:135
static ManagerType * instance()
Get the singleton instance.
Definition: ResourceManager.h:25
int addResource(const ResourceId &resource_id, const ResourceType &resource)
Add resource to manager.
Definition: ResourceManager.h:51
Definition: Box2DPlugin.h:6
virtual void release(int id)
Release a resource with identifier id.
Definition: ResourceManager.h:79
int size() const
Returns number of resources currently loaded.
Definition: ResourceManager.h:190
Class for efficiently managing loaded resources.
Definition: ResourceManager.h:18
~ResourceManager()
Class destructor.
Definition: ResourceManager.h:41
bool isLoaded(const ResourceId &resource_id) const
Returns wether the resource with ResourceId resource_id is loaded.
Definition: ResourceManager.h:155
int resourceCount() const
Returns number of all resources ever loaded.
Definition: ResourceManager.h:198
ResourceManager()
Class constructor.
Definition: ResourceManager.h:34
void clear()
Release all resources.
Definition: ResourceManager.h:205
int countResourceUsage(int id) const
Returns number of active requests for resource id.
Definition: ResourceManager.h:178