BsResourceHandle.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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), mRefCount(0)
  14. { }
  15. SPtr<Resource> mPtr;
  16. String mUUID;
  17. bool mIsCreated;
  18. UINT32 mRefCount;
  19. };
  20. /**
  21. * @brief Base class containing common functionality for resource handles.
  22. */
  23. class BS_CORE_EXPORT ResourceHandleBase : public IReflectable
  24. {
  25. public:
  26. virtual ~ResourceHandleBase();
  27. /**
  28. * @brief Checks if the resource is loaded. Until resource is loaded this handle
  29. * is invalid and you may not get the internal resource from it.
  30. *
  31. * @param checkDependencies If true, and if resource has any dependencies, this method will
  32. * also check if they are loaded.
  33. */
  34. bool isLoaded(bool checkDependencies = true) const;
  35. /**
  36. * @brief Blocks the current thread until the resource is fully loaded.
  37. *
  38. * @note Careful not to call this on the thread that does the loading.
  39. */
  40. void blockUntilLoaded(bool waitForDependencies = true) const;
  41. /**
  42. * @brief Returns the UUID of the resource the handle is referring to.
  43. */
  44. const String& getUUID() const { return mData != nullptr ? mData->mUUID : StringUtil::BLANK; }
  45. /**
  46. * @brief Gets the handle data. For internal use only.
  47. */
  48. const SPtr<ResourceHandleData>& getHandleData() const { return mData; }
  49. protected:
  50. ResourceHandleBase();
  51. /**
  52. * @brief Sets the created flag to true and assigns the resource pointer. Called
  53. * by the constructors, or if you constructed just using a UUID, then you need to
  54. * call this manually before you can access the resource from this handle.
  55. *
  56. * @note This is needed because two part construction is required due to
  57. * multithreaded nature of resource loading.
  58. * Internal method.
  59. */
  60. void setHandleData(const SPtr<Resource>& ptr, const String& uuid);
  61. /** @note All handles to the same source must share this same handle data. Otherwise things
  62. * like counting number of references or replacing pointed to resource become impossible
  63. * without additional logic. */
  64. SPtr<ResourceHandleData> mData;
  65. private:
  66. friend class Resources;
  67. BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  68. BS_STATIC_MUTEX(mResourceCreatedMutex)
  69. protected:
  70. inline void throwIfNotLoaded() const;
  71. /************************************************************************/
  72. /* RTTI */
  73. /************************************************************************/
  74. public:
  75. friend class ResourceHandleRTTI;
  76. static RTTITypeBase* getRTTIStatic();
  77. virtual RTTITypeBase* getRTTI() const override;
  78. };
  79. /**
  80. * @brief Represents a handle to a resource. Handles are similar to a smart pointers, but they have two advantages:
  81. * - When loading a resource asynchronously you can be immediately returned the handle
  82. * that you may use throughout the engine. The handle will be made valid as soon as
  83. * the resource is loaded.
  84. * - Handles can be serialized and deserialized, therefore saving/restoring references
  85. * to their original resource.
  86. */
  87. template <typename T>
  88. class ResourceHandle : public ResourceHandleBase
  89. {
  90. public:
  91. ResourceHandle()
  92. :ResourceHandleBase()
  93. { }
  94. /**
  95. * @brief Copy constructor.
  96. */
  97. ResourceHandle(const ResourceHandle<T>& ptr)
  98. :ResourceHandleBase()
  99. {
  100. mData = ptr.getHandleData();
  101. if (mData != nullptr)
  102. mData->mRefCount++;
  103. }
  104. ~ResourceHandle()
  105. {
  106. if (mData != nullptr)
  107. mData->mRefCount--;
  108. }
  109. /**
  110. * @brief Converts a specific handle to generic Resource handle.
  111. */
  112. operator ResourceHandle<Resource>() const
  113. {
  114. ResourceHandle<Resource> handle;
  115. handle.setHandleData(getHandleData());
  116. return handle;
  117. }
  118. /**
  119. * @brief Returns internal resource pointer.
  120. *
  121. * @note Throws exception if handle is invalid.
  122. */
  123. T* operator->() const { return get(); }
  124. /**
  125. * @brief Returns internal resource pointer and dereferences it.
  126. *
  127. * @note Throws exception if handle is invalid.
  128. */
  129. T& operator*() const { return *get(); }
  130. /**
  131. * @brief Clears the handle making it invalid and releases any references
  132. * held to the resource.
  133. */
  134. ResourceHandle<T>& operator=(std::nullptr_t ptr)
  135. {
  136. if (mData != nullptr)
  137. mData->mRefCount--;
  138. mData = nullptr;
  139. return *this;
  140. }
  141. template<class _Ty>
  142. struct Bool_struct
  143. {
  144. int _Member;
  145. };
  146. /**
  147. * @brief Allows direct conversion of handle to bool.
  148. *
  149. * @note This is needed because we can't directly convert to bool
  150. * since then we can assign pointer to bool and that's weird.
  151. */
  152. operator int Bool_struct<T>::*() const
  153. {
  154. return ((mData != nullptr && mData->mPtr != nullptr) ? &Bool_struct<T>::_Member : 0);
  155. }
  156. /**
  157. * @brief Returns internal resource pointer and dereferences it.
  158. *
  159. * @note Throws exception if handle is invalid.
  160. */
  161. T* get() const
  162. {
  163. throwIfNotLoaded();
  164. return reinterpret_cast<T*>(mData->mPtr.get());
  165. }
  166. /**
  167. * @brief Returns the internal shared pointer to the resource.
  168. *
  169. * @note Throws exception if handle is invalid.
  170. */
  171. SPtr<T> getInternalPtr() const
  172. {
  173. throwIfNotLoaded();
  174. return std::static_pointer_cast<T>(mData->mPtr);
  175. }
  176. private:
  177. friend class Resources;
  178. template<class _Ty1>
  179. friend class ResourceHandle;
  180. template<class _Ty1, class _Ty2>
  181. friend ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other);
  182. /**
  183. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  184. *
  185. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  186. * delete it elsewhere.
  187. */
  188. explicit ResourceHandle(T* ptr, const String& uuid)
  189. :ResourceHandleBase()
  190. {
  191. mData = bs_shared_ptr_new<ResourceHandleData>();
  192. mData->mRefCount++;
  193. setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  194. }
  195. /**
  196. * @brief Constructs an invalid handle with the specified UUID. You must call setHandleData
  197. * with the actual resource pointer to make the handle valid.
  198. */
  199. ResourceHandle(const String& uuid)
  200. :ResourceHandleBase()
  201. {
  202. mData = bs_shared_ptr_new<ResourceHandleData>();
  203. mData->mRefCount++;
  204. mData->mUUID = uuid;
  205. }
  206. /**
  207. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  208. */
  209. ResourceHandle(std::shared_ptr<T> ptr, const String& uuid)
  210. :ResourceHandleBase()
  211. {
  212. mData = bs_shared_ptr_new<ResourceHandleData>();
  213. mData->mRefCount++;
  214. setHandleData(ptr, uuid);
  215. }
  216. /**
  217. * @brief Sets the internal handle data to another previously created data.
  218. *
  219. * @note Internal method.
  220. */
  221. void setHandleData(const SPtr<ResourceHandleData>& data)
  222. {
  223. if (mData != nullptr)
  224. mData->mRefCount--;
  225. mData = data;
  226. if (mData != nullptr)
  227. mData->mRefCount++;
  228. }
  229. using ResourceHandleBase::setHandleData;
  230. };
  231. /**
  232. * @brief Casts one resource handle to another.
  233. */
  234. template<class _Ty1, class _Ty2>
  235. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  236. {
  237. ResourceHandle<_Ty1> handle;
  238. handle.setHandleData(other.getHandleData());
  239. return handle;
  240. }
  241. /**
  242. * @brief Checks if two handles point to the same resource.
  243. */
  244. template<class _Ty1, class _Ty2>
  245. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  246. {
  247. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  248. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  249. return _Left.getHandleData() == _Right.getHandleData();
  250. }
  251. template<class _Ty1, class _Ty2>
  252. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  253. {
  254. return (!(_Left == _Right));
  255. }
  256. }