summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-05-07 20:50:48 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-11-07 16:41:37 +0000
commitce5c689a5f9e544d724b2a41fda2f752ccc4a4ae (patch)
tree33f2c53d2cb1b67f3c7024bbec2366be875b57d9 /lib
parentFixup clang, cppcheck and iwyu warnings (diff)
downloadilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.bz2
ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.xz
ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.zip
Initial commit where writing objects back out to JSON
It's not perfect, writes explicit nulls, doesn't do shared @id
Diffstat (limited to 'lib')
-rw-r--r--lib/jsonParse-persistence.cpp116
-rw-r--r--lib/jsonParse-persistence.h28
-rw-r--r--lib/persistence.cpp6
-rw-r--r--lib/persistence.h158
4 files changed, 300 insertions, 8 deletions
diff --git a/lib/jsonParse-persistence.cpp b/lib/jsonParse-persistence.cpp
index dce9d3c..6ecfff8 100644
--- a/lib/jsonParse-persistence.cpp
+++ b/lib/jsonParse-persistence.cpp
@@ -1,4 +1,8 @@
#include "jsonParse-persistence.h"
+#include <algorithm>
+#include <array>
+#include <iomanip>
+#include <ostream>
#include <type_traits>
#include <utility>
@@ -83,4 +87,116 @@ namespace Persistence {
{
return stk.top();
}
+
+ static inline void
+ wrv(std::ostream & strm, char ch)
+ {
+ strm.put(ch);
+ };
+ static inline void
+ wrh(std::ostream & strm, char ch)
+ {
+ using namespace std::literals;
+ strm << R"(\u)"sv << std::setw(4) << std::hex << (int)ch << std::setw(1);
+ }
+ static inline void
+ wre(std::ostream & strm, char e)
+ {
+ strm << '\\' << e;
+ }
+ template<char E>
+ static inline void
+ wre(std::ostream & strm, char)
+ {
+ wre(strm, E);
+ }
+
+ using OutFunc = void (*)(std::ostream &, char);
+ using OutFuncs = std::array<OutFunc, 255>;
+ static constexpr OutFuncs outFuncs {[]() {
+ OutFuncs outFuncs;
+ outFuncs.fill(&wrv);
+ for (int x = 0; x < 0x20; x += 1) {
+ outFuncs[x] = &wrh;
+ }
+ outFuncs['\"'] = &wre<'"'>;
+ outFuncs['\\'] = &wre<'\\'>;
+ outFuncs['\b'] = &wre<'b'>;
+ outFuncs['\f'] = &wre<'f'>;
+ outFuncs['\n'] = &wre<'n'>;
+ outFuncs['\r'] = &wre<'r'>;
+ outFuncs['\t'] = &wre<'t'>;
+ return outFuncs;
+ }()};
+
+ JsonWritePersistence::JsonWritePersistence(std::ostream & s) : strm {s}
+ {
+ strm << std::boolalpha // for Boolean
+ << std::defaultfloat // for Number
+ << std::setfill('0'); // for String \uNNNN
+ }
+
+ void
+ JsonWritePersistence::beginObject() const
+ {
+ strm << '{';
+ }
+
+ void
+ JsonWritePersistence::beginArray() const
+ {
+ strm << '[';
+ }
+
+ void
+ JsonWritePersistence::pushValue(bool value) const
+ {
+ strm << value;
+ }
+
+ void
+ JsonWritePersistence::pushValue(float value) const
+ {
+ strm << value;
+ }
+
+ void JsonWritePersistence::pushValue(std::nullptr_t) const
+ {
+ strm << "null";
+ }
+
+ void
+ JsonWritePersistence::pushValue(const std::string_view value) const
+ {
+ strm << '"';
+ std::for_each(value.begin(), value.end(), [this](char ch) {
+ outFuncs[ch](strm, ch);
+ });
+ strm << '"';
+ }
+
+ void
+ JsonWritePersistence::nextValue() const
+ {
+ strm << ',';
+ }
+
+ void
+ JsonWritePersistence::pushKey(const std::string_view k) const
+ {
+ pushValue(k);
+ strm << ':';
+ }
+
+ void
+ JsonWritePersistence::endArray() const
+ {
+ strm << ']';
+ }
+
+ void
+ JsonWritePersistence::endObject() const
+ {
+ strm << '}';
+ }
}
diff --git a/lib/jsonParse-persistence.h b/lib/jsonParse-persistence.h
index 6449f7b..edb5305 100644
--- a/lib/jsonParse-persistence.h
+++ b/lib/jsonParse-persistence.h
@@ -7,6 +7,7 @@
#include <iosfwd>
#include <memory>
#include <string>
+#include <string_view>
namespace Persistence {
class JsonParsePersistence : public json::jsonParser {
@@ -39,6 +40,33 @@ namespace Persistence {
template<typename T> inline void pushValue(T && value);
inline SelectionPtr & current();
};
+
+ class JsonWritePersistence : public Writer {
+ public:
+ explicit JsonWritePersistence(std::ostream & s);
+
+ template<typename T>
+ inline void
+ saveState(T & t) const
+ {
+ SelectionT<T> {t}.write(*this);
+ }
+
+ protected:
+ void beginObject() const override;
+ void beginArray() const override;
+ void pushValue(bool value) const override;
+ void pushValue(float value) const override;
+ void pushValue(std::nullptr_t) const override;
+ void pushValue(const std::string_view value) const override;
+ void nextValue() const override;
+ void pushKey(const std::string_view k) const override;
+ void endArray() const override;
+ void endObject() const override;
+
+ private:
+ std::ostream & strm;
+ };
}
#endif
diff --git a/lib/persistence.cpp b/lib/persistence.cpp
index c4ee142..4f1be8f 100644
--- a/lib/persistence.cpp
+++ b/lib/persistence.cpp
@@ -78,5 +78,11 @@ namespace Persistence {
Selection::endObject(Stack &)
{
}
+
+ void
+ Selection::write(const Writer &) const
+ {
+ throw std::logic_error("Default write op shouldn't ever get called");
+ }
/// LCOV_EXCL_STOP
}
diff --git a/lib/persistence.h b/lib/persistence.h
index 519cb0e..4b72eb3 100644
--- a/lib/persistence.h
+++ b/lib/persistence.h
@@ -12,6 +12,7 @@
#include <string>
#include <string_view>
#include <type_traits>
+#include <typeinfo>
#include <utility>
#include <vector>
@@ -24,6 +25,19 @@ namespace Persistence {
using SelectionPtr = std::unique_ptr<Selection>;
using Stack = std::stack<SelectionPtr>;
+ struct Writer {
+ virtual void beginObject() const = 0;
+ virtual void beginArray() const = 0;
+ virtual void pushValue(bool value) const = 0;
+ virtual void pushValue(float value) const = 0;
+ virtual void pushValue(std::nullptr_t) const = 0;
+ virtual void pushValue(std::string_view value) const = 0;
+ virtual void nextValue() const = 0;
+ virtual void pushKey(std::string_view k) const = 0;
+ virtual void endArray() const = 0;
+ virtual void endObject() const = 0;
+ };
+
struct Selection {
Selection() = default;
virtual ~Selection() = default;
@@ -38,6 +52,8 @@ namespace Persistence {
virtual void endObject(Stack &);
virtual void beforeValue(Stack &);
[[nodiscard]] virtual SelectionPtr select(const std::string &);
+
+ virtual void write(const Writer &) const;
};
template<typename T> struct SelectionT;
@@ -75,25 +91,90 @@ namespace Persistence {
{
std::swap(this->v, evalue);
}
+
+ void
+ write(const Writer & out) const override
+ {
+ out.pushValue(this->v);
+ }
};
struct PersistenceStore {
- template<typename T> [[nodiscard]] inline bool persistType() const;
+ template<typename T> [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti);
+ enum class NameAction { Push, HandleAndContinue, Ignore };
template<typename T>
[[nodiscard]] inline bool
persistValue(const std::string_view key, T & value)
{
- if (key == name) {
+ const auto act {setName(key)};
+ if (act != NameAction::Ignore) {
sel = SelectionV<T>::make(value);
- return false;
+ if (act == NameAction::HandleAndContinue) {
+ selHandler();
+ }
}
- return true;
+ return (act != NameAction::Push);
}
- const std::string & name;
+
+ virtual NameAction setName(const std::string_view key) = 0;
+ virtual void selHandler() {};
+ virtual void setType(const std::string_view) = 0;
+
SelectionPtr sel {};
};
+ struct PersistenceSelect : public PersistenceStore {
+ explicit PersistenceSelect(const std::string & n) : name {n} { }
+
+ NameAction
+ setName(const std::string_view key) override
+ {
+ return (key == name) ? NameAction::Push : NameAction::Ignore;
+ }
+
+ void
+ setType(const std::string_view) override
+ {
+ }
+
+ const std::string & name;
+ };
+
+ struct PersistenceWrite : public PersistenceStore {
+ explicit PersistenceWrite(const Writer & o) : out {o} { }
+
+ NameAction
+ setName(const std::string_view key) override
+ {
+ if (!first) {
+ out.nextValue();
+ }
+ else {
+ first = false;
+ }
+ out.pushKey(key);
+ return NameAction::HandleAndContinue;
+ }
+
+ void
+ selHandler() override
+ {
+ this->sel->write(out);
+ };
+
+ void
+ setType(const std::string_view tn) override
+ {
+ out.pushKey("@typeid");
+ out.pushValue(tn);
+ first = false;
+ }
+
+ bool first {true};
+ const Writer & out;
+ };
+
template<glm::length_t L, typename T, glm::qualifier Q>
struct SelectionT<glm::vec<L, T, Q>> : public SelectionV<glm::vec<L, T, Q>> {
using V = glm::vec<L, T, Q>;
@@ -108,6 +189,17 @@ namespace Persistence {
}
glm::length_t idx {0};
+
+ void
+ write(const Writer & out) const override
+ {
+ for (glm::length_t idx = 0; idx < L; idx += 1) {
+ if (idx) {
+ out.nextValue();
+ }
+ SelectionT<T> {this->v[idx]}.write(out);
+ }
+ }
};
using SelectionV<V>::SelectionV;
@@ -117,6 +209,14 @@ namespace Persistence {
{
stk.push(this->template make_s<Members>(this->v));
}
+
+ void
+ write(const Writer & out) const override
+ {
+ out.beginArray();
+ Members {this->v}.write(out);
+ out.endArray();
+ }
};
template<typename T> struct SelectionT<std::vector<T>> : public SelectionV<std::vector<T>> {
@@ -130,6 +230,17 @@ namespace Persistence {
{
stk.push(SelectionV<T>::make(this->v.emplace_back()));
}
+
+ void
+ write(const Writer & out) const override
+ {
+ for (std::size_t idx = 0; idx < this->v.size(); idx += 1) {
+ if (idx) {
+ out.nextValue();
+ }
+ SelectionT<T> {this->v[idx]}.write(out);
+ }
+ }
};
using SelectionV<V>::SelectionV;
@@ -139,6 +250,14 @@ namespace Persistence {
{
stk.push(this->template make_s<Members>(this->v));
}
+
+ void
+ write(const Writer & out) const override
+ {
+ out.beginArray();
+ Members {this->v}.write(out);
+ out.endArray();
+ }
};
struct Persistable {
@@ -173,11 +292,14 @@ namespace Persistence {
template<typename T>
inline bool
- PersistenceStore::persistType() const
+ PersistenceStore::persistType(const T * const, const std::type_info & ti)
{
if constexpr (!std::is_abstract_v<T>) {
[[maybe_unused]] constexpr auto f = &Persistable::addFactory<T>;
}
+ if (typeid(std::decay_t<T>) == ti) {
+ setType(Persistable::typeName<T>());
+ }
return true;
}
@@ -240,7 +362,7 @@ namespace Persistence {
}
}
make_default_as_needed(this->v);
- PersistenceStore ps {mbr};
+ PersistenceSelect ps {mbr};
if (this->v->persist(ps)) {
throw std::runtime_error("cannot find member: " + mbr);
}
@@ -253,6 +375,15 @@ namespace Persistence {
make_default_as_needed(this->v);
stk.pop();
}
+
+ void
+ write(const Writer & out) const override
+ {
+ out.beginObject();
+ PersistenceWrite pw {out};
+ this->v->persist(pw);
+ out.endObject();
+ }
};
static inline void
@@ -289,6 +420,17 @@ namespace Persistence {
{
stk.pop();
}
+
+ void
+ write(const Writer & out) const override
+ {
+ if (this->v) {
+ SelectionObj {this->v}.write(out);
+ }
+ else {
+ out.pushValue(nullptr);
+ }
+ }
};
template<typename T> struct SelectionT<std::unique_ptr<T>> : public SelectionPtrBase<std::unique_ptr<T>, false> {
@@ -311,7 +453,7 @@ namespace Persistence {
};
}
-#define STORE_TYPE store.persistType<std::decay_t<decltype(*this)>>()
+#define STORE_TYPE store.persistType(this, typeid(*this))
#define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr)
#endif