improve container implementation

This commit is contained in:
falsycat 2023-10-01 14:53:42 +09:00
parent ab9e7f4a3d
commit 9bae60011e
4 changed files with 59 additions and 104 deletions

View File

@ -29,22 +29,17 @@ class EnvFixture : public ::testing::Test {
protected:
template <typename I>
void Install(const std::shared_ptr<I>& o) {
fmap_.emplace(typeid(I), [o](auto&) { return o; });
}
template <typename I>
void Install(std::function<std::shared_ptr<I>(Env&)>&& factory) {
fmap_.emplace(typeid(I), std::move(factory));
void Install(SimpleEnv::ObjectOrFactory&& v) {
map_.emplace(typeid(I), std::move(v));
}
template <typename I, typename T>
void Install() {
fmap_.emplace(
typeid(I), [](auto& env) { return std::make_shared<T>(env); });
map_.insert(SimpleEnv::MakeItem<I, T>());
}
protected:
void SetUp() override {
env_.emplace(std::move(fmap_));
env_.emplace(std::move(map_));
}
void TearDown() override {
env_ = std::nullopt;
@ -54,7 +49,7 @@ class EnvFixture : public ::testing::Test {
Env& env() noexcept { return *env_; }
private:
SimpleEnv::FactoryMap fmap_;
SimpleEnv::Map map_;
std::optional<SimpleEnv> env_;
};

View File

@ -6,18 +6,16 @@
#include "iface/env.hh"
static inline std::shared_ptr<nf7::Env> MakeEmptyEnv() {
return std::make_shared<nf7::SimpleEnv>(nf7::SimpleEnv::FactoryMap {});
}
static inline bool MatchPair(const std::optional<nf7::subsys::MetaEnv::Pair>& a,
const nf7::subsys::MetaEnv::Pair& b) {
static inline bool MatchPair(
const std::optional<nf7::subsys::MetaEnv::Pair>& a,
const nf7::subsys::MetaEnv::Pair& b) {
return a && a->first == b.first && &a->second == &b.second;
}
TEST(MetaEnv, FindOrByName) {
const auto a = MakeEmptyEnv();
const auto b = MakeEmptyEnv();
const auto c = MakeEmptyEnv();
const auto a = std::make_shared<nf7::SimpleEnv>();
const auto b = std::make_shared<nf7::SimpleEnv>();
const auto c = std::make_shared<nf7::SimpleEnv>();
nf7::core::MetaEnv sut {
{
@ -35,9 +33,9 @@ TEST(MetaEnv, FindOrByName) {
}
TEST(MetaEnv, FindOrByIndex) {
const auto a = MakeEmptyEnv();
const auto b = MakeEmptyEnv();
const auto c = MakeEmptyEnv();
const auto a = std::make_shared<nf7::SimpleEnv>();
const auto b = std::make_shared<nf7::SimpleEnv>();
const auto c = std::make_shared<nf7::SimpleEnv>();
nf7::core::MetaEnv sut {
{
@ -54,9 +52,9 @@ TEST(MetaEnv, FindOrByIndex) {
}
TEST(MetaEnv, FetchAll) {
const auto a = MakeEmptyEnv();
const auto b = MakeEmptyEnv();
const auto c = MakeEmptyEnv();
const auto a = std::make_shared<nf7::SimpleEnv>();
const auto b = std::make_shared<nf7::SimpleEnv>();
const auto c = std::make_shared<nf7::SimpleEnv>();
nf7::core::MetaEnv sut {
{

View File

@ -8,6 +8,7 @@
#include <typeindex>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
#include "iface/common/exception.hh"
@ -27,7 +28,6 @@ class Container {
Container& operator=(Container&&) = delete;
virtual std::shared_ptr<I> Get(std::type_index) = 0;
virtual bool installed(std::type_index) const noexcept = 0;
template <typename I2>
void Get(std::shared_ptr<I2>& out) {
@ -57,42 +57,34 @@ class Container {
const auto& ret = Get(idx);
return nullptr != ret? ret: def;
}
template <typename T>
bool installed() const noexcept {
return installed(typeid(T));
}
};
template <typename I>
class NullContainer : public Container<I> {
public:
static std::shared_ptr<NullContainer> instance()
try {
static const auto kInstance = std::make_shared<NullContainer>();
return kInstance;
} catch (const std::bad_alloc&) {
throw MemoryException {};
}
static inline const auto kInstance = std::make_shared<NullContainer>();
public:
NullContainer() = default;
public:
std::shared_ptr<I> Get(std::type_index) override { return nullptr; }
bool installed(std::type_index) const noexcept override { return false; }
};
template <typename I>
class SimpleContainer : public Container<I> {
public:
using Factory = std::function<std::shared_ptr<I>(Container<I>&)>;
using FactoryPair = std::pair<std::type_index, Factory>;
using FactoryMap = std::unordered_map<std::type_index, Factory>;
using ObjectMap = std::unordered_map<std::type_index, std::shared_ptr<I>>;
using Object = std::shared_ptr<I>;
using Factory = std::function<Object(Container<I>&)>;
using ObjectOrFactory = std::variant<Object, Factory>;
using MapItem = std::pair<std::type_index, ObjectOrFactory>;
using Map = std::unordered_map<std::type_index, ObjectOrFactory>;
public:
template <typename I2, typename T>
static FactoryPair MakePair() noexcept {
static MapItem MakeItem() noexcept {
static_assert(std::is_base_of_v<I, I2>,
"registerable interface must be based on "
"container common interface");
@ -102,62 +94,47 @@ class SimpleContainer : public Container<I> {
static_assert(std::is_constructible_v<T, Container<I>&>,
"registerable concrete type must be "
"constructible with container");
return FactoryPair {
typeid(I2), [](auto& x) { return std::make_shared<T>(x); }};
}
static std::shared_ptr<Container<I>> Make(FactoryMap&& factories) {
try {
return std::make_shared<Container<I>>(std::move(factories));
} catch (const std::bad_alloc&) {
throw MemoryException {};
}
return MapItem {
typeid(I2),
[](auto& x) { return std::make_shared<T>(x); },
};
}
SimpleContainer() = delete;
SimpleContainer(FactoryMap&& factories,
Container<I>& fb = *NullContainer<I>::instance()) noexcept
: fallback_(fb), factories_(std::move(factories)) { }
public:
SimpleContainer(Map&& m = {},
Container<I>& fb = *NullContainer<I>::kInstance) noexcept
: fallback_(fb), map_(std::move(m)) { }
std::shared_ptr<I> Get(std::type_index idx) override {
const auto obj_itr = objs_.find(idx);
if (objs_.end() != obj_itr) {
return obj_itr->second;
public:
Object Get(std::type_index idx) override {
assert(nest_ < 1000 && "circular dependency detected");
auto itr = map_.find(idx);
if (map_.end() == itr) {
return fallback_.Get(idx);
}
const auto factory_itr = factories_.find(idx);
if (factories_.end() != factory_itr) {
assert(nest_ < 1000 &&
"circular dependency detected in container factory");
auto& v = itr->second;
auto ret = Object {nullptr};
if (std::holds_alternative<Object>(v)) {
ret = std::get<Object>(v);
} else {
++nest_;
auto obj = factory_itr->second(*this);
ret = std::get<Factory>(v)(*this);
--nest_;
try {
const auto [itr, added] = objs_.insert({idx, std::move(obj)});
(void) itr;
(void) added;
assert(added);
return itr->second;
} catch (...) {
throw MemoryException {};
}
v = ret;
}
return fallback_.Get(idx);
}
bool installed(std::type_index idx) const noexcept override {
return factories_.contains(idx) || fallback_.installed(idx);
return nullptr != ret? ret:
throw Exception {"the specified interface is hidden"};
}
using Container<I>::Get;
using Container<I>::GetOr;
using Container<I>::installed;
private:
Container<I>& fallback_;
FactoryMap factories_;
ObjectMap objs_;
Map map_;
uint32_t nest_ = 0;
};

View File

@ -35,15 +35,15 @@ class BRecursive : public IB {
TEST(SimpleContainer, FetchIsolated) {
SUT sut {{
SUT::MakePair<IA, A>(),
SUT::MakeItem<IA, A>(),
}};
auto ptr = sut.Get<IA>();
EXPECT_TRUE(std::dynamic_pointer_cast<A>(ptr));
}
TEST(SimpleContainer, FetchDepending) {
SUT sut {{
SUT::MakePair<IA, A>(),
SUT::MakePair<IB, B>(),
SUT::MakeItem<IA, A>(),
SUT::MakeItem<IB, B>(),
}};
auto ptr = sut.Get<IB>();
EXPECT_TRUE(std::dynamic_pointer_cast<B>(ptr));
@ -54,21 +54,14 @@ TEST(SimpleContainer, FetchUnknown) {
}
TEST(SimpleContainer, FetchUnknownDepending) {
SUT sut {{
SUT::MakePair<IB, B>(),
SUT::MakeItem<IB, B>(),
}};
EXPECT_THROW(sut.Get<IB>(), nf7::Exception);
}
TEST(SimpleContainer, CheckInstalled) {
SUT sut {{
SUT::MakePair<IA, A>(),
}};
EXPECT_TRUE(sut.installed<IA>());
EXPECT_FALSE(sut.installed<IB>());
}
TEST(SimpleContainer, FetchWithFallback) {
SUT fb {{
SUT::MakePair<IA, A>(),
SUT::MakeItem<IA, A>(),
}};
SUT sut {{}, fb};
auto ptr = sut.Get<IA>();
@ -79,19 +72,11 @@ TEST(SimpleContainer, FetchUnknownWithFallback) {
SUT sut {{}, fb};
EXPECT_THROW(sut.Get<IA>(), nf7::Exception);
}
TEST(SimpleContainer, CheckInstalledWithFallback) {
SUT fb {{
SUT::MakePair<IA, A>(),
}};
SUT sut {{}, fb};
EXPECT_TRUE(sut.installed<IA>());
EXPECT_FALSE(sut.installed<IB>());
}
#if !defined(NDEBUG)
TEST(SimpleContainer, DeathByFetchRecursive) {
SUT sut {{
SUT::MakePair<IB, BRecursive>(),
SUT::MakeItem<IB, BRecursive>(),
}};
ASSERT_DEATH_IF_SUPPORTED(sut.Get<IB>(), "");
}