Resource.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. #include "Timer.h"
  26. class Deserializer;
  27. class Serializer;
  28. /// Base class for resources.
  29. class Resource : public Object
  30. {
  31. OBJECT(Resource);
  32. public:
  33. /// Construct.
  34. Resource(Context* context);
  35. /// Load resource. Return true if successful.
  36. virtual bool Load(Deserializer& source);
  37. /// Save resource. Return true if successful.
  38. virtual bool Save(Serializer& dest);
  39. /// %Set name.
  40. void SetName(const String& name);
  41. /// %Set memory use in bytes, possibly approximate.
  42. void SetMemoryUse(unsigned size);
  43. /// Reset last used timer.
  44. void ResetUseTimer();
  45. /// Return name.
  46. const String& GetName() const { return name_; }
  47. /// Return name hash.
  48. StringHash GetNameHash() const { return nameHash_; }
  49. /// Return memory use in bytes, possibly approximate.
  50. unsigned GetMemoryUse() const { return memoryUse_; }
  51. /// Return time since last use in milliseconds. If referred to elsewhere than in the resource cache, returns always zero.
  52. unsigned GetUseTimer();
  53. private:
  54. /// Name.
  55. String name_;
  56. /// Name hash.
  57. StringHash nameHash_;
  58. /// Last used timer.
  59. Timer useTimer_;
  60. /// Memory use in bytes.
  61. unsigned memoryUse_;
  62. };
  63. inline StringHash GetResourceHash(Resource* resource)
  64. {
  65. return resource ? resource->GetNameHash() : StringHash();
  66. }
  67. inline String GetResourceName(Resource* resource)
  68. {
  69. return resource ? resource->GetName() : String();
  70. }
  71. inline ShortStringHash GetResourceType(Resource* resource, ShortStringHash defaultType)
  72. {
  73. return resource ? resource->GetType() : defaultType;
  74. }
  75. inline ResourceRef GetResourceRef(Resource* resource, ShortStringHash defaultType)
  76. {
  77. return ResourceRef(GetResourceType(resource, defaultType), GetResourceHash(resource));
  78. }
  79. template <class T> Vector<StringHash> GetResourceHashes(const Vector<SharedPtr<T> >& resources)
  80. {
  81. Vector<StringHash> ret(resources.Size());
  82. for (unsigned i = 0; i < resources.Size(); ++i)
  83. ret[i] = GetResourceHash(resources[i]);
  84. return ret;
  85. }
  86. template <class T> ResourceRefList GetResourceRefList(const Vector<SharedPtr<T> >& resources)
  87. {
  88. return ResourceRefList(T::GetTypeStatic(), GetResourceHashes(resources));
  89. }