ResourceManager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace crown
  30. {
  31. struct ResourceEntry
  32. {
  33. bool operator==(const ResourceId& resource) const { return id == resource; }
  34. bool operator==(const ResourceEntry& b) const { return id == b.id; }
  35. ResourceId id;
  36. uint32_t type;
  37. uint32_t references;
  38. void* resource;
  39. };
  40. struct PendingRequest
  41. {
  42. LoadResourceId id;
  43. ResourceId resource;
  44. uint32_t type;
  45. };
  46. class Bundle;
  47. /// Keeps track and manages resources loaded by ResourceLoader.
  48. class ResourceManager
  49. {
  50. public:
  51. /// The resources will be loaded from @a bundle.
  52. ResourceManager(Bundle& bundle);
  53. ~ResourceManager();
  54. /// Loads the resource by @a type and @a name and returns its ResourceId.
  55. /// @note
  56. /// You have to call is_loaded() to check if the loading process is actually completed.
  57. ResourceId load(const char* type, const char* name);
  58. /// Unloads the resource @a name, freeing up all the memory associated by it
  59. /// and eventually any global object associated with it.
  60. /// If @a force is true, the resource is unloaded even if its reference count
  61. /// is greater than 1.
  62. /// @warning
  63. /// Use @a force option only if you know - exactly - what you are doing.
  64. void unload(ResourceId name, bool force = false);
  65. /// Returns whether the manager has the @a name resource into
  66. /// its list of resources.
  67. /// @warning
  68. /// Having a resource does not mean that the resource is
  69. /// ready to be used; See is_loaded().
  70. bool has(ResourceId name) const;
  71. /// Returns the resource instance associated to the given @a type and @a name.
  72. const void* get(const char* type, const char* name) const;
  73. /// Returns the data associated with the @a name resource.
  74. /// You will have to cast the returned pointer accordingly.
  75. const void* get(ResourceId name) const;
  76. /// Returns whether the @a name resource is loaded (i.e. whether
  77. /// you can use the data associated with it).
  78. bool is_loaded(ResourceId name) const;
  79. /// Returns the number of references to the resource @a name;
  80. uint32_t references(ResourceId name) const;
  81. /// Forces all of the loading requests to complete before preceeding.
  82. void flush();
  83. private:
  84. // Returns the entry of the given id.
  85. ResourceEntry* find(ResourceId id) const;
  86. // Polls the resource loader for loaded resources.
  87. void poll_resource_loader();
  88. // Loads the resource by name and type and returns its ResourceId.
  89. ResourceId load(uint32_t type, ResourceId name);
  90. void online(ResourceId name, void* resource);
  91. private:
  92. ProxyAllocator m_resource_heap;
  93. ResourceLoader m_loader;
  94. uint32_t m_seed;
  95. Queue<PendingRequest> m_pendings;
  96. Array<ResourceEntry> m_resources;
  97. private:
  98. friend class ResourcePackage;
  99. friend class Device;
  100. };
  101. } // namespace crown