diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-05-09 20:08:31 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-11-07 16:41:37 +0000 |
commit | 44ee58911b3ffbe9e48e5278bdc73edaf618facd (patch) | |
tree | 2ffd87c5f522e2b220b0cae551809aaed302816d | |
parent | Implement writing shared objects with @id (diff) | |
download | ilt-44ee58911b3ffbe9e48e5278bdc73edaf618facd.tar.bz2 ilt-44ee58911b3ffbe9e48e5278bdc73edaf618facd.tar.xz ilt-44ee58911b3ffbe9e48e5278bdc73edaf618facd.zip |
Don't write null value for pointers
Null is a sensible default which can't be defaulted to anything else sane
-rw-r--r-- | lib/persistence.cpp | 29 | ||||
-rw-r--r-- | lib/persistence.h | 18 |
2 files changed, 32 insertions, 15 deletions
diff --git a/lib/persistence.cpp b/lib/persistence.cpp index b85bff2..405c7ec 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -38,7 +38,7 @@ namespace Persistence { PersistenceSelect::PersistenceSelect(const std::string & n) : name {n} { } PersistenceStore::NameAction - PersistenceSelect::setName(const std::string_view key) + PersistenceSelect::setName(const std::string_view key, const Selection &) { return (key == name) ? NameAction::Push : NameAction::Ignore; } @@ -51,16 +51,19 @@ namespace Persistence { PersistenceWrite::PersistenceWrite(const Writer & o, bool sh) : out {o}, shared {sh} { } PersistenceStore::NameAction - PersistenceWrite::setName(const std::string_view key) - { - if (!first) { - out.nextValue(); - } - else { - first = false; + PersistenceWrite::setName(const std::string_view key, const Selection & s) + { + if (s.needsWrite()) { + if (!first) { + out.nextValue(); + } + else { + first = false; + } + out.pushKey(key); + return NameAction::HandleAndContinue; } - out.pushKey(key); - return NameAction::HandleAndContinue; + return NameAction::Ignore; } void @@ -135,6 +138,12 @@ namespace Persistence { { } + bool + Selection::needsWrite() const + { + return true; + } + void Selection::write(const Writer &) const { diff --git a/lib/persistence.h b/lib/persistence.h index 0eba1ab..252afde 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -53,6 +53,7 @@ namespace Persistence { virtual void beforeValue(Stack &); [[nodiscard]] virtual SelectionPtr select(const std::string &); + [[nodiscard]] virtual bool needsWrite() const; virtual void write(const Writer &) const; }; @@ -108,9 +109,10 @@ namespace Persistence { [[nodiscard]] inline bool persistValue(const std::string_view key, T & value) { - const auto act {setName(key)}; + SelectionT<T> s {value}; + const auto act {setName(key, s)}; if (act != NameAction::Ignore) { - sel = SelectionV<T>::make(value); + sel = std::make_unique<decltype(s)>(std::move(s)); if (act == NameAction::HandleAndContinue) { selHandler(); } @@ -118,7 +120,7 @@ namespace Persistence { return (act != NameAction::Push); } - virtual NameAction setName(const std::string_view key) = 0; + virtual NameAction setName(const std::string_view key, const Selection &) = 0; virtual void selHandler() {}; virtual void setType(const std::string_view, const Persistable *) = 0; @@ -128,7 +130,7 @@ namespace Persistence { struct PersistenceSelect : public PersistenceStore { explicit PersistenceSelect(const std::string & n); - NameAction setName(const std::string_view key) override; + NameAction setName(const std::string_view key, const Selection &) override; void setType(const std::string_view, const Persistable *) override; @@ -138,7 +140,7 @@ namespace Persistence { struct PersistenceWrite : public PersistenceStore { explicit PersistenceWrite(const Writer & o, bool sh); - NameAction setName(const std::string_view key) override; + NameAction setName(const std::string_view key, const Selection &) override; void selHandler() override; @@ -399,6 +401,12 @@ namespace Persistence { stk.pop(); } + [[nodiscard]] bool + needsWrite() const override + { + return this->v != nullptr; + } + void write(const Writer & out) const override { |