BsResourceHandle.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 AND initialized.
  33. *
  34. * @note Careful not to call this on the thread that does the loading or initializing.
  35. */
  36. void synchronize() 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. template <typename T1>
  110. ResourceHandle(const ResourceHandle<T1>& ptr)
  111. :ResourceHandleBase()
  112. {
  113. mData = ptr.getHandleData();
  114. }
  115. /**
  116. * @brief Converts a specific handle to generic Resource handle.
  117. */
  118. operator ResourceHandle<Resource>()
  119. {
  120. return ResourceHandle<Resource>(*this);
  121. }
  122. /**
  123. * @brief Returns internal resource pointer.
  124. *
  125. * @note Throws exception if handle is invalid.
  126. */
  127. T* operator->() const { return get(); }
  128. /**
  129. * @brief Returns internal resource pointer and dereferences it.
  130. *
  131. * @note Throws exception if handle is invalid.
  132. */
  133. T& operator*() const { return *get(); }
  134. /**
  135. * @brief Clears the handle making it invalid and releases any references
  136. * held to the resource.
  137. */
  138. ResourceHandle<T>& operator=(std::nullptr_t ptr)
  139. {
  140. mData = nullptr;
  141. return *this;
  142. }
  143. template<class _Ty>
  144. struct Bool_struct
  145. {
  146. int _Member;
  147. };
  148. /**
  149. * @brief Allows direct conversion of handle to bool.
  150. *
  151. * @note This is needed because we can't directly convert to bool
  152. * since then we can assign pointer to bool and that's weird.
  153. */
  154. operator int Bool_struct<T>::*() const
  155. {
  156. return ((mData != nullptr && mData->mPtr != nullptr) ? &Bool_struct<T>::_Member : 0);
  157. }
  158. /**
  159. * @brief Returns internal resource pointer and dereferences it.
  160. *
  161. * @note Throws exception if handle is invalid.
  162. */
  163. T* get() const
  164. {
  165. throwIfNotLoaded();
  166. return reinterpret_cast<T*>(mData->mPtr.get());
  167. }
  168. /**
  169. * @brief Returns the internal shared pointer to the resource.
  170. *
  171. * @note Throws exception if handle is invalid.
  172. */
  173. std::shared_ptr<T> getInternalPtr() const
  174. {
  175. throwIfNotLoaded();
  176. return std::static_pointer_cast<T>(mData->mPtr);
  177. }
  178. private:
  179. friend class Resources;
  180. /**
  181. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  182. *
  183. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  184. * delete it elsewhere.
  185. */
  186. explicit ResourceHandle(T* ptr, const String& uuid)
  187. :ResourceHandleBase()
  188. {
  189. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  190. _setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  191. }
  192. /**
  193. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  194. */
  195. ResourceHandle(std::shared_ptr<T> ptr, const String& uuid)
  196. :ResourceHandleBase()
  197. {
  198. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  199. _setHandleData(ptr, uuid);
  200. }
  201. };
  202. /**
  203. * @brief Casts one resource handle to another.
  204. */
  205. template<class _Ty1, class _Ty2>
  206. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  207. {
  208. return ResourceHandle<_Ty1>(other);
  209. }
  210. /**
  211. * @brief Checks if two handles point to the same resource.
  212. */
  213. template<class _Ty1, class _Ty2>
  214. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  215. {
  216. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  217. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  218. return _Left.getHandleData() == _Right.getHandleData();
  219. }
  220. template<class _Ty1, class _Ty2>
  221. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  222. {
  223. return (!(_Left == _Right));
  224. }
  225. }