1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-30 07:21:12 +00:00

Apply project naming styleguide to GenericObjectCache

This commit is contained in:
elsid 2023-12-25 14:23:36 +01:00
parent 45b1b4f1e0
commit 7a817d3147
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

View File

@ -57,8 +57,8 @@ namespace Resource
std::vector<osg::ref_ptr<osg::Object>> objectsToRemove; std::vector<osg::ref_ptr<osg::Object>> objectsToRemove;
{ {
const double expiryTime = referenceTime - expiryDelay; const double expiryTime = referenceTime - expiryDelay;
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
std::erase_if(_objectCache, [&](auto& v) { std::erase_if(mItems, [&](auto& v) {
Item& item = v.second; Item& item = v.second;
if ((item.mValue != nullptr && item.mValue->referenceCount() > 1) || item.mLastUsage == 0) if ((item.mValue != nullptr && item.mValue->referenceCount() > 1) || item.mLastUsage == 0)
item.mLastUsage = referenceTime; item.mLastUsage = referenceTime;
@ -76,18 +76,18 @@ namespace Resource
/** Remove all objects in the cache regardless of having external references or expiry times.*/ /** Remove all objects in the cache regardless of having external references or expiry times.*/
void clear() void clear()
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
_objectCache.clear(); mItems.clear();
} }
/** Add a key,object,timestamp triple to the Registry::ObjectCache.*/ /** Add a key,object,timestamp triple to the Registry::ObjectCache.*/
template <class K> template <class K>
void addEntryToObjectCache(K&& key, osg::Object* object, double timestamp = 0.0) void addEntryToObjectCache(K&& key, osg::Object* object, double timestamp = 0.0)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
const auto it = _objectCache.find(key); const auto it = mItems.find(key);
if (it == _objectCache.end()) if (it == mItems.end())
_objectCache.emplace_hint(it, std::forward<K>(key), Item{ object, timestamp }); mItems.emplace_hint(it, std::forward<K>(key), Item{ object, timestamp });
else else
it->second = Item{ object, timestamp }; it->second = Item{ object, timestamp };
} }
@ -95,18 +95,18 @@ namespace Resource
/** Remove Object from cache.*/ /** Remove Object from cache.*/
void removeFromObjectCache(const auto& key) void removeFromObjectCache(const auto& key)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
const auto itr = _objectCache.find(key); const auto itr = mItems.find(key);
if (itr != _objectCache.end()) if (itr != mItems.end())
_objectCache.erase(itr); mItems.erase(itr);
} }
/** Get an ref_ptr<Object> from the object cache*/ /** Get an ref_ptr<Object> from the object cache*/
osg::ref_ptr<osg::Object> getRefFromObjectCache(const auto& key) osg::ref_ptr<osg::Object> getRefFromObjectCache(const auto& key)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
const auto itr = _objectCache.find(key); const auto itr = mItems.find(key);
if (itr != _objectCache.end()) if (itr != mItems.end())
return itr->second.mValue; return itr->second.mValue;
else else
return nullptr; return nullptr;
@ -114,9 +114,9 @@ namespace Resource
std::optional<osg::ref_ptr<osg::Object>> getRefFromObjectCacheOrNone(const auto& key) std::optional<osg::ref_ptr<osg::Object>> getRefFromObjectCacheOrNone(const auto& key)
{ {
const std::lock_guard<std::mutex> lock(_objectCacheMutex); const std::lock_guard<std::mutex> lock(mMutex);
const auto it = _objectCache.find(key); const auto it = mItems.find(key);
if (it == _objectCache.end()) if (it == mItems.end())
return std::nullopt; return std::nullopt;
return it->second.mValue; return it->second.mValue;
} }
@ -124,9 +124,9 @@ namespace Resource
/** Check if an object is in the cache, and if it is, update its usage time stamp. */ /** Check if an object is in the cache, and if it is, update its usage time stamp. */
bool checkInObjectCache(const auto& key, double timeStamp) bool checkInObjectCache(const auto& key, double timeStamp)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
const auto itr = _objectCache.find(key); const auto itr = mItems.find(key);
if (itr != _objectCache.end()) if (itr != mItems.end())
{ {
itr->second.mLastUsage = timeStamp; itr->second.mLastUsage = timeStamp;
return true; return true;
@ -138,16 +138,16 @@ namespace Resource
/** call releaseGLObjects on all objects attached to the object cache.*/ /** call releaseGLObjects on all objects attached to the object cache.*/
void releaseGLObjects(osg::State* state) void releaseGLObjects(osg::State* state)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
for (const auto& [k, v] : _objectCache) for (const auto& [k, v] : mItems)
v.mValue->releaseGLObjects(state); v.mValue->releaseGLObjects(state);
} }
/** call node->accept(nv); for all nodes in the objectCache. */ /** call node->accept(nv); for all nodes in the objectCache. */
void accept(osg::NodeVisitor& nv) void accept(osg::NodeVisitor& nv)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
for (const auto& [k, v] : _objectCache) for (const auto& [k, v] : mItems)
if (osg::Object* const object = v.mValue.get()) if (osg::Object* const object = v.mValue.get())
if (osg::Node* const node = dynamic_cast<osg::Node*>(object)) if (osg::Node* const node = dynamic_cast<osg::Node*>(object))
node->accept(nv); node->accept(nv);
@ -157,24 +157,24 @@ namespace Resource
template <class Functor> template <class Functor>
void call(Functor&& f) void call(Functor&& f)
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
for (const auto& [k, v] : _objectCache) for (const auto& [k, v] : mItems)
f(k, v.mValue.get()); f(k, v.mValue.get());
} }
/** Get the number of objects in the cache. */ /** Get the number of objects in the cache. */
unsigned int getCacheSize() const unsigned int getCacheSize() const
{ {
std::lock_guard<std::mutex> lock(_objectCacheMutex); std::lock_guard<std::mutex> lock(mMutex);
return _objectCache.size(); return mItems.size();
} }
template <class K> template <class K>
std::optional<std::pair<KeyType, osg::ref_ptr<osg::Object>>> lowerBound(K&& key) std::optional<std::pair<KeyType, osg::ref_ptr<osg::Object>>> lowerBound(K&& key)
{ {
const std::lock_guard<std::mutex> lock(_objectCacheMutex); const std::lock_guard<std::mutex> lock(mMutex);
const auto it = _objectCache.lower_bound(std::forward<K>(key)); const auto it = mItems.lower_bound(std::forward<K>(key));
if (it == _objectCache.end()) if (it == mItems.end())
return std::nullopt; return std::nullopt;
return std::pair(it->first, it->second.mValue); return std::pair(it->first, it->second.mValue);
} }
@ -186,8 +186,8 @@ namespace Resource
double mLastUsage; double mLastUsage;
}; };
std::map<KeyType, Item, std::less<>> _objectCache; std::map<KeyType, Item, std::less<>> mItems;
mutable std::mutex _objectCacheMutex; mutable std::mutex mMutex;
}; };
class ObjectCache : public GenericObjectCache<std::string> class ObjectCache : public GenericObjectCache<std::string>