CmResourceHandle.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "CmPrerequisites.h"
  2. #include "CmResourceHandle.h"
  3. #include "CmResource.h"
  4. #include "CmResourceHandleRTTI.h"
  5. #include "CmResources.h"
  6. namespace CamelotEngine
  7. {
  8. CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mResourceCreatedCondition, ResourceHandleBase)
  9. CM_STATIC_MUTEX_CLASS_INSTANCE(mResourceCreatedMutex, ResourceHandleBase)
  10. RTTITypeBase* ResourceHandleData::getRTTIStatic()
  11. {
  12. return ResourceHandleDataRTTI::instance();
  13. }
  14. RTTITypeBase* ResourceHandleData::getRTTI() const
  15. {
  16. return ResourceHandleData::getRTTIStatic();
  17. }
  18. ResourceHandleBase::ResourceHandleBase()
  19. {
  20. mData = std::shared_ptr<ResourceHandleData>(new ResourceHandleData());
  21. }
  22. bool ResourceHandleBase::isLoaded() const
  23. {
  24. return (mData->mIsCreated && mData->mPtr != nullptr && mData->mPtr->isInitialized());
  25. }
  26. void ResourceHandleBase::waitUntilLoaded() const
  27. {
  28. if(!mData->mIsCreated)
  29. {
  30. CM_LOCK_MUTEX_NAMED(mResourceCreatedMutex, lock);
  31. while(!mData->mIsCreated)
  32. {
  33. CM_THREAD_WAIT(mResourceCreatedCondition, mResourceCreatedMutex, lock);
  34. }
  35. }
  36. mData->mPtr->waitUntilInitialized();
  37. }
  38. void ResourceHandleBase::resolve(std::shared_ptr<Resource> ptr)
  39. {
  40. init(ptr);
  41. }
  42. void ResourceHandleBase::setUUID(const String& uuid)
  43. {
  44. mData->mUUID = uuid;
  45. }
  46. void ResourceHandleBase::init(Resource* ptr)
  47. {
  48. init(std::shared_ptr<Resource>(ptr));
  49. }
  50. void ResourceHandleBase::init(std::shared_ptr<Resource> ptr)
  51. {
  52. mData->mPtr = ptr;
  53. if(mData->mPtr)
  54. {
  55. mData->mUUID = mData->mPtr->getUUID();
  56. if(!mData->mIsCreated)
  57. {
  58. CM_LOCK_MUTEX(mResourceCreatedMutex);
  59. {
  60. mData->mIsCreated = true;
  61. }
  62. CM_THREAD_NOTIFY_ALL(mResourceCreatedCondition);
  63. }
  64. }
  65. }
  66. void ResourceHandleBase::throwIfNotLoaded() const
  67. {
  68. if(!isLoaded())
  69. {
  70. CM_EXCEPT(InternalErrorException, "Trying to access a resource that hasn't been loaded yet.");
  71. }
  72. }
  73. RTTITypeBase* ResourceHandleBase::getRTTIStatic()
  74. {
  75. return ResourceHandleRTTI::instance();
  76. }
  77. RTTITypeBase* ResourceHandleBase::getRTTI() const
  78. {
  79. return ResourceHandleBase::getRTTIStatic();
  80. }
  81. }