BsResourceHandle.h 7.0 KB

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