ResourceManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Types.h"
  24. #include "Dictionary.h"
  25. #include "Resource.h"
  26. namespace crown
  27. {
  28. /**
  29. Resource manager.
  30. Keeps track of loaded resources.
  31. */
  32. class ResourceManager
  33. {
  34. // FIXME: replace with hashmap
  35. typedef Dictionary<StrId64, Resource*> NameToResourceDict;
  36. public:
  37. Resource* Create(const char* name, bool& created);
  38. void Destroy(const char* name);
  39. Resource* Load(const char* name);
  40. void Unload(const char* name, bool reload);
  41. Resource* GetByName(const char* name);
  42. int32_t GetResourceCount() const;
  43. /**
  44. * Creates a generic resource with the given name.
  45. * If a resource with the same name already exists, the
  46. * already existent resource will be returned. In order
  47. * to distinguish between a newly created resource or a
  48. * point32_ter to an existing one, you have to check
  49. * at the value returned by 'created'.
  50. * @param name The name of the resource
  51. * @param created Returns true if newly created, false otherwise
  52. * @return A point32_ter to the created resource
  53. */
  54. /**
  55. * Loads a generic resource from file.
  56. * The name of the file determines the name of the resource and vice-versa.
  57. * @param name Tha name of the resource
  58. * @return A point32_ter to the loaded resource
  59. */
  60. /**
  61. Unloads a generic resource with the given name.
  62. @param name
  63. The name of the resource
  64. @param reload
  65. Whether to reload after unloading
  66. */
  67. /**
  68. * Destroys a generic resource with the given name.
  69. * Causes the manager to remove the resource from its
  70. * int32_ternal list.
  71. * @note Destroying here is a misleading term since the
  72. * resource is not destroyed if there are other references
  73. * to it.
  74. * @param name The name of the resource
  75. */
  76. /**
  77. * Returns a resource by its name.
  78. * If the resource does not exists, returns a null point32_ter.
  79. * @param name The name of the resource
  80. * @return A point32_ter to the resource
  81. */
  82. /**
  83. * Returns the number of resources managed by this resource manager.
  84. * @return The number of resources
  85. */
  86. protected:
  87. ResourceManager(); //!< Constructor
  88. virtual ~ResourceManager(); //!< Destructor
  89. virtual Resource* CreateSpecific(const char* name) = 0;
  90. virtual void DestroySpecific(const char* name);
  91. // Resource name -> Resource point32_ter
  92. NameToResourceDict mNameToResourceDict;
  93. };
  94. } // namespace crown