diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-20 19:32:26 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-20 19:32:26 +0000 |
commit | 5bfeeb70c6a7ed6ccda0576dc39050eb4240434f (patch) | |
tree | e922a446eae5418b725d54bc93d59120dbced3fc | |
parent | Slightly tidier FaceController? It's still a mess (diff) | |
download | ilt-5bfeeb70c6a7ed6ccda0576dc39050eb4240434f.tar.bz2 ilt-5bfeeb70c6a7ed6ccda0576dc39050eb4240434f.tar.xz ilt-5bfeeb70c6a7ed6ccda0576dc39050eb4240434f.zip |
Lazy initialisation of SelectionPtr in persistValue
Always needed for write phase, but only need on read if their's a name match.
-rw-r--r-- | lib/persistence.cpp | 22 | ||||
-rw-r--r-- | lib/persistence.h | 13 |
2 files changed, 22 insertions, 13 deletions
diff --git a/lib/persistence.cpp b/lib/persistence.cpp index 06e40c7..e22d74d 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -48,10 +48,15 @@ namespace Persistence { PersistenceSelect::PersistenceSelect(const std::string & n) : name {n} { } - PersistenceStore::NameAction - PersistenceSelect::setName(const std::string_view key, const Selection &) + PersistenceStore::NameActionSelection + PersistenceSelect::setName(const std::string_view key, SelectionFactory && factory) { - return (key == name) ? NameAction::Push : NameAction::Ignore; + if (key == name) { + return {NameAction::Push, factory()}; + } + else { + return {NameAction::Ignore, nullptr}; + } } void @@ -61,10 +66,11 @@ namespace Persistence { PersistenceWrite::PersistenceWrite(const Writer & o, bool sh) : out {o}, shared {sh} { } - PersistenceStore::NameAction - PersistenceWrite::setName(const std::string_view key, const Selection & s) + PersistenceStore::NameActionSelection + PersistenceWrite::setName(const std::string_view key, SelectionFactory && factory) { - if (s.needsWrite()) { + auto s = factory(); + if (s->needsWrite()) { if (!first) { out.nextValue(); } @@ -72,9 +78,9 @@ namespace Persistence { first = false; } out.pushKey(key); - return NameAction::HandleAndContinue; + return {NameAction::HandleAndContinue, std::move(s)}; } - return NameAction::Ignore; + return {NameAction::Ignore, nullptr}; } void diff --git a/lib/persistence.h b/lib/persistence.h index 1cb3af0..d38d4b1 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -149,6 +149,7 @@ namespace Persistence { struct Persistable; struct PersistenceStore { + using SelectionFactory = std::function<SelectionPtr()>; PersistenceStore() = default; virtual ~PersistenceStore() = default; DEFAULT_MOVE_NO_COPY(PersistenceStore); @@ -156,12 +157,14 @@ namespace Persistence { template<typename T> [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti); enum class NameAction { Push, HandleAndContinue, Ignore }; + using NameActionSelection = std::pair<NameAction, SelectionPtr>; template<typename Helper, typename T> [[nodiscard]] inline bool persistValue(const std::string_view key, T & value) { - auto s = std::make_unique<Helper>(value); - const auto act {setName(key, *s)}; + auto [act, s] = setName(key, [&value]() { + return std::make_unique<Helper>(value); + }); if (act != NameAction::Ignore) { sel = std::move(s); if (act == NameAction::HandleAndContinue) { @@ -171,7 +174,7 @@ namespace Persistence { return (act != NameAction::Push); } - virtual NameAction setName(const std::string_view key, const Selection &) = 0; + [[nodiscard]] virtual NameActionSelection setName(const std::string_view key, SelectionFactory &&) = 0; virtual void selHandler() {}; virtual void setType(const std::string_view, const Persistable *) = 0; @@ -181,7 +184,7 @@ namespace Persistence { struct PersistenceSelect : public PersistenceStore { explicit PersistenceSelect(const std::string & n); - NameAction setName(const std::string_view key, const Selection &) override; + NameActionSelection setName(const std::string_view key, SelectionFactory &&) override; void setType(const std::string_view, const Persistable *) override; @@ -191,7 +194,7 @@ namespace Persistence { struct PersistenceWrite : public PersistenceStore { explicit PersistenceWrite(const Writer & o, bool sh); - NameAction setName(const std::string_view key, const Selection &) override; + NameActionSelection setName(const std::string_view key, SelectionFactory &&) override; void selHandler() override; |