resource_manager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "container_types.h"
  26. #include "resource.h"
  27. #include "proxy_allocator.h"
  28. #include "resource_loader.h"
  29. namespace crown
  30. {
  31. struct ResourceEntry
  32. {
  33. bool operator==(const ResourceId& res) const { return id == res; }
  34. bool operator==(const ResourceEntry& b) const { return id == b.id; }
  35. ResourceId id;
  36. uint32_t references;
  37. void* resource;
  38. };
  39. class Bundle;
  40. /// @defgroup Resource Resource
  41. /// Keeps track and manages resources loaded by ResourceLoader.
  42. ///
  43. /// @ingroup Resource
  44. class ResourceManager
  45. {
  46. public:
  47. /// The resources will be loaded from @a bundle.
  48. ResourceManager(Bundle& bundle);
  49. /// Loads the resource @a id.
  50. /// You can check whether the resource is loaded with can_get().
  51. void load(ResourceId id);
  52. /// Unloads the resource @a id.
  53. /// If @a force is true, the resource is unloaded even if its reference count
  54. /// is greater than 1.
  55. /// @warning
  56. /// Use @a force option only if you know *exactly* what you are doing.
  57. void unload(ResourceId id, bool force = false);
  58. /// Returns whether the manager has the resource @a id.
  59. bool can_get(ResourceId id) const;
  60. /// Returns the resource data by @a type and @a name.
  61. const void* get(const char* type, const char* name) const;
  62. /// Returns the resource data by @a id.
  63. const void* get(ResourceId id) const;
  64. /// Returns the number of references to resource @a id;
  65. uint32_t references(ResourceId id) const;
  66. /// Blocks until all load() requests have been completed.
  67. void flush();
  68. /// Completes all load() requests which have been loaded by ResourceLoader.
  69. void complete_requests();
  70. private:
  71. ResourceEntry* find(ResourceId id) const;
  72. void complete_request(ResourceId id, void* data);
  73. private:
  74. ProxyAllocator m_resource_heap;
  75. ResourceLoader m_loader;
  76. Array<ResourceEntry> m_resources;
  77. };
  78. } // namespace crown