From 2d6772cb1c592e4bd75be40f1fdfa924bbbc3c07 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 14 Mar 2023 18:14:54 +0000 Subject: Add postLoad support to persistence --- lib/persistence.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/persistence.h') diff --git a/lib/persistence.h b/lib/persistence.h index 35d60ca..1cb3af0 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -311,8 +311,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr map.emplace(std::invoke(Key, s), std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -327,8 +328,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr container.emplace_back(std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -342,6 +344,7 @@ namespace Persistence { DEFAULT_MOVE_COPY(Persistable); virtual bool persist(PersistenceStore & store) = 0; + virtual void postLoad(); [[nodiscard]] virtual std::string getId() const; @@ -484,6 +487,9 @@ namespace Persistence { endObject(Stack & stk) override { make_default_as_needed(this->v); + if (this->v) { + this->v->postLoad(); + } stk.pop(); } -- cgit v1.2.3 From 5bfeeb70c6a7ed6ccda0576dc39050eb4240434f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 20 Mar 2023 19:32:26 +0000 Subject: Lazy initialisation of SelectionPtr in persistValue Always needed for write phase, but only need on read if their's a name match. --- lib/persistence.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/persistence.h') 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; PersistenceStore() = default; virtual ~PersistenceStore() = default; DEFAULT_MOVE_NO_COPY(PersistenceStore); @@ -156,12 +157,14 @@ namespace Persistence { template [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti); enum class NameAction { Push, HandleAndContinue, Ignore }; + using NameActionSelection = std::pair; template [[nodiscard]] inline bool persistValue(const std::string_view key, T & value) { - auto s = std::make_unique(value); - const auto act {setName(key, *s)}; + auto [act, s] = setName(key, [&value]() { + return std::make_unique(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; -- cgit v1.2.3 From 3f2822035b3f1c67e0d6ce410bf43e84b0846037 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 20 Mar 2023 23:22:04 +0000 Subject: Add persistence support for std::optional<>s --- lib/persistence.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/persistence.h') diff --git a/lib/persistence.h b/lib/persistence.h index d38d4b1..cc2e4e5 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,10 @@ namespace Persistence { } }; + template struct SelectionT> : public SelectionT { + explicit SelectionT(std::optional & value) : SelectionT {value.emplace()} { } + }; + struct Persistable; struct PersistenceStore { using SelectionFactory = std::function; -- cgit v1.2.3