Resource.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Copyright (c) 2008-2013 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 "Object.h"
  24. #include "Timer.h"
  25. namespace Urho3D
  26. {
  27. class Deserializer;
  28. class Serializer;
  29. /// Base class for resources.
  30. class Resource : public Object
  31. {
  32. OBJECT(Resource);
  33. public:
  34. /// Construct.
  35. Resource(Context* context);
  36. /// Load resource. Return true if successful.
  37. virtual bool Load(Deserializer& source);
  38. /// Save resource. Return true if successful.
  39. virtual bool Save(Serializer& dest);
  40. /// Set name.
  41. void SetName(const String& name);
  42. /// Set memory use in bytes, possibly approximate.
  43. void SetMemoryUse(unsigned size);
  44. /// Reset last used timer.
  45. void ResetUseTimer();
  46. /// Return name.
  47. const String& GetName() const { return name_; }
  48. /// Return name hash.
  49. StringHash GetNameHash() const { return nameHash_; }
  50. /// Return memory use in bytes, possibly approximate.
  51. unsigned GetMemoryUse() const { return memoryUse_; }
  52. /// Return time since last use in milliseconds. If referred to elsewhere than in the resource cache, returns always zero.
  53. unsigned GetUseTimer();
  54. private:
  55. /// Name.
  56. String name_;
  57. /// Name hash.
  58. StringHash nameHash_;
  59. /// Last used timer.
  60. Timer useTimer_;
  61. /// Memory use in bytes.
  62. unsigned memoryUse_;
  63. };
  64. inline StringHash GetResourceHash(Resource* resource)
  65. {
  66. return resource ? resource->GetNameHash() : StringHash();
  67. }
  68. inline String GetResourceName(Resource* resource)
  69. {
  70. return resource ? resource->GetName() : String();
  71. }
  72. inline ShortStringHash GetResourceType(Resource* resource, ShortStringHash defaultType)
  73. {
  74. return resource ? resource->GetType() : defaultType;
  75. }
  76. inline ResourceRef GetResourceRef(Resource* resource, ShortStringHash defaultType)
  77. {
  78. return ResourceRef(GetResourceType(resource, defaultType), GetResourceHash(resource));
  79. }
  80. template <class T> Vector<StringHash> GetResourceHashes(const Vector<SharedPtr<T> >& resources)
  81. {
  82. Vector<StringHash> ret(resources.Size());
  83. for (unsigned i = 0; i < resources.Size(); ++i)
  84. ret[i] = GetResourceHash(resources[i]);
  85. return ret;
  86. }
  87. template <class T> ResourceRefList GetResourceRefList(const Vector<SharedPtr<T> >& resources)
  88. {
  89. return ResourceRefList(T::GetTypeStatic(), GetResourceHashes(resources));
  90. }
  91. }