BsResourceHandle.h 11 KB

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