ResourceManager.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef ANKI_RESOURCE_RESOURCE_MANAGER_H
  2. #define ANKI_RESOURCE_RESOURCE_MANAGER_H
  3. #include "anki/util/Vector.h"
  4. #include "anki/util/StdTypes.h"
  5. #include "anki/util/Singleton.h"
  6. #include <string>
  7. namespace anki {
  8. /// Holds information about a resource
  9. template<typename Type>
  10. struct ResourceHook
  11. {
  12. std::string uuid; ///< Unique identifier
  13. U32 referenceCounter = 0;
  14. Type* resource = nullptr;
  15. ~ResourceHook()
  16. {}
  17. Bool operator==(const ResourceHook& b) const
  18. {
  19. return uuid == b.uuid
  20. && referenceCounter == b.referenceCounter
  21. && resource == b.resource;
  22. }
  23. };
  24. /// Resource manager. It holds a few global variables
  25. class ResourceManager
  26. {
  27. public:
  28. ResourceManager();
  29. const std::string& getDataPath() const
  30. {
  31. return dataPath;
  32. }
  33. std::string fixResourcePath(const char* filename) const;
  34. private:
  35. std::string dataPath;
  36. };
  37. /// The singleton of resource manager
  38. typedef Singleton<ResourceManager> ResourceManagerSingleton;
  39. /// Convenience macro to sanitize resources
  40. #define ANKI_R(x_) \
  41. ResourceManagerSingleton::get().fixResourcePath(x_).c_str()
  42. /// Manage resources of a certain type
  43. template<typename Type>
  44. class TypeResourceManager
  45. {
  46. public:
  47. typedef TypeResourceManager<Type> Self;
  48. typedef ResourceHook<Type> Hook;
  49. typedef PtrVector<Hook> Container;
  50. typedef typename Container::iterator Iterator;
  51. typedef typename Container::const_iterator ConstIterator;
  52. virtual ~TypeResourceManager()
  53. {}
  54. Hook& load(const char* filename);
  55. void unload(const Hook& hook);
  56. protected:
  57. Container hooks;
  58. Iterator find(const char* filename);
  59. /// Allocate and load a resource.
  60. /// This method allocates memory for a resource and loads it (calls the
  61. /// load method). Its been used by the load method. Its a separate
  62. /// method because we want to specialize it for async loaded resources
  63. virtual void allocAndLoadRsrc(const char* filename, Type*& ptr);
  64. /// Dealocate the resource. Its separate for two reasons:
  65. /// - Because we want to specialize it for the async loaded resources
  66. /// - Because we cannot have the operator delete in a template body.
  67. /// Apparently the compiler is to dump to decide
  68. virtual void deallocRsrc(Type* rsrc);
  69. };
  70. } // end namespace anki
  71. #include "anki/resource/ResourceManager.inl.h"
  72. #endif