BsResourceHandle.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. protected:
  56. ResourceHandleBase();
  57. std::shared_ptr<ResourceHandleData> mData;
  58. private:
  59. friend class Resources;
  60. BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  61. BS_STATIC_MUTEX(mResourceCreatedMutex)
  62. protected:
  63. inline void throwIfNotLoaded() const;
  64. /************************************************************************/
  65. /* RTTI */
  66. /************************************************************************/
  67. public:
  68. friend class ResourceHandleRTTI;
  69. static RTTITypeBase* getRTTIStatic();
  70. virtual RTTITypeBase* getRTTI() const;
  71. };
  72. /**
  73. * @brief Represents a handle to a resource. Handles are similar to a smart pointers, but they have two advantages:
  74. * - When loading a resource asynchronously you can be immediately returned the handle
  75. * that you may use throughout the engine. The handle will be made valid as soon as
  76. * the resource is loaded.
  77. * - Handles can be serialized and deserialized, therefore saving/restoring references
  78. * to their original resource.
  79. */
  80. template <typename T>
  81. class ResourceHandle : public ResourceHandleBase
  82. {
  83. public:
  84. ResourceHandle()
  85. :ResourceHandleBase()
  86. { }
  87. /**
  88. * @brief Constructs an invalid handle with the specified UUID. You must call _setHandleData
  89. * with the actual resource pointer to make the handle valid.
  90. */
  91. ResourceHandle(const String& uuid)
  92. :ResourceHandleBase()
  93. {
  94. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  95. mData->mUUID = uuid;
  96. }
  97. /**
  98. * @brief Copy constructor.
  99. */
  100. template <typename T1>
  101. ResourceHandle(const ResourceHandle<T1>& ptr)
  102. :ResourceHandleBase()
  103. {
  104. mData = ptr.getHandleData();
  105. }
  106. /**
  107. * @brief Converts a specific handle to generic Resource handle.
  108. */
  109. operator ResourceHandle<Resource>()
  110. {
  111. return ResourceHandle<Resource>(*this);
  112. }
  113. /**
  114. * @brief Returns internal resource pointer.
  115. *
  116. * @note Throws exception if handle is invalid.
  117. */
  118. T* operator->() const { return get(); }
  119. /**
  120. * @brief Returns internal resource pointer and dereferences it.
  121. *
  122. * @note Throws exception if handle is invalid.
  123. */
  124. T& operator*() const { return *get(); }
  125. /**
  126. * @brief Clears the handle making it invalid and releases any references
  127. * held to the resource.
  128. */
  129. ResourceHandle<T>& operator=(std::nullptr_t ptr)
  130. {
  131. mData = nullptr;
  132. return *this;
  133. }
  134. template<class _Ty>
  135. struct Bool_struct
  136. {
  137. int _Member;
  138. };
  139. /**
  140. * @brief Allows direct conversion of handle to bool.
  141. *
  142. * @note This is needed because we can't directly convert to bool
  143. * since then we can assign pointer to bool and that's weird.
  144. */
  145. operator int Bool_struct<T>::*() const
  146. {
  147. return ((mData != nullptr && mData->mPtr != nullptr) ? &Bool_struct<T>::_Member : 0);
  148. }
  149. /**
  150. * @brief Returns internal resource pointer and dereferences it.
  151. *
  152. * @note Throws exception if handle is invalid.
  153. */
  154. T* get() const
  155. {
  156. throwIfNotLoaded();
  157. return reinterpret_cast<T*>(mData->mPtr.get());
  158. }
  159. /**
  160. * @brief Returns the internal shared pointer to the resource.
  161. *
  162. * @note Throws exception if handle is invalid.
  163. */
  164. std::shared_ptr<T> getInternalPtr() const
  165. {
  166. throwIfNotLoaded();
  167. return std::static_pointer_cast<T>(mData->mPtr);
  168. }
  169. private:
  170. friend class Resources;
  171. /**
  172. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  173. *
  174. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  175. * delete it elsewhere.
  176. */
  177. explicit ResourceHandle(T* ptr, const String& uuid)
  178. :ResourceHandleBase()
  179. {
  180. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  181. _setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  182. }
  183. /**
  184. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  185. */
  186. ResourceHandle(std::shared_ptr<T> ptr, const String& uuid)
  187. :ResourceHandleBase()
  188. {
  189. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  190. _setHandleData(ptr, uuid);
  191. }
  192. };
  193. /**
  194. * @brief Casts one resource handle to another.
  195. */
  196. template<class _Ty1, class _Ty2>
  197. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  198. {
  199. return ResourceHandle<_Ty1>(other);
  200. }
  201. /**
  202. * @brief Checks if two handles point to the same resource.
  203. */
  204. template<class _Ty1, class _Ty2>
  205. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  206. {
  207. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  208. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  209. return _Left.getHandleData() == _Right.getHandleData();
  210. }
  211. template<class _Ty1, class _Ty2>
  212. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  213. {
  214. return (!(_Left == _Right));
  215. }
  216. }