Ocular Engine
ResourceManager.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_RESOURCES_RESOURCE_MANAGER__H__
19 #define __H__OCULAR_RESOURCES_RESOURCE_MANAGER__H__
20 
21 #include <memory>
22 #include <list>
23 #include <string>
24 #include <unordered_map>
25 
26 #include "ResourceDetails.hpp"
27 #include "ResourceMemoryDetails.hpp"
28 #include "ResourcePriorityBehaviour.hpp"
29 #include "ResourceExplorer.hpp"
30 #include "ResourceLoader.hpp"
31 #include "ResourceLoaderManager.hpp"
32 #include "ResourceSaver.hpp"
33 #include "ResourceSaverManager.hpp"
34 #include "ResourceDefaults.hpp"
35 
36 //------------------------------------------------------------------------------------------
37 
42 namespace Ocular
43 {
48  namespace Core
49  {
111  {
112  public:
113 
114  ResourceManager();
115  ~ResourceManager();
116 
117  //----------------------------------------
118 
122  void initialize();
123 
127  void unloadAllResources();
128 
132  void forceSourceRefresh();
133 
140  bool forceLoadResource(std::string const& path);
141 
148  bool forceUnloadResource(std::string const& path);
149 
163  bool addResource(std::string const& name, File const& sourceFile, Resource* resource, ResourceType type);
164 
172 
180  Resource* getResource(std::string const& path);
181 
189  template<class T> T* getResource(std::string const& path) { return dynamic_cast<T*>(getResource(path)); };
190 
195  Resource* getEmptyResource(ResourceType type);
196 
201  Resource* getMissingResource(ResourceType type);
202 
218  File getResourceFile(std::string const& path) const;
219 
235  std::string getResourceMappingName(File const& file) const;
236 
241  bool isInMemory(std::string const& path);
242 
247  bool doesExist(std::string const& path);
248 
261  Resource* loadUnmanagedFile(File const& file);
262 
280  bool saveResource(Resource* resource, File const& file);
281 
288  void setMemoryLimit(uint64_t maxMemory);
289 
298  void setPriorityBehaviour(ResourcePriorityBehaviour behaviour);
299 
306  void setSourceBlacklist(std::list<std::string> const& blacklist);
307 
319  void setSourceDirectory(std::string const& directory);
320 
324  std::string const& getSourceDirectory() const;
325 
331  void registerResourceLoader(std::shared_ptr<AResourceLoader> loader);
332 
338  void registerResourceSaver(std::shared_ptr<AResourceSaver> loader);
339 
343  unsigned getNumberOfResources() const;
344 
348  unsigned getNumberOfResourceLoaders() const;
349 
353  unsigned getNumberOfResourceSavers() const;
354 
362  void isFileTypeSupported(std::string const& extension, bool& canLoad, bool& canSave) const;
363 
370  void getResourcesOfType(ResourceType type, std::vector<std::string>& resources);
371 
372  protected:
373 
379  void freeMemorySpace();
380 
385 
390 
395 
400 
401  private:
402 
403  std::unordered_map<std::string, std::shared_ptr<ResourceDetails>> m_ResourceMap;
404  std::unordered_map<std::string, File> m_FileMap;
405 
406  ResourceExplorer m_ResourceExplorer;
407  ResourceLoaderManager m_ResourceLoaderManager;
408  ResourceSaverManager m_ResourceSaverManager;
409  ResourceMemoryDetails m_MemoryDetails;
410  ResourceDefaults m_ResourceDefaults;
411 
412  ResourcePriorityBehaviour m_PriorityBehaviour;
413 
414  uint64_t m_MemoryLimit;
415 
416  };
417  }
421 }
426 //------------------------------------------------------------------------------------------
427 
428 #endif
T * getResource(std::string const &path)
Definition: ResourceManager.hpp:189
Definition: ResourceExplorer.hpp:49
Definition: ResourceSaverManager.hpp:44
ResourceDetails * findLeastFrequentlyUsed()
Definition: ResourceManager.cpp:664
bool forceLoadResource(std::string const &path)
Definition: ResourceManager.cpp:121
ResourceDetails * findSizeAscending()
Definition: ResourceManager.cpp:708
void isFileTypeSupported(std::string const &extension, bool &canLoad, bool &canSave) const
Definition: ResourceManager.cpp:554
void getResourcesOfType(ResourceType type, std::vector< std::string > &resources)
Definition: ResourceManager.cpp:560
unsigned getNumberOfResourceLoaders() const
Definition: ResourceManager.cpp:544
Maintains a resource and usage details.
Definition: ResourceDetails.hpp:44
Definition: ResourceDefaults.hpp:38
unsigned getNumberOfResourceSavers() const
Definition: ResourceManager.cpp:549
void setMemoryLimit(uint64_t maxMemory)
Definition: ResourceManager.cpp:503
ResourceDetails * findLeastRecentlyUsed()
Definition: ResourceManager.cpp:686
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
void registerResourceLoader(std::shared_ptr< AResourceLoader > loader)
Definition: ResourceManager.cpp:529
Resource * getEmptyResource(ResourceType type)
Definition: ResourceManager.cpp:370
void setSourceDirectory(std::string const &directory)
Definition: ResourceManager.cpp:519
std::string getResourceMappingName(File const &file) const
Definition: ResourceManager.cpp:395
bool addResource(std::string const &name, File const &sourceFile, Resource *resource, ResourceType type)
Definition: ResourceManager.cpp:199
bool saveResource(Resource *resource, File const &file)
Definition: ResourceManager.cpp:477
bool forceUnloadResource(std::string const &path)
Definition: ResourceManager.cpp:171
void freeMemorySpace()
Definition: ResourceManager.cpp:602
std::string const & getSourceDirectory() const
Definition: ResourceManager.cpp:524
Resource * getResource(std::string const &path)
Definition: ResourceManager.cpp:229
void setSourceBlacklist(std::list< std::string > const &blacklist)
Definition: ResourceManager.cpp:514
bool doesExist(std::string const &path)
Definition: ResourceManager.cpp:446
Definition: ResourceLoaderManager.hpp:44
Resource * getMissingResource(ResourceType type)
Definition: ResourceManager.cpp:375
File getResourceFile(std::string const &path) const
Definition: ResourceManager.cpp:380
void forceSourceRefresh()
Definition: ResourceManager.cpp:90
Definition: File.hpp:41
Definition: ResourceMemoryDetails.hpp:41
unsigned getNumberOfResources() const
Definition: ResourceManager.cpp:539
void registerResourceSaver(std::shared_ptr< AResourceSaver > loader)
Definition: ResourceManager.cpp:534
ResourceMemoryDetails getMemoryUsage()
Definition: ResourceManager.cpp:224
bool isInMemory(std::string const &path)
Definition: ResourceManager.cpp:426
void setPriorityBehaviour(ResourcePriorityBehaviour behaviour)
Definition: ResourceManager.cpp:509
Definition: ResourceManager.hpp:110
Resource * loadUnmanagedFile(File const &file)
Definition: ResourceManager.cpp:461
ResourceDetails * findSizeDescending()
Definition: ResourceManager.cpp:730
Definition: Resource.hpp:46