BsResourceHandle.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #pragma once
  2. #include "BsIReflectable.h"
  3. namespace BansheeEngine
  4. {
  5. /**
  6. * @brief Data that is shared between all resource handles.
  7. */
  8. struct BS_CORE_EXPORT ResourceHandleData
  9. {
  10. ResourceHandleData()
  11. :mIsCreated(false), mRefCount(0)
  12. { }
  13. SPtr<Resource> mPtr;
  14. String mUUID;
  15. bool mIsCreated;
  16. UINT32 mRefCount;
  17. };
  18. /**
  19. * @brief Base class containing common functionality for resource handles.
  20. */
  21. class BS_CORE_EXPORT ResourceHandleBase : public IReflectable
  22. {
  23. public:
  24. virtual ~ResourceHandleBase();
  25. /**
  26. * @brief Checks if the resource is loaded. Until resource is loaded this handle
  27. * is invalid and you may not get the internal resource from it.
  28. *
  29. * @param checkDependencies If true, and if resource has any dependencies, this method will
  30. * also check if they are loaded.
  31. */
  32. bool isLoaded(bool checkDependencies = true) const;
  33. /**
  34. * @brief Blocks the current thread until the resource is fully loaded.
  35. *
  36. * @note Careful not to call this on the thread that does the loading.
  37. */
  38. void blockUntilLoaded(bool waitForDependencies = true) const;
  39. /**
  40. * @brief Releases an internal reference to this resource held by the resources system, if there is one.
  41. *
  42. * @see Resources::release(ResourceHandleBase&)
  43. */
  44. void release();
  45. /**
  46. * @brief Returns the UUID of the resource the handle is referring to.
  47. */
  48. const String& getUUID() const { return mData != nullptr ? mData->mUUID : StringUtil::BLANK; }
  49. /**
  50. * @brief Gets the handle data. For internal use only.
  51. */
  52. const SPtr<ResourceHandleData>& getHandleData() const { return mData; }
  53. protected:
  54. ResourceHandleBase();
  55. /**
  56. * @brief Destroys the resource the handle is pointing to.
  57. */
  58. void destroy();
  59. /**
  60. * @brief Sets the created flag to true and assigns the resource pointer. Called
  61. * by the constructors, or if you constructed just using a UUID, then you need to
  62. * call this manually before you can access the resource from this handle.
  63. *
  64. * @note This is needed because two part construction is required due to
  65. * multithreaded nature of resource loading.
  66. * Internal method.
  67. */
  68. void setHandleData(const SPtr<Resource>& ptr, const String& uuid);
  69. /**
  70. * @brief Increments the reference count of the handle. Only to be used by ::Resources for keeping
  71. * internal references.
  72. */
  73. void addInternalRef();
  74. /**
  75. * @brief Decrements the reference count of the handle. Only to be used by ::Resources for keeping
  76. * internal references.
  77. */
  78. void removeInternalRef();
  79. /** @note All handles to the same source must share this same handle data. Otherwise things
  80. * like counting number of references or replacing pointed to resource become impossible
  81. * without additional logic. */
  82. SPtr<ResourceHandleData> mData;
  83. private:
  84. friend class Resources;
  85. BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  86. BS_STATIC_MUTEX(mResourceCreatedMutex)
  87. protected:
  88. inline void throwIfNotLoaded() const;
  89. };
  90. /**
  91. * @copydoc ResourceHandleBase
  92. *
  93. * Handles differences in reference counting depending if the handle is normal or weak.
  94. */
  95. template <bool WeakHandle>
  96. class BS_CORE_EXPORT TResourceHandleBase : public ResourceHandleBase { };
  97. /**
  98. * @brief Specialization of TResourceHandleBase for weak handles. Weak handles do no reference counting.
  99. */
  100. template<>
  101. class BS_CORE_EXPORT TResourceHandleBase<true> : public ResourceHandleBase
  102. {
  103. public:
  104. virtual ~TResourceHandleBase() { }
  105. protected:
  106. void addRef() { };
  107. void releaseRef() { };
  108. /************************************************************************/
  109. /* RTTI */
  110. /************************************************************************/
  111. public:
  112. friend class WeakResourceHandleRTTI;
  113. static RTTITypeBase* getRTTIStatic();
  114. virtual RTTITypeBase* getRTTI() const override;
  115. };
  116. /**
  117. * @brief Specialization of TResourceHandleBase for normal (non-weak) handles.
  118. */
  119. template<>
  120. class BS_CORE_EXPORT TResourceHandleBase<false> : public ResourceHandleBase
  121. {
  122. public:
  123. virtual ~TResourceHandleBase() { }
  124. protected:
  125. void addRef() { if (mData) mData->mRefCount++; };
  126. void releaseRef()
  127. {
  128. if (mData)
  129. {
  130. mData->mRefCount--;
  131. if (mData->mRefCount == 0)
  132. destroy();
  133. }
  134. };
  135. /************************************************************************/
  136. /* RTTI */
  137. /************************************************************************/
  138. public:
  139. friend class WeakResourceHandleRTTI;
  140. friend class ResourceHandleRTTI;
  141. static RTTITypeBase* getRTTIStatic();
  142. virtual RTTITypeBase* getRTTI() const override;
  143. };
  144. /**
  145. * @brief Represents a handle to a resource. Handles are similar to a smart pointers, but they have two advantages:
  146. * - When loading a resource asynchronously you can be immediately returned the handle
  147. * that you may use throughout the engine. The handle will be made valid as soon as
  148. * the resource is loaded.
  149. * - Handles can be serialized and deserialized, therefore saving/restoring references
  150. * to their original resource.
  151. */
  152. template <typename T, bool WeakHandle>
  153. class TResourceHandle : public TResourceHandleBase<WeakHandle>
  154. {
  155. public:
  156. TResourceHandle()
  157. :TResourceHandleBase()
  158. { }
  159. /**
  160. * @brief Copy constructor.
  161. */
  162. TResourceHandle(const TResourceHandle<T, WeakHandle>& ptr)
  163. :TResourceHandleBase()
  164. {
  165. mData = ptr.getHandleData();
  166. addRef();
  167. }
  168. virtual ~TResourceHandle()
  169. {
  170. releaseRef();
  171. }
  172. /**
  173. * @brief Converts a specific handle to generic Resource handle.
  174. */
  175. operator TResourceHandle<Resource, WeakHandle>() const
  176. {
  177. TResourceHandle<Resource, WeakHandle> handle;
  178. handle.setHandleData(getHandleData());
  179. return handle;
  180. }
  181. /**
  182. * @brief Returns internal resource pointer.
  183. *
  184. * @note Throws exception if handle is invalid.
  185. */
  186. T* operator->() const { return get(); }
  187. /**
  188. * @brief Returns internal resource pointer and dereferences it.
  189. *
  190. * @note Throws exception if handle is invalid.
  191. */
  192. T& operator*() const { return *get(); }
  193. /**
  194. * @brief Clears the handle making it invalid and releases any references
  195. * held to the resource.
  196. */
  197. TResourceHandle<T, WeakHandle>& operator=(std::nullptr_t ptr)
  198. {
  199. releaseRef();
  200. mData = nullptr;
  201. return *this;
  202. }
  203. /**
  204. * @brief Normal assignment operator.
  205. */
  206. TResourceHandle<T, WeakHandle>& operator=(const TResourceHandle<T, WeakHandle>& rhs)
  207. {
  208. setHandleData(rhs.getHandleData());
  209. return *this;
  210. }
  211. template<class _Ty>
  212. struct Bool_struct
  213. {
  214. int _Member;
  215. };
  216. /**
  217. * @brief Allows direct conversion of handle to bool.
  218. *
  219. * @note This is needed because we can't directly convert to bool
  220. * since then we can assign pointer to bool and that's weird.
  221. */
  222. operator int Bool_struct<T>::*() const
  223. {
  224. return ((mData != nullptr && !mData->mUUID.empty()) ? &Bool_struct<T>::_Member : 0);
  225. }
  226. /**
  227. * @brief Returns internal resource pointer and dereferences it.
  228. *
  229. * @note Throws exception if handle is invalid.
  230. */
  231. T* get() const
  232. {
  233. throwIfNotLoaded();
  234. return reinterpret_cast<T*>(mData->mPtr.get());
  235. }
  236. /**
  237. * @brief Returns the internal shared pointer to the resource.
  238. *
  239. * @note Throws exception if handle is invalid.
  240. */
  241. SPtr<T> getInternalPtr() const
  242. {
  243. throwIfNotLoaded();
  244. return std::static_pointer_cast<T>(mData->mPtr);
  245. }
  246. /**
  247. * @brief Converts a handle into a weak handle.
  248. */
  249. TResourceHandle<T, true> getWeak() const
  250. {
  251. TResourceHandle<T, true> handle;
  252. handle.setHandleData(getHandleData());
  253. return handle;
  254. }
  255. protected:
  256. friend Resources;
  257. template<class _T, bool _Weak>
  258. friend class TResourceHandle;
  259. template<class _Ty1, class _Ty2, bool Weak>
  260. friend TResourceHandle<_Ty1, Weak> static_resource_cast(const TResourceHandle<_Ty2, Weak>& other);
  261. /**
  262. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  263. *
  264. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  265. * delete it elsewhere.
  266. */
  267. explicit TResourceHandle(T* ptr, const String& uuid)
  268. :TResourceHandleBase()
  269. {
  270. mData = bs_shared_ptr_new<ResourceHandleData>();
  271. addRef();
  272. setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  273. }
  274. /**
  275. * @brief Constructs an invalid handle with the specified UUID. You must call setHandleData
  276. * with the actual resource pointer to make the handle valid.
  277. */
  278. TResourceHandle(const String& uuid)
  279. :TResourceHandleBase()
  280. {
  281. mData = bs_shared_ptr_new<ResourceHandleData>();
  282. mData->mUUID = uuid;
  283. addRef();
  284. }
  285. /**
  286. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  287. */
  288. TResourceHandle(const SPtr<T> ptr, const String& uuid)
  289. :TResourceHandleBase()
  290. {
  291. mData = bs_shared_ptr_new<ResourceHandleData>();
  292. addRef();
  293. setHandleData(ptr, uuid);
  294. }
  295. /**
  296. * @brief Replaces the internal handle data pointer, effectively transforming the handle into a different handle.
  297. */
  298. void setHandleData(const SPtr<ResourceHandleData>& data)
  299. {
  300. releaseRef();
  301. mData = data;
  302. addRef();
  303. }
  304. /**
  305. * @brief Converts a weak handle into a normal handle.
  306. */
  307. TResourceHandle<T, false> lock() const
  308. {
  309. TResourceHandle<Resource, false> handle;
  310. handle.setHandleData(getHandleData());
  311. return handle;
  312. }
  313. using TResourceHandleBase::setHandleData;
  314. };
  315. template <typename T>
  316. using ResourceHandle = TResourceHandle<T, false>;
  317. template <typename T>
  318. using WeakResourceHandle = TResourceHandle<T, true>;
  319. /**
  320. * @brief Casts one resource handle to another.
  321. */
  322. template<class _Ty1, class _Ty2, bool Weak>
  323. TResourceHandle<_Ty1, Weak> static_resource_cast(const TResourceHandle<_Ty2, Weak>& other)
  324. {
  325. TResourceHandle<_Ty1, Weak> handle;
  326. handle.setHandleData(other.getHandleData());
  327. return handle;
  328. }
  329. /**
  330. * @brief Checks if two handles point to the same resource.
  331. */
  332. template<class _Ty1, bool _Weak1, class _Ty2, bool _Weak2>
  333. bool operator==(const TResourceHandle<_Ty1, _Weak1>& _Left, const TResourceHandle<_Ty2, _Weak2>& _Right)
  334. {
  335. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  336. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  337. return _Left.getHandleData() == _Right.getHandleData();
  338. }
  339. /**
  340. * @brief Checks if a handle is null.
  341. */
  342. template<class _Ty1, bool _Weak1, class _Ty2, bool _Weak2>
  343. bool operator==(const TResourceHandle<_Ty1, _Weak1>& _Left, std::nullptr_t _Right)
  344. {
  345. return _Left.getHandleData() == nullptr || _Left.getHandleData()->mUUID.empty();
  346. }
  347. template<class _Ty1, bool _Weak1, class _Ty2, bool _Weak2>
  348. bool operator!=(const TResourceHandle<_Ty1, _Weak1>& _Left, const TResourceHandle<_Ty2, _Weak2>& _Right)
  349. {
  350. return (!(_Left == _Right));
  351. }
  352. }