1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 16:29:55 +00:00
openmw/components/resource/resourcemanager.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.8 KiB
C++
Raw Normal View History

#ifndef OPENMW_COMPONENTS_RESOURCE_MANAGER_H
#define OPENMW_COMPONENTS_RESOURCE_MANAGER_H
#include <osg/ref_ptr>
#include <components/settings/values.hpp>
2019-03-13 07:15:58 +00:00
#include "objectcache.hpp"
namespace VFS
{
class Manager;
}
namespace osg
{
class Stats;
class State;
}
namespace Resource
{
2019-03-13 07:15:58 +00:00
class BaseResourceManager
{
public:
2023-05-31 21:11:03 +00:00
virtual ~BaseResourceManager() = default;
2023-08-27 15:12:20 +00:00
virtual void updateCache(double referenceTime) = 0;
virtual void clearCache() = 0;
virtual void setExpiryDelay(double expiryDelay) = 0;
virtual void reportStats(unsigned int frameNumber, osg::Stats* stats) const = 0;
virtual void releaseGLObjects(osg::State* state) = 0;
2019-03-13 07:15:58 +00:00
};
/// @brief Base class for managers that require a virtual file system and object cache.
/// @par This base class implements clearing of the cache, but populating it and what it's used for is up to the
/// individual sub classes.
2019-03-13 07:15:58 +00:00
template <class KeyType>
class GenericResourceManager : public BaseResourceManager
{
public:
2019-03-13 07:15:58 +00:00
typedef GenericObjectCache<KeyType> CacheType;
explicit GenericResourceManager(
const VFS::Manager* vfs, double expiryDelay = Settings::cells().mCacheExpiryDelay)
2019-03-13 07:15:58 +00:00
: mVFS(vfs)
, mCache(new CacheType)
, mExpiryDelay(expiryDelay)
2019-03-13 07:15:58 +00:00
{
}
2023-05-31 21:11:03 +00:00
virtual ~GenericResourceManager() = default;
/// Clear cache entries that have not been referenced for longer than expiryDelay.
void updateCache(double referenceTime) override
2019-03-13 07:15:58 +00:00
{
mCache->updateTimeStampOfObjectsInCacheWithExternalReferences(referenceTime);
mCache->removeExpiredObjectsInCache(referenceTime - mExpiryDelay);
}
2017-08-19 19:26:46 +00:00
/// Clear all cache entries.
void clearCache() override { mCache->clear(); }
2017-08-19 19:26:46 +00:00
/// How long to keep objects in cache after no longer being referenced.
void setExpiryDelay(double expiryDelay) final { mExpiryDelay = expiryDelay; }
double getExpiryDelay() const { return mExpiryDelay; }
2019-03-13 07:15:58 +00:00
const VFS::Manager* getVFS() const { return mVFS; }
void reportStats(unsigned int frameNumber, osg::Stats* stats) const override {}
void releaseGLObjects(osg::State* state) override { mCache->releaseGLObjects(state); }
protected:
const VFS::Manager* mVFS;
2019-03-13 07:15:58 +00:00
osg::ref_ptr<CacheType> mCache;
double mExpiryDelay;
};
2019-03-13 07:15:58 +00:00
class ResourceManager : public GenericResourceManager<std::string>
{
public:
explicit ResourceManager(const VFS::Manager* vfs)
2019-03-13 07:15:58 +00:00
: GenericResourceManager<std::string>(vfs)
{
}
explicit ResourceManager(const VFS::Manager* vfs, double expiryDelay)
: GenericResourceManager<std::string>(vfs, expiryDelay)
{
}
2019-03-13 07:15:58 +00:00
};
}
#endif