1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-03 16:09:39 +00:00

Support heterogeneous lookup in GenericObjectCache

This commit is contained in:
elsid 2023-12-25 13:50:58 +01:00
parent 56401a90a1
commit fd2fc63dd3
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
2 changed files with 68 additions and 6 deletions

View file

@ -288,5 +288,62 @@ namespace Resource
EXPECT_EQ(cache->lowerBound(4), std::nullopt);
}
TEST(ResourceGenericObjectCacheTest, addEntryToObjectCacheShouldSupportHeterogeneousLookup)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
const std::string key = "key";
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(std::string_view("key"), value);
EXPECT_EQ(cache->getRefFromObjectCache(key), value);
}
TEST(ResourceGenericObjectCacheTest, addEntryToObjectCacheShouldKeyMoving)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
std::string key(128, 'a');
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(std::move(key), value);
EXPECT_EQ(key, "");
EXPECT_EQ(cache->getRefFromObjectCache(std::string(128, 'a')), value);
}
TEST(ResourceGenericObjectCacheTest, removeFromObjectCacheShouldSupportHeterogeneousLookup)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
const std::string key = "key";
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(key, value);
ASSERT_EQ(cache->getRefFromObjectCache(key), value);
cache->removeFromObjectCache(std::string_view("key"));
EXPECT_EQ(cache->getRefFromObjectCacheOrNone(key), std::nullopt);
}
TEST(ResourceGenericObjectCacheTest, getRefFromObjectCacheShouldSupportHeterogeneousLookup)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
const std::string key = "key";
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(key, value);
EXPECT_EQ(cache->getRefFromObjectCache(std::string_view("key")), value);
}
TEST(ResourceGenericObjectCacheTest, getRefFromObjectCacheOrNoneShouldSupportHeterogeneousLookup)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
const std::string key = "key";
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(key, value);
EXPECT_THAT(cache->getRefFromObjectCacheOrNone(std::string_view("key")), Optional(value));
}
TEST(ResourceGenericObjectCacheTest, checkInObjectCacheShouldSupportHeterogeneousLookup)
{
osg::ref_ptr<GenericObjectCache<std::string>> cache(new GenericObjectCache<std::string>);
const std::string key = "key";
osg::ref_ptr<Object> value(new Object);
cache->addEntryToObjectCache(key, value);
EXPECT_TRUE(cache->checkInObjectCache(std::string_view("key"), 0));
}
}
}

View file

@ -81,14 +81,19 @@ namespace Resource
}
/** Add a key,object,timestamp triple to the Registry::ObjectCache.*/
void addEntryToObjectCache(const KeyType& key, osg::Object* object, double timestamp = 0.0)
template <class K>
void addEntryToObjectCache(K&& key, osg::Object* object, double timestamp = 0.0)
{
std::lock_guard<std::mutex> lock(_objectCacheMutex);
_objectCache[key] = Item{ object, timestamp };
const auto it = _objectCache.find(key);
if (it == _objectCache.end())
_objectCache.emplace_hint(it, std::forward<K>(key), Item{ object, timestamp });
else
it->second = Item{ object, timestamp };
}
/** Remove Object from cache.*/
void removeFromObjectCache(const KeyType& key)
void removeFromObjectCache(const auto& key)
{
std::lock_guard<std::mutex> lock(_objectCacheMutex);
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
@ -97,7 +102,7 @@ namespace Resource
}
/** Get an ref_ptr<Object> from the object cache*/
osg::ref_ptr<osg::Object> getRefFromObjectCache(const KeyType& key)
osg::ref_ptr<osg::Object> getRefFromObjectCache(const auto& key)
{
std::lock_guard<std::mutex> lock(_objectCacheMutex);
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
@ -107,7 +112,7 @@ namespace Resource
return nullptr;
}
std::optional<osg::ref_ptr<osg::Object>> getRefFromObjectCacheOrNone(const KeyType& key)
std::optional<osg::ref_ptr<osg::Object>> getRefFromObjectCacheOrNone(const auto& key)
{
const std::lock_guard<std::mutex> lock(_objectCacheMutex);
const auto it = _objectCache.find(key);
@ -117,7 +122,7 @@ namespace Resource
}
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
bool checkInObjectCache(const KeyType& key, double timeStamp)
bool checkInObjectCache(const auto& key, double timeStamp)
{
std::lock_guard<std::mutex> lock(_objectCacheMutex);
typename ObjectCacheMap::iterator itr = _objectCache.find(key);