Resource.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #ifndef RESOURCE_RESOURCE_H
  24. #define RESOURCE_RESOURCE_H
  25. #include "HashedType.h"
  26. #include "Timer.h"
  27. class Deserializer;
  28. class ResourceCache;
  29. class Serializer;
  30. //! Base class for resources
  31. class Resource : public HashedType
  32. {
  33. public:
  34. //! Construct with name
  35. Resource(const std::string& name);
  36. //! Load resource. Throw exception on error
  37. virtual void load(Deserializer& source, ResourceCache* cache = 0) = 0;
  38. //! Save resource. Throw exception if not supported or on error
  39. virtual void save(Serializer& dest);
  40. //! Set memory use in bytes, possibly approximate
  41. void setMemoryUse(unsigned size);
  42. //! Reset last used timer
  43. void resetUseTimer();
  44. //! Return name
  45. const std::string& getName() const { return mName; }
  46. //! Return name hash
  47. StringHash getNameHash() const { return mNameHash; }
  48. //! Return memory use in bytes, possibly approximate
  49. unsigned getMemoryUse() const { return mMemoryUse; }
  50. //! Return time since last use in milliseconds. If referred to elsewhere than in the resource cache, returns always zero
  51. unsigned getUseTimer();
  52. private:
  53. //! Name
  54. std::string mName;
  55. //! Name hash
  56. StringHash mNameHash;
  57. //! Last used timer
  58. Timer mUseTimer;
  59. //! Memory use in bytes
  60. unsigned mMemoryUse;
  61. };
  62. inline StringHash getResourceHash(Resource* resource)
  63. {
  64. if (resource)
  65. return resource->getNameHash();
  66. else
  67. return StringHash();
  68. }
  69. inline std::string getResourceName(Resource* resource)
  70. {
  71. if (resource)
  72. return resource->getName();
  73. else
  74. return std::string();
  75. }
  76. #endif // RESOURCE_RESOURCE_H