ResourceManager.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "ContainerTypes.h"
  26. #include "Resource.h"
  27. #include "ProxyAllocator.h"
  28. #include "ResourceLoader.h"
  29. #include "Hash.h"
  30. namespace crown
  31. {
  32. struct ResourceEntry
  33. {
  34. uint32_t type;
  35. uint32_t references;
  36. void* resource;
  37. };
  38. struct PendingRequest
  39. {
  40. LoadResourceId id;
  41. ResourceId resource;
  42. uint32_t type;
  43. };
  44. class Bundle;
  45. /// @defgroup Resource Resource.
  46. /// Keeps track and manages resources loaded by ResourceLoader.
  47. ///
  48. /// @ingroup Resource
  49. class ResourceManager
  50. {
  51. public:
  52. /// The resources will be loaded from @a bundle.
  53. ResourceManager(Bundle& bundle, uint32_t seed);
  54. ~ResourceManager();
  55. /// Loads the resource by @a type and @a name and returns its ResourceId.
  56. /// @note
  57. /// You have to call is_loaded() to check if the loading process is actually completed.
  58. ResourceId load(const char* type, const char* name);
  59. /// Unloads the resource @a name, freeing up all the memory associated by it
  60. /// and eventually any global object associated with it.
  61. /// If @a force is true, the resource is unloaded even if its reference count
  62. /// is greater than 1.
  63. /// @warning
  64. /// Use @a force option only if you know - exactly - what you are doing.
  65. void unload(ResourceId name, bool force = false);
  66. /// Returns whether the manager has the @a name resource into
  67. /// its list of resources.
  68. /// @warning
  69. /// Having a resource does not mean that the resource is
  70. /// ready to be used; See is_loaded().
  71. bool has(ResourceId name) const;
  72. /// Returns the resource instance associated to the given @a type and @a name.
  73. const void* get(const char* type, const char* name) const;
  74. /// Returns the data associated with the @a name resource.
  75. /// You will have to cast the returned pointer accordingly.
  76. const void* get(ResourceId name) const;
  77. /// Returns whether the @a name resource is loaded (i.e. whether
  78. /// you can use the data associated with it).
  79. bool is_loaded(ResourceId name) const;
  80. /// Returns the number of references to the resource @a name;
  81. uint32_t references(ResourceId name) const;
  82. /// Forces all of the loading requests to complete before preceeding.
  83. void flush();
  84. /// Returns the seed used to generate resource name hashes.
  85. uint32_t seed() const;
  86. private:
  87. ResourceEntry* find(ResourceId id) const;
  88. // Polls the resource loader for loaded resources.
  89. void poll_resource_loader();
  90. // Loads the resource by name and type and returns its ResourceId.
  91. ResourceId load(uint32_t type, ResourceId name);
  92. void online(ResourceId name, void* resource);
  93. private:
  94. ProxyAllocator m_resource_heap;
  95. ResourceLoader m_loader;
  96. uint32_t m_seed;
  97. Queue<PendingRequest> m_pendings;
  98. Hash<ResourceEntry> m_resources;
  99. private:
  100. friend class ResourcePackage;
  101. friend class Device;
  102. };
  103. } // namespace crown