From abd7f486a0fb34320e3afae6780677cfd4ff85cc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 25 Apr 2021 19:19:28 +0100 Subject: Erring toward complete JSON loader --- lib/jsonParse-persistance.cpp | 11 +++---- lib/jsonParse-persistance.h | 13 ++++----- lib/persistance.cpp | 11 +++++-- lib/persistance.h | 68 +++++++++++++++++++++++++++---------------- 4 files changed, 63 insertions(+), 40 deletions(-) (limited to 'lib') diff --git a/lib/jsonParse-persistance.cpp b/lib/jsonParse-persistance.cpp index 89887da..ad9fc2a 100644 --- a/lib/jsonParse-persistance.cpp +++ b/lib/jsonParse-persistance.cpp @@ -1,11 +1,10 @@ #include "jsonParse-persistance.h" namespace Persistanace { - JsonParsePersistance::JsonParsePersistance(std::istream & in) : json::jsonParser {&in} { } - void - JsonParsePersistance::loadState() + JsonParsePersistance::loadState(std::istream & in) { + this->switch_streams(&in, nullptr); yy_push_state(0); yylex(); } @@ -13,7 +12,8 @@ namespace Persistanace { void JsonParsePersistance::BeginObject() { - stk.push(current()->BeginObject()); + current()->beforeValue(stk); + current()->BeginObject(stk); } void @@ -61,7 +61,8 @@ namespace Persistanace { void JsonParsePersistance::EndObject() { - stk.pop(); + current()->EndObject(stk); + current()->EndObject(stk); } template diff --git a/lib/jsonParse-persistance.h b/lib/jsonParse-persistance.h index d5c76ed..bea93e0 100644 --- a/lib/jsonParse-persistance.h +++ b/lib/jsonParse-persistance.h @@ -11,18 +11,18 @@ namespace Persistanace { class JsonParsePersistance : public json::jsonParser { public: - explicit JsonParsePersistance(std::istream & in); - template - void - loadState(T & t) + inline T + loadState(std::istream & in) { + T t {}; stk.push(std::make_unique>(std::ref(t))); - loadState(); + loadState(in); + return t; } protected: - void loadState(); + void loadState(std::istream & in); void BeginObject() override; void BeginArray() override; @@ -34,7 +34,6 @@ namespace Persistanace { void EndArray() override; void EndObject() override; - private: Stack stk; template inline void PushValue(T && value); diff --git a/lib/persistance.cpp b/lib/persistance.cpp index 7539b44..bdcbe9f 100644 --- a/lib/persistance.cpp +++ b/lib/persistance.cpp @@ -47,8 +47,8 @@ namespace Persistanace { throw std::runtime_error("Unexpected array"); } - SelectionPtr - Selection::BeginObject() + void + Selection::BeginObject(Stack &) { throw std::runtime_error("Unexpected object"); } @@ -62,7 +62,12 @@ namespace Persistanace { SelectionPtr Selection::select(const std::string &) { - throw std::runtime_error("Unexpected persist"); + throw std::runtime_error("Unexpected select"); + } + + void + Selection::EndObject(Stack &) + { } static_assert(!SelectionT::ArrayLike); diff --git a/lib/persistance.h b/lib/persistance.h index 92366f2..7f28ad1 100644 --- a/lib/persistance.h +++ b/lib/persistance.h @@ -32,7 +32,8 @@ namespace Persistanace { virtual void operator()(const std::nullptr_t &); virtual void operator()(std::string &); virtual void BeginArray(Stack &); - virtual SelectionPtr BeginObject(); + virtual void BeginObject(Stack &); + virtual void EndObject(Stack &); virtual void beforeValue(Stack &); virtual SelectionPtr select(const std::string &); }; @@ -94,28 +95,13 @@ namespace Persistanace { explicit SelectionT(std::vector & value) : v {value} { } void - BeginArray(Stack &) override - { - } - - void - beforeValue(Stack & stk) override + BeginArray(Stack & stk) override { stk.push(std::make_unique>(std::ref(v.emplace_back()))); } - static constexpr bool ArrayLike {true}; - std::vector & v; - }; - - template - concept ArrayLike = SelectionT::ArrayLike; - - template struct SelectionT> : public Selection { - explicit SelectionT(std::vector & value) : v {value} { } - void - BeginArray(Stack & stk) override + beforeValue(Stack & stk) override { stk.push(std::make_unique>(std::ref(v.emplace_back()))); } @@ -172,10 +158,12 @@ namespace Persistanace { } else { if (!v) { - if constexpr (!std::is_abstract_v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot select member of null object"); + } + else { v = std::make_unique(); } - throw std::runtime_error("cannot select member of null object"); } PersistanceStore ps {mbr}; v->persist(ps); @@ -183,18 +171,48 @@ namespace Persistanace { } } + void + EndObject(Stack & stk) override + { + if (!v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot default create abstract object"); + } + else { + v = std::make_unique(); + } + } + stk.pop(); + } + Ptr & v; }; - explicit SelectionT(std::unique_ptr & o) : v {o} { } + explicit SelectionT(Ptr & o) : v {o} { } + + void + beforeValue(Stack &) override + { + } + + void + operator()(const std::nullptr_t &) override + { + v.reset(); + } - SelectionPtr - BeginObject() override + void + BeginObject(Stack & stk) override + { + stk.push(std::make_unique(v)); + } + void + EndObject(Stack & stk) override { - return std::make_unique(v); + stk.pop(); } - std::unique_ptr & v; + Ptr & v; }; } -- cgit v1.2.3