CmResourceHandle.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include "CmIReflectable.h"
  3. namespace CamelotFramework
  4. {
  5. template <typename T>
  6. class ResourceHandle;
  7. struct CM_EXPORT ResourceHandleData
  8. {
  9. ResourceHandleData()
  10. :mIsCreated(false)
  11. { }
  12. std::shared_ptr<Resource> mPtr;
  13. String mUUID;
  14. bool mIsCreated;
  15. };
  16. class CM_EXPORT ResourceHandleBase : public IReflectable
  17. {
  18. public:
  19. /**
  20. * @brief Checks if the resource is loaded
  21. */
  22. bool isLoaded() const;
  23. /**
  24. * @brief Blocks the current thread until the resource is fully loaded.
  25. *
  26. * @note Careful not to call this on the thread that does the loading.
  27. */
  28. void synchronize() const;
  29. /**
  30. * @brief Returns the UUID of the resource the handle is referring to.
  31. */
  32. const String& getUUID() const { return mData->mUUID; }
  33. /**
  34. * @brief Gets the handle data. For internal use only.
  35. */
  36. std::shared_ptr<ResourceHandleData> getHandleData() const { return mData; }
  37. protected:
  38. ResourceHandleBase();
  39. std::shared_ptr<ResourceHandleData> mData;
  40. void init(Resource* ptr);
  41. void init(std::shared_ptr<Resource> ptr);
  42. template <typename T1>
  43. void init(const ResourceHandle<T1>& ptr)
  44. {
  45. mData = ptr.mData;
  46. }
  47. private:
  48. friend class Resources;
  49. CM_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
  50. CM_STATIC_MUTEX(mResourceCreatedMutex)
  51. /**
  52. * @brief Sets the created flag to true. Should only be called
  53. * by Resources class after loading of the resource is fully done.
  54. */
  55. void resolve(std::shared_ptr<Resource> ptr);
  56. /**
  57. * @brief Sets an uuid of the ResourceHandle. Should only be called by
  58. * Resources class.
  59. */
  60. void ResourceHandleBase::setUUID(const String& uuid);
  61. protected:
  62. inline void throwIfNotLoaded() const;
  63. /************************************************************************/
  64. /* RTTI */
  65. /************************************************************************/
  66. public:
  67. friend class ResourceHandleRTTI;
  68. static RTTITypeBase* getRTTIStatic();
  69. virtual RTTITypeBase* getRTTI() const;
  70. };
  71. template <typename T>
  72. class ResourceHandle : public ResourceHandleBase
  73. {
  74. public:
  75. ResourceHandle()
  76. :ResourceHandleBase()
  77. { }
  78. template <typename T1>
  79. ResourceHandle(const ResourceHandle<T1>& ptr)
  80. :ResourceHandleBase()
  81. {
  82. mData = ptr.getHandleData();
  83. }
  84. operator ResourceHandle<Resource>()
  85. {
  86. return ResourceHandle<Resource>(*this);
  87. }
  88. // TODO Low priority - User can currently try to access these even if resource ptr is not resolved
  89. T* get() const
  90. {
  91. throwIfNotLoaded();
  92. return reinterpret_cast<T*>(mData->mPtr.get());
  93. }
  94. T* operator->() const { return get(); }
  95. T& operator*() const { return *get(); }
  96. std::shared_ptr<T> getInternalPtr() const
  97. {
  98. throwIfNotLoaded();
  99. return std::static_pointer_cast<T>(mData->mPtr);
  100. }
  101. /**
  102. * @brief Releases the reference held by this handle.
  103. */
  104. void reset()
  105. {
  106. mData = cm_shared_ptr<ResourceHandleData, ScratchAlloc>();
  107. }
  108. template<class _Ty>
  109. struct CM_Bool_struct
  110. {
  111. int _Member;
  112. };
  113. // Conversion to bool
  114. // (Why not just directly convert to bool? Because then we can assign pointer to bool and that's weird)
  115. operator int CM_Bool_struct<T>::*() const
  116. {
  117. return (((mData->mPtr != nullptr)) ? &CM_Bool_struct<T>::_Member : 0);
  118. }
  119. private:
  120. friend class Resource;
  121. explicit ResourceHandle(T* ptr)
  122. :ResourceHandleBase()
  123. {
  124. init(ptr);
  125. }
  126. ResourceHandle(std::shared_ptr<T> ptr)
  127. :ResourceHandleBase()
  128. {
  129. init(ptr);
  130. }
  131. };
  132. template<class _Ty1, class _Ty2>
  133. ResourceHandle<_Ty1> static_resource_cast(const ResourceHandle<_Ty2>& other)
  134. {
  135. return ResourceHandle<_Ty1>(other);
  136. }
  137. template<class _Ty1, class _Ty2>
  138. bool operator==(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  139. {
  140. return (_Left.get() == _Right.get());
  141. }
  142. template<class _Ty1, class _Ty2>
  143. bool operator!=(const ResourceHandle<_Ty1>& _Left, const ResourceHandle<_Ty2>& _Right)
  144. {
  145. return (!(_Left == _Right));
  146. }
  147. }