ResourcePointer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /// Construct and load
  25. ResourcePointer(const char* filename)
  26. {
  27. load(filename);
  28. }
  29. ~ResourcePointer()
  30. {
  31. unload();
  32. }
  33. /// @name Accessors
  34. /// @{
  35. const Value& operator*() const
  36. {
  37. ANKI_ASSERT(hook != nullptr);
  38. return *hook->resource;
  39. }
  40. Value& operator*()
  41. {
  42. ANKI_ASSERT(hook != nullptr);
  43. return *hook->resource;
  44. }
  45. const Value* operator->() const
  46. {
  47. ANKI_ASSERT(hook != nullptr);
  48. return hook->resource;
  49. }
  50. Value* operator->()
  51. {
  52. ANKI_ASSERT(hook != nullptr);
  53. return hook->resource;
  54. }
  55. const Value* get() const
  56. {
  57. return hook->resource;
  58. }
  59. Value* get()
  60. {
  61. return hook->resource;
  62. }
  63. const std::string& getResourceName() const
  64. {
  65. return hook->uuid;
  66. }
  67. /// @}
  68. /// Copy
  69. Self& operator=(const Self& b)
  70. {
  71. copy(b);
  72. return *this;
  73. }
  74. /// Load the resource using the resource manager
  75. void load(const char* filename)
  76. {
  77. ANKI_ASSERT(hook == nullptr);
  78. hook = &ResourceManagerSingleton::get().load(filename);
  79. }
  80. private:
  81. /// Points to an element located in a container in the resource manager
  82. Hook* hook = nullptr;
  83. /// Unloads the resource @see loadRsrc
  84. void unload()
  85. {
  86. if(hook != nullptr)
  87. {
  88. ResourceManagerSingleton::get().unload(*hook);
  89. hook = nullptr;
  90. }
  91. }
  92. /// If this empty and @a b empty then unload. If @a b has something then
  93. /// unload this and load exactly what @b has. In everything else do nothing
  94. void copy(const Self& b)
  95. {
  96. if(b.hook == nullptr)
  97. {
  98. if(hook != nullptr)
  99. {
  100. unload();
  101. }
  102. }
  103. else
  104. {
  105. unload();
  106. load(b.hook->uuid.c_str());
  107. }
  108. }
  109. };
  110. } // end namespace
  111. #endif