ResourceObject.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/Common.h>
  7. #include <AnKi/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Util/Atomic.h>
  9. #include <AnKi/Util/String.h>
  10. namespace anki {
  11. // The base of all resource objects.
  12. class ResourceObject
  13. {
  14. friend class ResourceManager;
  15. template<typename>
  16. friend class ResourcePtrDeleter;
  17. public:
  18. ResourceObject(CString fname, U32 uuid)
  19. : m_uuid(uuid)
  20. , m_fname(fname)
  21. {
  22. ANKI_ASSERT(uuid > 0);
  23. ANKI_ASSERT(!fname.isEmpty());
  24. }
  25. virtual ~ResourceObject() = default;
  26. void retain() const
  27. {
  28. m_refcount.fetchAdd(1);
  29. }
  30. I32 release() const
  31. {
  32. return m_refcount.fetchSub(1);
  33. }
  34. CString getFilename() const
  35. {
  36. ANKI_ASSERT(!m_fname.isEmpty());
  37. return m_fname.toCString();
  38. }
  39. // To check if 2 resource pointers are actually the same resource.
  40. U32 getUuid() const
  41. {
  42. ANKI_ASSERT(m_uuid > 0);
  43. return m_uuid;
  44. }
  45. I32 getRefcount() const
  46. {
  47. return m_refcount.load();
  48. }
  49. // If true the resource has changed in the filesystem and this one is an obsolete version
  50. Bool isObsolete() const
  51. {
  52. return m_isObsolete.load() != 0;
  53. }
  54. protected:
  55. Error openFile(const ResourceFilename& filename, ResourceFilePtr& file);
  56. Error openFileReadAllText(const ResourceFilename& filename, ResourceString& file);
  57. Error openFileParseXml(const ResourceFilename& filename, ResourceXmlDocument& xml);
  58. private:
  59. mutable Atomic<I32> m_refcount = {0};
  60. mutable Atomic<U32> m_isObsolete = {0}; // If the file of the resource changed in the filesystem then this flag is 1
  61. U32 m_uuid = 0;
  62. ResourceString m_fname; // Unique resource name
  63. };
  64. } // end namespace anki