summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-04-25 19:19:28 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-11-07 16:41:37 +0000
commitabd7f486a0fb34320e3afae6780677cfd4ff85cc (patch)
treef2633cc76e78e35300a7dafdf3bce1bd8bcf2b7b /lib
parentRemove spare semi colons (diff)
downloadilt-abd7f486a0fb34320e3afae6780677cfd4ff85cc.tar.bz2
ilt-abd7f486a0fb34320e3afae6780677cfd4ff85cc.tar.xz
ilt-abd7f486a0fb34320e3afae6780677cfd4ff85cc.zip
Erring toward complete JSON loader
Diffstat (limited to 'lib')
-rw-r--r--lib/jsonParse-persistance.cpp11
-rw-r--r--lib/jsonParse-persistance.h13
-rw-r--r--lib/persistance.cpp11
-rw-r--r--lib/persistance.h68
4 files changed, 63 insertions, 40 deletions
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<typename T>
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<typename T>
- void
- loadState(T & t)
+ inline T
+ loadState(std::istream & in)
{
+ T t {};
stk.push(std::make_unique<SelectionT<T>>(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<typename T> 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<float>::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<T> & value) : v {value} { }
void
- BeginArray(Stack &) override
- {
- }
-
- void
- beforeValue(Stack & stk) override
+ BeginArray(Stack & stk) override
{
stk.push(std::make_unique<SelectionT<T>>(std::ref(v.emplace_back())));
}
- static constexpr bool ArrayLike {true};
- std::vector<T> & v;
- };
-
- template<typename T>
- concept ArrayLike = SelectionT<T>::ArrayLike;
-
- template<ArrayLike T> struct SelectionT<std::vector<T>> : public Selection {
- explicit SelectionT(std::vector<T> & value) : v {value} { }
-
void
- BeginArray(Stack & stk) override
+ beforeValue(Stack & stk) override
{
stk.push(std::make_unique<SelectionT<T>>(std::ref(v.emplace_back())));
}
@@ -172,10 +158,12 @@ namespace Persistanace {
}
else {
if (!v) {
- if constexpr (!std::is_abstract_v<T>) {
+ if constexpr (std::is_abstract_v<T>) {
+ throw std::runtime_error("cannot select member of null object");
+ }
+ else {
v = std::make_unique<T>();
}
- 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<T>) {
+ throw std::runtime_error("cannot default create abstract object");
+ }
+ else {
+ v = std::make_unique<T>();
+ }
+ }
+ stk.pop();
+ }
+
Ptr & v;
};
- explicit SelectionT(std::unique_ptr<T> & 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<SelectionObj>(v));
+ }
+ void
+ EndObject(Stack & stk) override
{
- return std::make_unique<SelectionObj>(v);
+ stk.pop();
}
- std::unique_ptr<T> & v;
+ Ptr & v;
};
}