BsResourceHandle.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsIReflectable.h"
  6. namespace BansheeEngine
  7. {
  8. template <typename T>
  9. class ResourceHandle;
  10. /**
  11. * @brief Data that is shared between all resource handles.
  12. */
  13. struct BS_CORE_EXPORT ResourceHandleData
  14. {
  15. ResourceHandleData()
  16. :mIsCreated(false)
  17. { }
  18. std::shared_ptr<Resource> mPtr;
  19. String mUUID;
  20. bool mIsCreated;
  21. };
  22. /**
  23. * @brief Base class containing common functionality for resource handles.
  24. */
  25. class BS_CORE_EXPORT ResourceHandleBase : public IReflectable
  26. {
  27. public:
  28. virtual ~ResourceHandleBase();
  29. /**
  30. * @brief Checks if the resource is loaded. Until resource is loaded this handle
  31. * is invalid and you may not get the internal resource from it.
  32. */
  33. bool isLoaded() const;
  34. /**
  35. * @brief Blocks the current thread until the resource is fully loaded AND initialized.
  36. *
  37. * @note Careful not to call this on the thread that does the loading or initializing.
  38. */
  39. void synchronize() const;
  40. /**
  41. * @brief Returns the UUID of the resource the handle is referring to.
  42. */
  43. const String& getUUID() const { return mData != nullptr ? mData->mUUID : StringUtil::BLANK; }
  44. /**
  45. * @brief Gets the handle data. For internal use only.
  46. */
  47. const std::shared_ptr<ResourceHandleData>& getHandleData() const { return mData; }
  48. /**
  49. * @brief Sets the created flag to true and assigns the resource pointer. Called
  50. * by the constructors, or if you constructed just using a UUID, then you need to
  51. * call this manually before you can access the resource from this handle.
  52. *
  53. * @note This is needed because two part construction is required due to
  54. * multithreaded nature of resource loading.
  55. * Internal method.
  56. */
  57. void _setHandleData(std::shared_ptr<Resource> ptr, const String& uuid);
  58. protected:
  59. ResourceHandleBase();
  60. std::shared_ptr<ResourceHandleData> mData;
  61. private:
  62. friend class Resources;
  63. BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  64. BS_STATIC_MUTEX(mResourceCreatedMutex)
  65. protected:
  66. inline void throwIfNotLoaded() const;
  67. /************************************************************************/
  68. /* RTTI */
  69. /************************************************************************/
  70. public:
  71. friend class ResourceHandleRTTI;
  72. static RTTITypeBase* getRTTIStatic();
  73. virtual RTTITypeBase* getRTTI() const;
  74. };
  75. /**
  76. * @brief Represents a handle to a resource. Handles are similar to a smart pointers, but they have two advantages:
  77. * - When loading a resource asynchronously you can be immediately returned the handle
  78. * that you may use throughout the engine. The handle will be made valid as soon as
  79. * the resource is loaded.
  80. * - Handles can be serialized and deserialized, therefore saving/restoring references
  81. * to their original resource.
  82. */
  83. template <typename T>
  84. class ResourceHandle : public ResourceHandleBase
  85. {
  86. public:
  87. ResourceHandle()
  88. :ResourceHandleBase()
  89. { }
  90. /**
  91. * @brief Constructs an invalid handle with the specified UUID. You must call _setHandleData
  92. * with the actual resource pointer to make the handle valid.
  93. */
  94. ResourceHandle(const String& uuid)
  95. :ResourceHandleBase()
  96. {
  97. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  98. mData->mUUID = uuid;
  99. }
  100. /**
  101. * @brief Copy constructor.
  102. */
  103. template <typename T1>
  104. ResourceHandle(const ResourceHandle<T1>& ptr)
  105. :ResourceHandleBase()
  106. {
  107. mData = ptr.getHandleData();
  108. }
  109. /**
  110. * @brief Converts a specific handle to generic Resource handle.
  111. */
  112. operator ResourceHandle<Resource>()
  113. {
  114. return ResourceHandle<Resource>(*this);
  115. }
  116. /**
  117. * @brief Returns internal resource pointer.
  118. *
  119. * @note Throws exception if handle is invalid.
  120. */
  121. T* operator->() const { return get(); }
  122. /**
  123. * @brief Returns internal resource pointer and dereferences it.
  124. *
  125. * @note Throws exception if handle is invalid.
  126. */
  127. T& operator*() const { return *get(); }
  128. /**
  129. * @brief Clears the handle making it invalid and releases any references
  130. * held to the resource.
  131. */
  132. ResourceHandle<T>& operator=(std::nullptr_t ptr)
  133. {
  134. mData = nullptr;
  135. return *this;
  136. }
  137. template<class _Ty>
  138. struct Bool_struct
  139. {
  140. int _Member;
  141. };
  142. /**
  143. * @brief Allows direct conversion of handle to bool.
  144. *
  145. * @note This is needed because we can't directly convert to bool
  146. * since then we can assign pointer to bool and that's weird.
  147. */
  148. operator int Bool_struct<T>::*() const
  149. {
  150. return ((mData != nullptr && mData->mPtr != nullptr) ? &Bool_struct<T>::_Member : 0);
  151. }
  152. /**
  153. * @brief Returns internal resource pointer and dereferences it.
  154. *
  155. * @note Throws exception if handle is invalid.
  156. */
  157. T* get() const
  158. {
  159. throwIfNotLoaded();
  160. return reinterpret_cast<T*>(mData->mPtr.get());
  161. }
  162. /**
  163. * @brief Returns the internal shared pointer to the resource.
  164. *
  165. * @note Throws exception if handle is invalid.
  166. */
  167. std::shared_ptr<T> getInternalPtr() const
  168. {
  169. throwIfNotLoaded();
  170. return std::static_pointer_cast<T>(mData->mPtr);
  171. }
  172. private:
  173. friend class Resources;
  174. /**
  175. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  176. *
  177. * @note Handle will take ownership of the provided resource pointer, so make sure you don't
  178. * delete it elsewhere.
  179. */
  180. explicit ResourceHandle(T* ptr, const String& uuid)
  181. :ResourceHandleBase()
  182. {
  183. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  184. _setHandleData(std::shared_ptr<Resource>(ptr, uuid));
  185. }
  186. /**
  187. * @brief Constructs a new valid handle for the provided resource with the provided UUID.
  188. */
  189. ResourceHandle(std::shared_ptr<T> ptr, const String& uuid)
  190. :ResourceHandleBase()
  191. {
  192. mData = bs_shared_ptr<ResourceHandleData, PoolAlloc>();
  193. _setHandleData(ptr, uuid);
  194. }
  195. };
  196. /**
  197. * @brief Casts one resource handle to another.
  198. */
  199. template<class _Ty1, class _Ty2>
  200. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  201. {
  202. return ResourceHandle<_Ty1>(other);
  203. }
  204. /**
  205. * @brief Checks if two handles point to the same resource.
  206. */
  207. template<class _Ty1, class _Ty2>
  208. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  209. {
  210. if(_Left.getHandleData() != nullptr && _Right.getHandleData() != nullptr)
  211. return _Left.getHandleData()->mPtr == _Right.getHandleData()->mPtr;
  212. return _Left.getHandleData() == _Right.getHandleData();
  213. }
  214. template<class _Ty1, class _Ty2>
  215. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  216. {
  217. return (!(_Left == _Right));
  218. }
  219. }