ResourceObject.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2009-2021, 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. // Forward
  12. class XmlDocument;
  13. /// @addtogroup resource
  14. /// @{
  15. /// The base of all resource objects.
  16. class ResourceObject
  17. {
  18. friend class ResourceManager;
  19. public:
  20. ResourceObject(ResourceManager* manager);
  21. virtual ~ResourceObject();
  22. /// @privatesection
  23. ResourceManager& getManager() const
  24. {
  25. return *m_manager;
  26. }
  27. ResourceAllocator<U8> getAllocator() const;
  28. TempResourceAllocator<U8> getTempAllocator() const;
  29. Atomic<I32>& getRefcount()
  30. {
  31. return m_refcount;
  32. }
  33. const Atomic<I32>& getRefcount() const
  34. {
  35. return m_refcount;
  36. }
  37. CString getFilename() const
  38. {
  39. ANKI_ASSERT(!m_fname.isEmpty());
  40. return m_fname.toCString();
  41. }
  42. // Internals:
  43. ANKI_INTERNAL void setFilename(const CString& fname)
  44. {
  45. ANKI_ASSERT(m_fname.isEmpty());
  46. m_fname.create(getAllocator(), fname);
  47. }
  48. ANKI_INTERNAL void setUuid(U64 uuid)
  49. {
  50. ANKI_ASSERT(uuid > 0);
  51. m_uuid = uuid;
  52. }
  53. /// To check if 2 resource pointers are actually the same resource.
  54. ANKI_INTERNAL U64 getUuid() const
  55. {
  56. ANKI_ASSERT(m_uuid > 0);
  57. return m_uuid;
  58. }
  59. ANKI_INTERNAL ANKI_USE_RESULT Error openFile(const ResourceFilename& filename, ResourceFilePtr& file);
  60. ANKI_INTERNAL ANKI_USE_RESULT Error openFileReadAllText(const ResourceFilename& filename, StringAuto& file);
  61. ANKI_INTERNAL ANKI_USE_RESULT Error openFileParseXml(const ResourceFilename& filename, XmlDocument& xml);
  62. private:
  63. ResourceManager* m_manager;
  64. Atomic<I32> m_refcount;
  65. String m_fname; ///< Unique resource name.
  66. U64 m_uuid = 0;
  67. };
  68. /// @}
  69. } // end namespace anki