From dd34656c362e6be126df14ee75ce1353d758ac3b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 28 Apr 2021 14:59:52 +0100 Subject: Common base class for value selections --- lib/persistance.h | 136 +++++++++++++++++++++--------------------------------- 1 file changed, 53 insertions(+), 83 deletions(-) (limited to 'lib') diff --git a/lib/persistance.h b/lib/persistance.h index 32e5cb2..863de47 100644 --- a/lib/persistance.h +++ b/lib/persistance.h @@ -36,41 +36,43 @@ namespace Persistanace { virtual void endObject(Stack &); virtual void beforeValue(Stack &); virtual SelectionPtr select(const std::string &); - - template static SelectionPtr make(T & value); - template static SelectionPtr make_s(T & value); }; - template struct SelectionT : public Selection { - explicit SelectionT(T & value) : v {value} { } + template struct SelectionT; + + template struct SelectionV : public Selection { + explicit SelectionV(T & value) : v {value} { } void beforeValue(Stack &) override { } - void - setValue(T & evalue) override + static SelectionPtr + make(T & value) + { + return make_s>(value); + } + + template + static SelectionPtr + make_s(T & value) { - std::swap(v, evalue); + return std::make_unique(value); } T & v; }; - template - SelectionPtr - Selection::make(T & value) - { - return make_s>(value); - } + template struct SelectionT : public SelectionV { + using SelectionV::SelectionV; - template - SelectionPtr - Selection::make_s(T & value) - { - return std::make_unique(value); - } + void + setValue(T & evalue) override + { + std::swap(this->v, evalue); + } + }; struct PersistanceStore { // virtual bool persistType(const std::type_info &) = 0; @@ -79,7 +81,7 @@ namespace Persistanace { persistValue(const std::string_view key, T & value) { if (key == name) { - sel = Selection::make(value); + sel = SelectionV::make(value); return false; } return true; @@ -88,67 +90,51 @@ namespace Persistanace { SelectionPtr sel {}; }; - template struct SelectionT> : public Selection { - using V = glm::vec; + template + struct SelectionT> : public SelectionV> { + using V = glm::vec; - struct Members : public Selection { - explicit Members(V & value) : v {value} { } + struct Members : public SelectionV { + using SelectionV::SelectionV; void beforeValue(Stack & stk) override { - stk.push(make(v[idx++])); + stk.push(SelectionV::make(this->v[idx++])); } - V & v; glm::length_t idx {0}; }; - explicit SelectionT(V & value) : v {value} { } + using SelectionV::SelectionV; void beginArray(Stack & stk) override { - stk.push(make_s(v)); + stk.push(make_s(this->v)); } - - void - beforeValue(Stack &) override - { - } - - V & v; }; - template struct SelectionT> : public Selection { + template struct SelectionT> : public SelectionV> { using V = std::vector; - struct Members : public Selection { - explicit Members(V & value) : v {value} { } + struct Members : public SelectionV { + using SelectionV::SelectionV; void beforeValue(Stack & stk) override { - stk.push(make(v.emplace_back())); + stk.push(SelectionV::make(this->v.emplace_back())); } - - V & v; }; - explicit SelectionT(V & value) : v {value} { } + using SelectionV::SelectionV; void beginArray(Stack & stk) override { - stk.push(make_s(v)); + stk.push(make_s(this->v)); } - - void - beforeValue(Stack &) override - { - } - - std::vector & v; }; struct Persistable { @@ -162,52 +148,45 @@ namespace Persistanace { static std::unique_ptr callFactory(const std::string_view); }; - template struct SelectionT> : public Selection { + template struct SelectionT> : public SelectionV> { using Ptr = std::unique_ptr; - struct SelectionObj : public Selection { - struct MakeObjectByTypeName : public Selection { - explicit MakeObjectByTypeName(Ptr & o) : o {o} { } - - void - beforeValue(Stack &) override - { - } + struct SelectionObj : public SelectionV { + struct MakeObjectByTypeName : public SelectionV { + using SelectionV::SelectionV; void setValue(std::string & type) override { auto no = Persistable::callFactory(type); if (dynamic_cast(no.get())) { - o.reset(static_cast(no.release())); + this->v.reset(static_cast(no.release())); } } - - Ptr & o; }; - explicit SelectionObj(Ptr & o) : v {o} { } + using SelectionV::SelectionV; SelectionPtr select(const std::string & mbr) override { using namespace std::literals; if (mbr == "@typeid"sv) { - if (v) { + if (this->v) { throw std::runtime_error("cannot set object type after creation"); } - return make_s(v); + return make_s(this->v); } else { - if (!v) { + if (!this->v) { if constexpr (std::is_abstract_v) { throw std::runtime_error("cannot select member of null object"); } else { - v = std::make_unique(); + this->v = std::make_unique(); } } PersistanceStore ps {mbr}; - if (v->persist(ps)) { + if (this->v->persist(ps)) { throw std::runtime_error("cannot find member: " + mbr); } return std::move(ps.sel); @@ -217,37 +196,30 @@ namespace Persistanace { void endObject(Stack & stk) override { - if (!v) { + if (!this->v) { if constexpr (std::is_abstract_v) { throw std::runtime_error("cannot default create abstract object"); } else { - v = std::make_unique(); + this->v = std::make_unique(); } } stk.pop(); } - - Ptr & v; }; - explicit SelectionT(Ptr & o) : v {o} { } - - void - beforeValue(Stack &) override - { - } + using SelectionV::SelectionV; void setValue(const std::nullptr_t &) override { - v.reset(); + this->v.reset(); } void beginObject(Stack & stk) override { - stk.push(make_s(v)); + stk.push(make_s(this->v)); } void @@ -255,8 +227,6 @@ namespace Persistanace { { stk.pop(); } - - Ptr & v; }; } -- cgit v1.2.3