BsResourceHandle.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #pragma once
  2. #include "BsIReflectable.h"
  3. namespace BansheeEngine
  4. {
  5. template <typename T>
  6. class ResourceHandle;
  7. /**
  8. * @brief Data that is shared between all resource handles.
  9. */
  10. struct BS_CORE_EXPORT ResourceHandleData
  11. {
  12. ResourceHandleData()
  13. :mIsCreated(false)
  14. { }
  15. std::shared_ptr<Resource> mPtr;
  16. String mUUID;
  17. bool mIsCreated;
  18. };
  19. /**
  20. * @brief Base class containing common functionality for resource handles.
  21. */
  22. class BS_CORE_EXPORT ResourceHandleBase : public IReflectable
  23. {
  24. public:
  25. /**
  26. * @brief Checks if the resource is loaded. Until resource is loaded this handle
  27. * is invalid and you may not get the internal resource from it.
  28. */
  29. bool isLoaded() const;
  30. /**
  31. * @brief Blocks the current thread until the resource is fully loaded AND initialized.
  32. *
  33. * @note Careful not to call this on the thread that does the loading or initializing.
  34. */
  35. void synchronize() const;
  36. /**
  37. * @brief Returns the UUID of the resource the handle is referring to.
  38. */
  39. const String& getUUID() const { return mData != nullptr ? mData->mUUID : StringUtil::BLANK; }
  40. /**
  41. * @brief Gets the handle data. For internal use only.
  42. */
  43. const std::shared_ptr<ResourceHandleData>& getHandleData() const { return mData; }
  44. /**
  45. * @brief Sets the created flag to true and assigns the resource pointer. Called
  46. * by the constructors, or if you constructed just using a UUID, then you need to
  47. * call this manually before you can access the resource from this handle.
  48. *
  49. * @note This is needed because two part construction is required due to
  50. * multithreaded nature of resource loading.
  51. * Internal method.
  52. */
  53. void _setHandleData(std::shared_ptr<Resource> ptr, const String& uuid);
  54. protected:
  55. ResourceHandleBase();
  56. std::shared_ptr<ResourceHandleData> mData;
  57. private:
  58. friend class Resources;
  59. BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  60. BS_STATIC_MUTEX(mResourceCreatedMutex)
  61. protected:
  62. inline void throwIfNotLoaded() const;
  63. /************************************************************************/
  64. /* RTTI */
  65. /************************************************************************/
  66. public:
  67. friend class ResourceHandleRTTI;
  68. static RTTITypeBase* getRTTIStatic();
  69. virtual RTTITypeBase* getRTTI() const;
  70. };
  71. /**
  72. * @brief Represents a handle to a resource. Handles are similar to a smart pointers, but they have two advantages:
  73. * - When loading a resource asynchronously you can be immediately returned the handle
  74. * that you may use throughout the engine. The handle will be made valid as soon as
  75. * the resource is loaded.
  76. * - Handles can be serialized and deserialized, therefore saving/restoring references
  77. * to their original resource.
  78. */
  79. template <typename T>
  80. class ResourceHandle : public ResourceHandleBase
  81. {
  82. public:
  83. ResourceHandle()
  84. :ResourceHandleBase()
  85. { }
  86. /**
  87. * @brief Constructs an invalid handle with the specified UUID. You must call _setHandleData
  88. * with the actual resource pointer to make the handle valid.
  89. */
  90. ResourceHandle(const String& uuid)
  91. :ResourceHandleBase()
  92. {
  93. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  94. mData->mUUID = uuid;
  95. }
  96. /**
  97. * @brief Copy constructor.
  98. */
  99. template <typename T1>
  100. ResourceHandle(const ResourceHandle<T1>& ptr)
  101. :ResourceHandleBase()
  102. {
  103. mData = ptr.getHandleData();
  104. }
  105. /**
  106. * @brief Converts a specific handle to generic Resource handle.
  107. */
  108. operator ResourceHandle<Resource>()
  109. {
  110. return ResourceHandle<Resource>(*this);
  111. }
  112. /**
  113. * @brief Returns internal resource pointer.
  114. *
  115. * @note Throws exception if handle is invalid.
  116. */
  117. T* operator->() const { return get(); }
  118. /**
  119. * @brief Returns internal resource pointer and dereferences it.
  120. *
  121. * @note Throws exception if handle is invalid.
  122. */
  123. T& operator*() const { return *get(); }
  124. /**
  125. * @brief Clears the handle making it invalid and releases any references
  126. * held to the resource.
  127. */
  128. ResourceHandle<T>& operator=(std::nullptr_t ptr)
  129. {
  130. mData = nullptr;
  131. return *this;
  132. }
  133. template<class _Ty>
  134. struct Bool_struct
  135. {
  136. int _Member;
  137. };
  138. /**
  139. * @brief Allows direct conversion of handle to bool.
  140. *
  141. * @note This is needed because we can't directly convert to bool
  142. * since then we can assign pointer to bool and that's weird.
  143. */
  144. operator int Bool_struct<T>::*() const
  145. {
  146. return ((mData != nullptr && mData->mPtr != nullptr) ? &Bool_struct<T>::_Member : 0);
  147. }
  148. /**
  149. * @brief Returns internal resource pointer and dereferences it.
  150. *
  151. * @note Throws exception if handle is invalid.
  152. */
  153. T* get() const
  154. {
  155. throwIfNotLoaded();
  156. return reinterpret_cast<T*>(mData->mPtr.get());
  157. }
  158. /**
  159. * @brief Returns the internal shared pointer to the resource.
  160. *
  161. * @note Throws exception if handle is invalid.
  162. */
  163. std::shared_ptr<T> getInternalPtr() const
  164. {
  165. throwIfNotLoaded();
  166. return std::static_pointer_cast<T>(mData->mPtr);
  167. }
  168. private:
  169. friend class Resources;
  170. /**
  171. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  172. *
  173. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  174. * delete it elsewhere.
  175. */
  176. explicit ResourceHandle(T* ptr, const String& uuid)
  177. :ResourceHandleBase()
  178. {
  179. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  180. _setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  181. }
  182. /**
  183. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  184. */
  185. ResourceHandle(std::shared_ptr<T> ptr, const String& uuid)
  186. :ResourceHandleBase()
  187. {
  188. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  189. _setHandleData(ptr, uuid);
  190. }
  191. };
  192. /**
  193. * @brief Casts one resource handle to another.
  194. */
  195. template<class _Ty1, class _Ty2>
  196. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  197. {
  198. return ResourceHandle<_Ty1>(other);
  199. }
  200. /**
  201. * @brief Checks if two handles point to the same resource.
  202. */
  203. template<class _Ty1, class _Ty2>
  204. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  205. {
  206. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  207. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  208. return _Left.getHandleData() == _Right.getHandleData();
  209. }
  210. template<class _Ty1, class _Ty2>
  211. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  212. {
  213. return (!(_Left == _Right));
  214. }
  215. }