BsResourceHandle.h 7.2 KB

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