2
0

Resource.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. #include "../Core/Timer.h"
  25. namespace Urho3D
  26. {
  27. class Deserializer;
  28. class Serializer;
  29. /// Asynchronous loading state of a resource.
  30. enum AsyncLoadState
  31. {
  32. /// No async operation in progress.
  33. ASYNC_DONE = 0,
  34. /// Queued for asynchronous loading.
  35. ASYNC_QUEUED = 1,
  36. /// In progress of calling BeginLoad() in a worker thread.
  37. ASYNC_LOADING = 2,
  38. /// BeginLoad() succeeded. EndLoad() can be called in the main thread.
  39. ASYNC_SUCCESS = 3,
  40. /// BeginLoad() failed.
  41. ASYNC_FAIL = 4
  42. };
  43. /// Base class for resources.
  44. class URHO3D_API Resource : public Object
  45. {
  46. URHO3D_OBJECT(Resource, Object);
  47. public:
  48. /// Construct.
  49. Resource(Context* context);
  50. /// Load resource synchronously. Call both BeginLoad() & EndLoad() and return true if both succeeded.
  51. bool Load(Deserializer& source);
  52. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  53. virtual bool BeginLoad(Deserializer& source);
  54. /// Finish resource loading. Always called from the main thread. Return true if successful.
  55. virtual bool EndLoad();
  56. /// Save resource. Return true if successful.
  57. virtual bool Save(Serializer& dest) const;
  58. /// Load resource from file.
  59. bool LoadFile(const String& fileName);
  60. /// Save resource to file.
  61. virtual bool SaveFile(const String& fileName) const;
  62. /// Set name.
  63. void SetName(const String& name);
  64. /// Set memory use in bytes, possibly approximate.
  65. void SetMemoryUse(unsigned size);
  66. /// Reset last used timer.
  67. void ResetUseTimer();
  68. /// Set the asynchronous loading state. Called by ResourceCache. Resources in the middle of asynchronous loading are not normally returned to user.
  69. void SetAsyncLoadState(AsyncLoadState newState);
  70. /// Return name.
  71. const String& GetName() const { return name_; }
  72. /// Return name hash.
  73. StringHash GetNameHash() const { return nameHash_; }
  74. /// Return memory use in bytes, possibly approximate.
  75. unsigned GetMemoryUse() const { return memoryUse_; }
  76. /// Return time since last use in milliseconds. If referred to elsewhere than in the resource cache, returns always zero.
  77. unsigned GetUseTimer();
  78. /// Return the asynchronous loading state.
  79. AsyncLoadState GetAsyncLoadState() const { return asyncLoadState_; }
  80. private:
  81. /// Name.
  82. String name_;
  83. /// Name hash.
  84. StringHash nameHash_;
  85. /// Last used timer.
  86. Timer useTimer_;
  87. /// Memory use in bytes.
  88. unsigned memoryUse_;
  89. /// Asynchronous loading state.
  90. AsyncLoadState asyncLoadState_;
  91. };
  92. inline const String& GetResourceName(Resource* resource)
  93. {
  94. return resource ? resource->GetName() : String::EMPTY;
  95. }
  96. inline StringHash GetResourceType(Resource* resource, StringHash defaultType)
  97. {
  98. return resource ? resource->GetType() : defaultType;
  99. }
  100. inline ResourceRef GetResourceRef(Resource* resource, StringHash defaultType)
  101. {
  102. return ResourceRef(GetResourceType(resource, defaultType), GetResourceName(resource));
  103. }
  104. template <class T> Vector<String> GetResourceNames(const Vector<SharedPtr<T> >& resources)
  105. {
  106. Vector<String> ret(resources.Size());
  107. for (unsigned i = 0; i < resources.Size(); ++i)
  108. ret[i] = GetResourceName(resources[i]);
  109. return ret;
  110. }
  111. template <class T> ResourceRefList GetResourceRefList(const Vector<SharedPtr<T> >& resources)
  112. {
  113. return ResourceRefList(T::GetTypeStatic(), GetResourceNames(resources));
  114. }
  115. }