summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-04-17 23:59:42 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-11-07 16:41:37 +0000
commit1d8a75f050f54ccb837ac13530d53c03fca38798 (patch)
treee2fda09ddf8e0f9b0e917da7d34e0fa056025a9b
parentSecond swing persistance (diff)
downloadilt-1d8a75f050f54ccb837ac13530d53c03fca38798.tar.bz2
ilt-1d8a75f050f54ccb837ac13530d53c03fca38798.tar.xz
ilt-1d8a75f050f54ccb837ac13530d53c03fca38798.zip
Move json persistance into lib
-rw-r--r--lib/jsonParse-persistance.cpp81
-rw-r--r--lib/jsonParse-persistance.h45
-rw-r--r--lib/persistance.h3
-rw-r--r--test/test-persistance.cpp87
4 files changed, 131 insertions, 85 deletions
diff --git a/lib/jsonParse-persistance.cpp b/lib/jsonParse-persistance.cpp
new file mode 100644
index 0000000..89887da
--- /dev/null
+++ b/lib/jsonParse-persistance.cpp
@@ -0,0 +1,81 @@
+#include "jsonParse-persistance.h"
+
+namespace Persistanace {
+ JsonParsePersistance::JsonParsePersistance(std::istream & in) : json::jsonParser {&in} { }
+
+ void
+ JsonParsePersistance::loadState()
+ {
+ yy_push_state(0);
+ yylex();
+ }
+
+ void
+ JsonParsePersistance::BeginObject()
+ {
+ stk.push(current()->BeginObject());
+ }
+
+ void
+ JsonParsePersistance::BeginArray()
+ {
+ current()->BeginArray(stk);
+ }
+
+ void
+ JsonParsePersistance::PushBoolean(bool value)
+ {
+ PushValue(value);
+ }
+
+ void
+ JsonParsePersistance::PushNumber(float value)
+ {
+ PushValue(value);
+ }
+
+ void
+ JsonParsePersistance::PushNull()
+ {
+ PushValue(nullptr);
+ }
+
+ void
+ JsonParsePersistance::PushText(std::string && value)
+ {
+ PushValue(value);
+ }
+
+ void
+ JsonParsePersistance::PushKey(std::string && k)
+ {
+ stk.push(current()->select(k));
+ }
+
+ void
+ JsonParsePersistance::EndArray()
+ {
+ stk.pop();
+ }
+
+ void
+ JsonParsePersistance::EndObject()
+ {
+ stk.pop();
+ }
+
+ template<typename T>
+ inline void
+ JsonParsePersistance::PushValue(T && value)
+ {
+ current()->beforeValue(stk);
+ (*current())(value);
+ stk.pop();
+ }
+
+ inline SelectionPtr &
+ JsonParsePersistance::current()
+ {
+ return stk.top();
+ }
+}
diff --git a/lib/jsonParse-persistance.h b/lib/jsonParse-persistance.h
new file mode 100644
index 0000000..d5c76ed
--- /dev/null
+++ b/lib/jsonParse-persistance.h
@@ -0,0 +1,45 @@
+#ifndef JSONPARSE_PERSISTANCE
+#define JSONPARSE_PERSISTANCE
+
+#include "jsonParse.h" // IWYU pragma: export
+#include "persistance.h" // IWYU pragma: export
+#include <functional>
+#include <iosfwd>
+#include <memory>
+#include <string>
+
+namespace Persistanace {
+ class JsonParsePersistance : public json::jsonParser {
+ public:
+ explicit JsonParsePersistance(std::istream & in);
+
+ template<typename T>
+ void
+ loadState(T & t)
+ {
+ stk.push(std::make_unique<SelectionT<T>>(std::ref(t)));
+ loadState();
+ }
+
+ protected:
+ void loadState();
+
+ void BeginObject() override;
+ void BeginArray() override;
+ void PushBoolean(bool value) override;
+ void PushNumber(float value) override;
+ void PushNull() override;
+ void PushText(std::string && value) override;
+ void PushKey(std::string && k) override;
+ void EndArray() override;
+ void EndObject() override;
+
+ private:
+ Stack stk;
+
+ template<typename T> inline void PushValue(T && value);
+ inline SelectionPtr & current();
+ };
+}
+
+#endif
diff --git a/lib/persistance.h b/lib/persistance.h
index 75222e0..92366f2 100644
--- a/lib/persistance.h
+++ b/lib/persistance.h
@@ -198,4 +198,7 @@ namespace Persistanace {
};
}
+#define STORE_TYPE store.persistType(typeid(*this))
+#define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr)
+
#endif
diff --git a/test/test-persistance.cpp b/test/test-persistance.cpp
index b6bde3f..101c2d2 100644
--- a/test/test-persistance.cpp
+++ b/test/test-persistance.cpp
@@ -2,96 +2,13 @@
#include <boost/test/unit_test.hpp>
-#include <functional>
#include <glm/glm.hpp>
#include <iosfwd>
-#include <jsonParse.h>
+#include <jsonParse-persistance.h>
#include <memory>
-#include <persistance.h>
#include <string>
#include <vector>
-struct TestObject;
-
-namespace Persistanace {
- struct JsonLoadPersistanceStore : public json::jsonParser {
- explicit JsonLoadPersistanceStore(std::istream & in) : json::jsonParser {&in} { }
-
- Stack stk;
-
- inline SelectionPtr &
- current()
- {
- return stk.top();
- }
-
- template<typename T>
- void
- loadState(std::unique_ptr<T> & t)
- {
- stk.push(std::make_unique<SelectionT<std::unique_ptr<T>>>(std::ref(t)));
- yy_push_state(0);
- yylex();
- }
- void
- BeginObject() override
- {
- stk.push(current()->BeginObject());
- }
- void
- BeginArray() override
- {
- current()->BeginArray(stk);
- }
- template<typename T>
- inline void
- PushValue(T && value)
- {
- current()->beforeValue(stk);
- (*current())(value);
- stk.pop();
- }
- void
- PushBoolean(bool value) override
- {
- PushValue(value);
- }
- void
- PushNumber(float value) override
- {
- PushValue(value);
- }
- void
- PushNull() override
- {
- PushValue(nullptr);
- }
- void
- PushText(std::string && value) override
- {
- PushValue(value);
- }
- void
- PushKey(std::string && k) override
- {
- stk.push(current()->select(k));
- }
- void
- EndArray() override
- {
- stk.pop();
- }
- void
- EndObject() override
- {
- stk.pop();
- }
- };
-}
-
-#define STORE_TYPE store.persistType(typeid(*this))
-#define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr)
-
struct TestObject : public Persistanace::Persistable {
TestObject() = default;
@@ -132,7 +49,7 @@ BOOST_AUTO_TEST_CASE(load_object)
{
Persistanace::Persistable::addFactory("TestObject", std::make_unique<TestObject>);
std::stringstream ss {input};
- Persistanace::JsonLoadPersistanceStore jlps {ss};
+ Persistanace::JsonParsePersistance jlps {ss};
std::unique_ptr<TestObject> to;
jlps.loadState(to);