CmResourceRef.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /* RTTI */
  54. /************************************************************************/
  55. public:
  56. friend class ResourceRefRTTI;
  57. static RTTITypeBase* getRTTIStatic();
  58. virtual RTTITypeBase* getRTTI() const;
  59. };
  60. template <typename T>
  61. class ResourceRef : public ResourceRefBase
  62. {
  63. public:
  64. ResourceRef()
  65. :ResourceRefBase()
  66. { }
  67. ResourceRef(T* ptr)
  68. :ResourceRefBase()
  69. {
  70. init(ptr);
  71. }
  72. ResourceRef(std::shared_ptr<T> ptr)
  73. :ResourceRefBase()
  74. {
  75. init(ptr);
  76. }
  77. template <typename T1>
  78. ResourceRef(const ResourceRef<T1>& ptr)
  79. :ResourceRefBase()
  80. {
  81. init(ptr);
  82. }
  83. operator ResourceRef<Resource>()
  84. {
  85. return ResourceRef<Resource>(mData->mPtr);
  86. }
  87. // TODO Low priority - User can currently try to access these even if resource ptr is not resolved
  88. T* get() const { if(!isResolved()) return nullptr; return static_cast<T*>(mData->mPtr.get()); }
  89. T* operator->() const { return get(); }
  90. T& operator*() const { return *get(); }
  91. std::shared_ptr<T> getInternalPtr() { if(!isResolved()) return nullptr; return std::static_pointer_cast<T>(mData->mPtr); }
  92. // Conversion to bool
  93. // (Why not just directly convert to bool? Because then we can assign pointer to bool and that's weird)
  94. operator int CM_Bool_struct<T>::*() const
  95. {
  96. return ((isResolved() && (mData->mPtr.get() != 0)) ? &CM_Bool_struct<T>::_Member : 0);
  97. }
  98. };
  99. template<class _Ty1, class _Ty2>
  100. ResourceRef<_Ty1> static_resource_cast(const ResourceRef<_Ty2>& other)
  101. {
  102. return ResourceRef<_Ty1>(other);
  103. }
  104. template<class _Ty1, class _Ty2>
  105. bool operator==(const ResourceRef<_Ty1>& _Left, const ResourceRef<_Ty2>& _Right)
  106. {
  107. return (_Left.get() == _Right.get());
  108. }
  109. template<class _Ty1, class _Ty2>
  110. bool operator!=(const ResourceRef<_Ty1>& _Left, const ResourceRef<_Ty2>& _Right)
  111. {
  112. return (!(_Left == _Right));
  113. }
  114. }