GenericObjectCache stored osg::ref_ptr<osg::Object>, so everything cached had to derive from osg::Object whether that meant anything or not: NifFileHolder and BgsmFileHolder existed for no other reason, Resource::BulletShape and ESMTerrain::LandObject carried META_Object and a CopyOp constructor nothing ever called, and every getter paid a static_cast on the way out. The cache and MultiObjectCache now take their value type as a template parameter, ask "is anything else holding this?" through a useCount overload rather than a base class method, and constrain accept and releaseGLObjects so a value type with neither is not an error. Both holders are deleted; BulletShape, BulletShapeInstance and LandObject become plain classes held by std::shared_ptr across physics, pathfinding, navmeshtool and terrain storage; MWPhysics::HeightField's type-erased osg::Object keep-alive becomes the LandObject it was always holding; every remaining manager names the type it actually caches through the NodeResourceManager alias, which removes eight static_casts and stops clang-format wrapping the declarations. The preloader needs two keep-alive sets for now, because the render-owned entries are still osg::Object; they merge once those move behind the render backend.
99 lines
3.4 KiB
C++
99 lines
3.4 KiB
C++
#include <components/resource/multiobjectcache.hpp>
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace Resource
|
|
{
|
|
namespace
|
|
{
|
|
using namespace ::testing;
|
|
|
|
// A value type that derives from nothing, which is the point of the value
|
|
// type parameter; BulletShapeInstance is the real user.
|
|
using Cache = MultiObjectCache<std::shared_ptr<const std::string>>;
|
|
|
|
constexpr VFS::Path::NormalizedView path("meshes/a.nif");
|
|
constexpr VFS::Path::NormalizedView otherPath("meshes/b.nif");
|
|
|
|
TEST(ResourceMultiObjectCacheTest, takeShouldReturnNullForAbsentKey)
|
|
{
|
|
Cache cache;
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), nullptr);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, takeShouldRemoveTheEntry)
|
|
{
|
|
Cache cache;
|
|
const auto value = std::make_shared<const std::string>("value");
|
|
cache.addEntryToObjectCache(path, value);
|
|
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), value);
|
|
// "Non reusable": taking is a move out, not a read.
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), nullptr);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, shouldHoldMoreThanOneValuePerKey)
|
|
{
|
|
Cache cache;
|
|
cache.addEntryToObjectCache(path, std::make_shared<const std::string>("first"));
|
|
cache.addEntryToObjectCache(path, std::make_shared<const std::string>("second"));
|
|
|
|
EXPECT_NE(cache.takeFromObjectCache(path), nullptr);
|
|
EXPECT_NE(cache.takeFromObjectCache(path), nullptr);
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), nullptr);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, shouldRefuseNullValues)
|
|
{
|
|
Cache cache;
|
|
cache.addEntryToObjectCache(path, nullptr);
|
|
EXPECT_EQ(cache.getStats().mSize, 0u);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, removeUnreferencedShouldKeepValuesHeldElsewhere)
|
|
{
|
|
Cache cache;
|
|
const auto held = std::make_shared<const std::string>("held");
|
|
cache.addEntryToObjectCache(path, held);
|
|
cache.addEntryToObjectCache(otherPath, std::make_shared<const std::string>("dropped"));
|
|
|
|
cache.removeUnreferencedObjectsInCache();
|
|
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), held);
|
|
EXPECT_EQ(cache.takeFromObjectCache(otherPath), nullptr);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, clearShouldRemoveEverything)
|
|
{
|
|
Cache cache;
|
|
const auto held = std::make_shared<const std::string>("held");
|
|
cache.addEntryToObjectCache(path, held);
|
|
|
|
cache.clear();
|
|
|
|
EXPECT_EQ(cache.getStats().mSize, 0u);
|
|
EXPECT_EQ(cache.takeFromObjectCache(path), nullptr);
|
|
}
|
|
|
|
TEST(ResourceMultiObjectCacheTest, statsShouldCountGetsHitsAndExpiries)
|
|
{
|
|
Cache cache;
|
|
cache.addEntryToObjectCache(path, std::make_shared<const std::string>("value"));
|
|
cache.addEntryToObjectCache(otherPath, std::make_shared<const std::string>("dropped"));
|
|
|
|
cache.takeFromObjectCache(path);
|
|
cache.takeFromObjectCache(path);
|
|
cache.removeUnreferencedObjectsInCache();
|
|
|
|
const CacheStats stats = cache.getStats();
|
|
EXPECT_EQ(stats.mGet, 2u);
|
|
EXPECT_EQ(stats.mHit, 1u);
|
|
EXPECT_EQ(stats.mExpired, 1u);
|
|
}
|
|
}
|
|
}
|