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 /lib/persistence.h | |
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.
Diffstat (limited to 'lib/persistence.h')
-rw-r--r-- | lib/persistence.h | 13 |
1 files changed, 8 insertions, 5 deletions
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; |