CmResourceRef.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #include "CmIReflectable.h"
  3. template<class _Ty>
  4. struct CM_Bool_struct
  5. {
  6. int _Member;
  7. };
  8. namespace CamelotEngine
  9. {
  10. template <typename T>
  11. class ResourceRef;
  12. struct CM_EXPORT ResourceRefData : public IReflectable
  13. {
  14. ResourceRefData()
  15. :mIsResolved(false)
  16. { }
  17. std::shared_ptr<Resource> mPtr;
  18. String mUUID;
  19. bool mIsResolved;
  20. /************************************************************************/
  21. /* RTTI */
  22. /************************************************************************/
  23. public:
  24. friend class ResourceRefDataRTTI;
  25. static RTTITypeBase* getRTTIStatic();
  26. virtual RTTITypeBase* getRTTI() const;
  27. };
  28. class CM_EXPORT ResourceRefBase : public IReflectable
  29. {
  30. public:
  31. /**
  32. * @brief Checks if the resource is loaded
  33. */
  34. bool isResolved() const { return mData->mIsResolved; }
  35. protected:
  36. ResourceRefBase();
  37. std::shared_ptr<ResourceRefData> mData;
  38. void init(Resource* ptr);
  39. void init(std::shared_ptr<Resource> ptr);
  40. template <typename T1>
  41. void init(const ResourceRef<T1>& ptr)
  42. {
  43. mData = ptr.mData;
  44. }
  45. private:
  46. friend class Resources;
  47. /**
  48. * @brief Sets the resolved flag to true. Should only be called
  49. * by Resources class after loading of the resource is fully done.
  50. */
  51. void resolve(std::shared_ptr<Resource> ptr);
  52. /**
  53. * @brief Sets an uuid of the ResourceRef. Should only be called by
  54. * Resources class.
  55. */
  56. void ResourceRefBase::setUUID(const String& uuid);
  57. /************************************************************************/
  58. /* RTTI */
  59. /************************************************************************/
  60. public:
  61. friend class ResourceRefRTTI;
  62. static RTTITypeBase* getRTTIStatic();
  63. virtual RTTITypeBase* getRTTI() const;
  64. };
  65. template <typename T>
  66. class ResourceRef : public ResourceRefBase
  67. {
  68. public:
  69. ResourceRef()
  70. :ResourceRefBase()
  71. { }
  72. ResourceRef(T* ptr)
  73. :ResourceRefBase()
  74. {
  75. init(ptr);
  76. }
  77. ResourceRef(std::shared_ptr<T> ptr)
  78. :ResourceRefBase()
  79. {
  80. init(ptr);
  81. }
  82. template <typename T1>
  83. ResourceRef(const ResourceRef<T1>& ptr)
  84. :ResourceRefBase()
  85. {
  86. init(ptr);
  87. }
  88. operator ResourceRef<Resource>()
  89. {
  90. return ResourceRef<Resource>(mData->mPtr);
  91. }
  92. // TODO Low priority - User can currently try to access these even if resource ptr is not resolved
  93. T* get() const
  94. {
  95. if(!isResolved())
  96. return nullptr;
  97. return static_cast<T*>(mData->mPtr.get());
  98. }
  99. T* operator->() const { return get(); }
  100. T& operator*() const { return *get(); }
  101. std::shared_ptr<T> getInternalPtr() { if(!isResolved()) return nullptr; return std::static_pointer_cast<T>(mData->mPtr); }
  102. // Conversion to bool
  103. // (Why not just directly convert to bool? Because then we can assign pointer to bool and that's weird)
  104. operator int CM_Bool_struct<T>::*() const
  105. {
  106. return ((isResolved() && (mData->mPtr.get() != 0)) ? &CM_Bool_struct<T>::_Member : 0);
  107. }
  108. };
  109. template<class _Ty1, class _Ty2>
  110. ResourceRef<_Ty1> static_resource_cast(const ResourceRef<_Ty2>& other)
  111. {
  112. return ResourceRef<_Ty1>(other);
  113. }
  114. template<class _Ty1, class _Ty2>
  115. bool operator==(const ResourceRef<_Ty1>& _Left, const ResourceRef<_Ty2>& _Right)
  116. {
  117. return (_Left.get() == _Right.get());
  118. }
  119. template<class _Ty1, class _Ty2>
  120. bool operator!=(const ResourceRef<_Ty1>& _Left, const ResourceRef<_Ty2>& _Right)
  121. {
  122. return (!(_Left == _Right));
  123. }
  124. }