ResourcePointer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef ANKI_RESOURCE_RESOURCE_POINTER_H
  2. #define ANKI_RESOURCE_RESOURCE_POINTER_H
  3. #include "anki/util/Assert.h"
  4. namespace anki {
  5. /// Special smart pointer that points to resource classes.
  6. ///
  7. /// It looks like auto_ptr but the main difference is that when its out of scope
  8. /// it tries to unload the resource.
  9. template<typename Type, typename ResourceManagerSingleton>
  10. class ResourcePointer
  11. {
  12. public:
  13. typedef Type Value; ///< Resource type
  14. typedef ResourcePointer<Value, ResourceManagerSingleton> Self;
  15. typedef typename ResourceManagerSingleton::Value::Hook Hook;
  16. /// Default constructor
  17. ResourcePointer()
  18. {}
  19. /// Copy constructor
  20. ResourcePointer(const Self& b)
  21. {
  22. copy(b);
  23. }
  24. /// Move contructor
  25. ResourcePointer(Self&& b)
  26. {
  27. *this = std::move(b);
  28. }
  29. /// Construct and load
  30. ResourcePointer(const char* filename)
  31. {
  32. load(filename);
  33. }
  34. ~ResourcePointer()
  35. {
  36. unload();
  37. }
  38. /// @name Accessors
  39. /// @{
  40. const Value& operator*() const
  41. {
  42. ANKI_ASSERT(hook != nullptr);
  43. return *hook->resource;
  44. }
  45. Value& operator*()
  46. {
  47. ANKI_ASSERT(hook != nullptr);
  48. return *hook->resource;
  49. }
  50. const Value* operator->() const
  51. {
  52. ANKI_ASSERT(hook != nullptr);
  53. return hook->resource;
  54. }
  55. Value* operator->()
  56. {
  57. ANKI_ASSERT(hook != nullptr);
  58. return hook->resource;
  59. }
  60. const Value* get() const
  61. {
  62. ANKI_ASSERT(hook != nullptr);
  63. return hook->resource;
  64. }
  65. Value* get()
  66. {
  67. ANKI_ASSERT(hook != nullptr);
  68. return hook->resource;
  69. }
  70. const std::string& getResourceName() const
  71. {
  72. ANKI_ASSERT(hook != nullptr);
  73. return hook->uuid;
  74. }
  75. /// @}
  76. /// Copy
  77. Self& operator=(const Self& b)
  78. {
  79. copy(b);
  80. return *this;
  81. }
  82. /// Move
  83. Self& operator=(Self&& b)
  84. {
  85. hook = b.hook;
  86. b.hook = nullptr;
  87. return *this;
  88. }
  89. /// Load the resource using the resource manager
  90. void load(const char* filename)
  91. {
  92. ANKI_ASSERT(hook == nullptr);
  93. hook = &ResourceManagerSingleton::get().load(filename);
  94. }
  95. Bool isLoaded() const
  96. {
  97. return hook != nullptr;
  98. }
  99. private:
  100. /// Points to an element located in a container in the resource manager
  101. Hook* hook = nullptr;
  102. /// Unloads the resource @see loadRsrc
  103. void unload()
  104. {
  105. if(hook != nullptr)
  106. {
  107. ResourceManagerSingleton::get().unload(*hook);
  108. hook = nullptr;
  109. }
  110. }
  111. /// If this empty and @a b empty then unload. If @a b has something then
  112. /// unload this and load exactly what @b has. In everything else do nothing
  113. void copy(const Self& b)
  114. {
  115. if(b.hook == nullptr)
  116. {
  117. if(hook != nullptr)
  118. {
  119. unload();
  120. }
  121. }
  122. else
  123. {
  124. unload();
  125. load(b.hook->uuid.c_str());
  126. }
  127. }
  128. };
  129. } // end namespace anki
  130. #endif