From a31858d29048735b812d385f75db4ed6a6a94556 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 30 Apr 2021 01:03:42 +0100 Subject: Fix the fact I've been spelling persistence wrong this whole time --- lib/jsonParse-persistance.cpp | 84 ---------- lib/jsonParse-persistance.h | 44 ----- lib/jsonParse-persistence.cpp | 84 ++++++++++ lib/jsonParse-persistence.h | 44 +++++ lib/persistance.cpp | 81 --------- lib/persistance.h | 373 ------------------------------------------ lib/persistence.cpp | 81 +++++++++ lib/persistence.h | 373 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 582 insertions(+), 582 deletions(-) delete mode 100644 lib/jsonParse-persistance.cpp delete mode 100644 lib/jsonParse-persistance.h create mode 100644 lib/jsonParse-persistence.cpp create mode 100644 lib/jsonParse-persistence.h delete mode 100644 lib/persistance.cpp delete mode 100644 lib/persistance.h create mode 100644 lib/persistence.cpp create mode 100644 lib/persistence.h (limited to 'lib') diff --git a/lib/jsonParse-persistance.cpp b/lib/jsonParse-persistance.cpp deleted file mode 100644 index 100fa5c..0000000 --- a/lib/jsonParse-persistance.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "jsonParse-persistance.h" - -namespace Persistanace { - void - JsonParsePersistance::loadState(std::istream & in) - { - this->switch_streams(&in, nullptr); - yy_push_state(0); - yylex(); - } - - void - JsonParsePersistance::beginObject() - { - current()->beforeValue(stk); - current()->beginObject(stk); - } - - void - JsonParsePersistance::beginArray() - { - current()->beforeValue(stk); - 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(); - stk.pop(); - } - - void - JsonParsePersistance::endObject() - { - current()->endObject(stk); - current()->endObject(stk); - } - - template - inline void - JsonParsePersistance::pushValue(T && value) - { - current()->beforeValue(stk); - current()->setValue(value); - stk.pop(); - } - - inline SelectionPtr & - JsonParsePersistance::current() - { - return stk.top(); - } -} diff --git a/lib/jsonParse-persistance.h b/lib/jsonParse-persistance.h deleted file mode 100644 index 37483bc..0000000 --- a/lib/jsonParse-persistance.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef JSONPARSE_PERSISTANCE -#define JSONPARSE_PERSISTANCE - -#include "jsonParse.h" // IWYU pragma: export -#include "persistance.h" // IWYU pragma: export -#include -#include -#include -#include - -namespace Persistanace { - class JsonParsePersistance : public json::jsonParser { - public: - template - inline T - loadState(std::istream & in) - { - T t {}; - stk.push(std::make_unique>(std::ref(t))); - loadState(in); - return t; - } - - protected: - void loadState(std::istream & in); - - 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; - - Stack stk; - - template inline void pushValue(T && value); - inline SelectionPtr & current(); - }; -} - -#endif diff --git a/lib/jsonParse-persistence.cpp b/lib/jsonParse-persistence.cpp new file mode 100644 index 0000000..5b3314e --- /dev/null +++ b/lib/jsonParse-persistence.cpp @@ -0,0 +1,84 @@ +#include "jsonParse-persistence.h" + +namespace Persistence { + void + JsonParsePersistence::loadState(std::istream & in) + { + this->switch_streams(&in, nullptr); + yy_push_state(0); + yylex(); + } + + void + JsonParsePersistence::beginObject() + { + current()->beforeValue(stk); + current()->beginObject(stk); + } + + void + JsonParsePersistence::beginArray() + { + current()->beforeValue(stk); + current()->beginArray(stk); + } + + void + JsonParsePersistence::pushBoolean(bool value) + { + pushValue(value); + } + + void + JsonParsePersistence::pushNumber(float value) + { + pushValue(value); + } + + void + JsonParsePersistence::pushNull() + { + pushValue(nullptr); + } + + void + JsonParsePersistence::pushText(std::string && value) + { + pushValue(value); + } + + void + JsonParsePersistence::pushKey(std::string && k) + { + stk.push(current()->select(k)); + } + + void + JsonParsePersistence::endArray() + { + stk.pop(); + stk.pop(); + } + + void + JsonParsePersistence::endObject() + { + current()->endObject(stk); + current()->endObject(stk); + } + + template + inline void + JsonParsePersistence::pushValue(T && value) + { + current()->beforeValue(stk); + current()->setValue(value); + stk.pop(); + } + + inline SelectionPtr & + JsonParsePersistence::current() + { + return stk.top(); + } +} diff --git a/lib/jsonParse-persistence.h b/lib/jsonParse-persistence.h new file mode 100644 index 0000000..6449f7b --- /dev/null +++ b/lib/jsonParse-persistence.h @@ -0,0 +1,44 @@ +#ifndef JSONPARSE_PERSISTANCE +#define JSONPARSE_PERSISTANCE + +#include "jsonParse.h" // IWYU pragma: export +#include "persistence.h" // IWYU pragma: export +#include +#include +#include +#include + +namespace Persistence { + class JsonParsePersistence : public json::jsonParser { + public: + template + inline T + loadState(std::istream & in) + { + T t {}; + stk.push(std::make_unique>(std::ref(t))); + loadState(in); + return t; + } + + protected: + void loadState(std::istream & in); + + 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; + + Stack stk; + + template inline void pushValue(T && value); + inline SelectionPtr & current(); + }; +} + +#endif diff --git a/lib/persistance.cpp b/lib/persistance.cpp deleted file mode 100644 index 204e8f0..0000000 --- a/lib/persistance.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include "persistance.h" -#include - -namespace Persistanace { - using Factories - = std::pair()>, std::function()>>; - using NamedTypeFactories = std::map; - static NamedTypeFactories namedTypeFactories; - - void - Persistable::addFactory(const std::string_view t, std::function()> fu, - std::function()> fs) - { - namedTypeFactories.emplace(t, std::make_pair(std::move(fu), std::move(fs))); - } - - std::unique_ptr - Persistable::callFactory(const std::string_view t) - { - return namedTypeFactories.at(t).first(); - } - - std::shared_ptr - Persistable::callSharedFactory(const std::string_view t) - { - return namedTypeFactories.at(t).second(); - } - - void - Selection::setValue(float &) - { - throw std::runtime_error("Unexpected float"); - } - - void - Selection::setValue(bool &) - { - throw std::runtime_error("Unexpected bool"); - } - - void - Selection::setValue(const std::nullptr_t &) - { - throw std::runtime_error("Unexpected null"); - } - - void - Selection::setValue(std::string &) - { - throw std::runtime_error("Unexpected string"); - } - - void - Selection::beginArray(Stack &) - { - throw std::runtime_error("Unexpected array"); - } - - void - Selection::beginObject(Stack &) - { - throw std::runtime_error("Unexpected object"); - } - - void - Selection::beforeValue(Stack &) - { - throw std::runtime_error("Unexpected value"); - } - - SelectionPtr - Selection::select(const std::string &) - { - throw std::runtime_error("Unexpected select"); - } - - void - Selection::endObject(Stack &) - { - } -} diff --git a/lib/persistance.h b/lib/persistance.h deleted file mode 100644 index c64d4ec..0000000 --- a/lib/persistance.h +++ /dev/null @@ -1,373 +0,0 @@ -#ifndef PERSISTANCE_H -#define PERSISTANCE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace glm { - template struct vec; -} - -namespace Persistanace { - struct Selection; - using SelectionPtr = std::unique_ptr; - using Stack = std::stack; - - struct Selection { - Selection() = default; - virtual ~Selection() = default; - DEFAULT_MOVE_COPY(Selection); - - virtual void setValue(float &); - virtual void setValue(bool &); - virtual void setValue(const std::nullptr_t &); - virtual void setValue(std::string &); - virtual void beginArray(Stack &); - virtual void beginObject(Stack &); - virtual void endObject(Stack &); - virtual void beforeValue(Stack &); - virtual SelectionPtr select(const std::string &); - }; - - template struct SelectionT; - - template struct SelectionV : public Selection { - explicit SelectionV(T & value) : v {value} { } - - void - beforeValue(Stack &) override - { - } - - static SelectionPtr - make(T & value) - { - return make_s>(value); - } - - template - static SelectionPtr - make_s(T & value) - { - return std::make_unique(value); - } - - T & v; - }; - - template struct SelectionT : public SelectionV { - using SelectionV::SelectionV; - - void - setValue(T & evalue) override - { - std::swap(this->v, evalue); - } - }; - - struct PersistanceStore { - template inline bool persistType() const; - - template - inline bool - persistValue(const std::string_view key, T & value) - { - if (key == name) { - sel = SelectionV::make(value); - return false; - } - return true; - } - const std::string & name; - SelectionPtr sel {}; - }; - - template - struct SelectionT> : public SelectionV> { - using V = glm::vec; - - struct Members : public SelectionV { - using SelectionV::SelectionV; - - void - beforeValue(Stack & stk) override - { - stk.push(SelectionV::make(this->v[idx++])); - } - - glm::length_t idx {0}; - }; - - using SelectionV::SelectionV; - - void - beginArray(Stack & stk) override - { - stk.push(this->template make_s(this->v)); - } - }; - - template struct SelectionT> : public SelectionV> { - using V = std::vector; - - struct Members : public SelectionV { - using SelectionV::SelectionV; - - void - beforeValue(Stack & stk) override - { - stk.push(SelectionV::make(this->v.emplace_back())); - } - }; - - using SelectionV::SelectionV; - - void - beginArray(Stack & stk) override - { - stk.push(this->template make_s(this->v)); - } - }; - - struct Persistable { - Persistable() = default; - virtual ~Persistable() = default; - DEFAULT_MOVE_COPY(Persistable); - - virtual bool persist(PersistanceStore & store) = 0; - - template - constexpr static auto - typeName() - { - constexpr std::string_view name {__PRETTY_FUNCTION__}; - constexpr auto s {name.find("T = ") + 4}, e {name.rfind(']')}; - return name.substr(s, e - s); - } - - template static void addFactory() __attribute__((constructor)); - static void addFactory(const std::string_view, std::function()>, - std::function()>); - static std::unique_ptr callFactory(const std::string_view); - static std::shared_ptr callSharedFactory(const std::string_view); - }; - - template - void - Persistable::addFactory() - { - addFactory(typeName(), std::make_unique, std::make_shared); - } - - template - inline bool - PersistanceStore::persistType() const - { - if constexpr (!std::is_abstract_v) { - [[maybe_unused]] constexpr auto f = &Persistable::addFactory; - } - return true; - } - - template struct SelectionT> : public SelectionV> { - using Ptr = std::unique_ptr; - 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())) { - this->v.reset(static_cast(no.release())); - } - } - }; - - using SelectionV::SelectionV; - - SelectionPtr - select(const std::string & mbr) override - { - using namespace std::literals; - if (mbr == "@typeid"sv) { - if (this->v) { - throw std::runtime_error("cannot set object type after creation"); - } - return this->template make_s(this->v); - } - else { - if (!this->v) { - if constexpr (std::is_abstract_v) { - throw std::runtime_error("cannot select member of null object"); - } - else { - this->v = std::make_unique(); - } - } - PersistanceStore ps {mbr}; - if (this->v->persist(ps)) { - throw std::runtime_error("cannot find member: " + mbr); - } - return std::move(ps.sel); - } - } - - void - endObject(Stack & stk) override - { - if (!this->v) { - if constexpr (std::is_abstract_v) { - throw std::runtime_error("cannot default create abstract object"); - } - else { - this->v = std::make_unique(); - } - } - stk.pop(); - } - }; - - using SelectionV::SelectionV; - - void - setValue(const std::nullptr_t &) override - { - this->v.reset(); - } - - void - beginObject(Stack & stk) override - { - stk.push(this->template make_s(this->v)); - } - - void - endObject(Stack & stk) override - { - stk.pop(); - } - }; - - // TODO Move this - using SharedObjects = std::map>; - inline SharedObjects sharedObjects; - - template struct SelectionT> : public SelectionV> { - using Ptr = std::shared_ptr; - struct SelectionObj : public SelectionV { - struct MakeObjectByTypeName : public SelectionV { - using SelectionV::SelectionV; - - void - setValue(std::string & type) override - { - auto no = Persistable::callSharedFactory(type); - if (auto tno = std::dynamic_pointer_cast(no)) { - this->v = std::move(tno); - } - } - }; - - struct RememberObjectById : public SelectionV { - using SelectionV::SelectionV; - - void - setValue(std::string & id) override - { - sharedObjects.emplace(id, this->v); - } - }; - - using SelectionV::SelectionV; - - SelectionPtr - select(const std::string & mbr) override - { - using namespace std::literals; - if (mbr == "@typeid"sv) { - if (this->v) { - throw std::runtime_error("cannot set object type after creation"); - } - return this->template make_s(this->v); - } - else if (mbr == "@id"sv) { - return this->template make_s(this->v); - } - else { - if (!this->v) { - if constexpr (std::is_abstract_v) { - throw std::runtime_error("cannot select member of null object"); - } - else { - this->v = std::make_shared(); - } - } - PersistanceStore ps {mbr}; - if (this->v->persist(ps)) { - throw std::runtime_error("cannot find member: " + mbr); - } - return std::move(ps.sel); - } - } - - void - endObject(Stack & stk) override - { - if (!this->v) { - if constexpr (std::is_abstract_v) { - throw std::runtime_error("cannot default create abstract object"); - } - else { - this->v = std::make_shared(); - } - } - stk.pop(); - } - }; - - using SelectionV::SelectionV; - - void - setValue(const std::nullptr_t &) override - { - this->v.reset(); - } - - void - setValue(std::string & id) override - { - if (auto teo = std::dynamic_pointer_cast(sharedObjects.at(id))) { - this->v = std::move(teo); - } - } - - void - beginObject(Stack & stk) override - { - stk.push(this->template make_s(this->v)); - } - - void - endObject(Stack & stk) override - { - stk.pop(); - } - }; -} - -#define STORE_TYPE store.persistType>() -#define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr) - -#endif diff --git a/lib/persistence.cpp b/lib/persistence.cpp new file mode 100644 index 0000000..5744cd7 --- /dev/null +++ b/lib/persistence.cpp @@ -0,0 +1,81 @@ +#include "persistence.h" +#include + +namespace Persistence { + using Factories + = std::pair()>, std::function()>>; + using NamedTypeFactories = std::map; + static NamedTypeFactories namedTypeFactories; + + void + Persistable::addFactory(const std::string_view t, std::function()> fu, + std::function()> fs) + { + namedTypeFactories.emplace(t, std::make_pair(std::move(fu), std::move(fs))); + } + + std::unique_ptr + Persistable::callFactory(const std::string_view t) + { + return namedTypeFactories.at(t).first(); + } + + std::shared_ptr + Persistable::callSharedFactory(const std::string_view t) + { + return namedTypeFactories.at(t).second(); + } + + void + Selection::setValue(float &) + { + throw std::runtime_error("Unexpected float"); + } + + void + Selection::setValue(bool &) + { + throw std::runtime_error("Unexpected bool"); + } + + void + Selection::setValue(const std::nullptr_t &) + { + throw std::runtime_error("Unexpected null"); + } + + void + Selection::setValue(std::string &) + { + throw std::runtime_error("Unexpected string"); + } + + void + Selection::beginArray(Stack &) + { + throw std::runtime_error("Unexpected array"); + } + + void + Selection::beginObject(Stack &) + { + throw std::runtime_error("Unexpected object"); + } + + void + Selection::beforeValue(Stack &) + { + throw std::runtime_error("Unexpected value"); + } + + SelectionPtr + Selection::select(const std::string &) + { + throw std::runtime_error("Unexpected select"); + } + + void + Selection::endObject(Stack &) + { + } +} diff --git a/lib/persistence.h b/lib/persistence.h new file mode 100644 index 0000000..ed21ec8 --- /dev/null +++ b/lib/persistence.h @@ -0,0 +1,373 @@ +#ifndef PERSISTANCE_H +#define PERSISTANCE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace glm { + template struct vec; +} + +namespace Persistence { + struct Selection; + using SelectionPtr = std::unique_ptr; + using Stack = std::stack; + + struct Selection { + Selection() = default; + virtual ~Selection() = default; + DEFAULT_MOVE_COPY(Selection); + + virtual void setValue(float &); + virtual void setValue(bool &); + virtual void setValue(const std::nullptr_t &); + virtual void setValue(std::string &); + virtual void beginArray(Stack &); + virtual void beginObject(Stack &); + virtual void endObject(Stack &); + virtual void beforeValue(Stack &); + virtual SelectionPtr select(const std::string &); + }; + + template struct SelectionT; + + template struct SelectionV : public Selection { + explicit SelectionV(T & value) : v {value} { } + + void + beforeValue(Stack &) override + { + } + + static SelectionPtr + make(T & value) + { + return make_s>(value); + } + + template + static SelectionPtr + make_s(T & value) + { + return std::make_unique(value); + } + + T & v; + }; + + template struct SelectionT : public SelectionV { + using SelectionV::SelectionV; + + void + setValue(T & evalue) override + { + std::swap(this->v, evalue); + } + }; + + struct PersistenceStore { + template inline bool persistType() const; + + template + inline bool + persistValue(const std::string_view key, T & value) + { + if (key == name) { + sel = SelectionV::make(value); + return false; + } + return true; + } + const std::string & name; + SelectionPtr sel {}; + }; + + template + struct SelectionT> : public SelectionV> { + using V = glm::vec; + + struct Members : public SelectionV { + using SelectionV::SelectionV; + + void + beforeValue(Stack & stk) override + { + stk.push(SelectionV::make(this->v[idx++])); + } + + glm::length_t idx {0}; + }; + + using SelectionV::SelectionV; + + void + beginArray(Stack & stk) override + { + stk.push(this->template make_s(this->v)); + } + }; + + template struct SelectionT> : public SelectionV> { + using V = std::vector; + + struct Members : public SelectionV { + using SelectionV::SelectionV; + + void + beforeValue(Stack & stk) override + { + stk.push(SelectionV::make(this->v.emplace_back())); + } + }; + + using SelectionV::SelectionV; + + void + beginArray(Stack & stk) override + { + stk.push(this->template make_s(this->v)); + } + }; + + struct Persistable { + Persistable() = default; + virtual ~Persistable() = default; + DEFAULT_MOVE_COPY(Persistable); + + virtual bool persist(PersistenceStore & store) = 0; + + template + constexpr static auto + typeName() + { + constexpr std::string_view name {__PRETTY_FUNCTION__}; + constexpr auto s {name.find("T = ") + 4}, e {name.rfind(']')}; + return name.substr(s, e - s); + } + + template static void addFactory() __attribute__((constructor)); + static void addFactory(const std::string_view, std::function()>, + std::function()>); + static std::unique_ptr callFactory(const std::string_view); + static std::shared_ptr callSharedFactory(const std::string_view); + }; + + template + void + Persistable::addFactory() + { + addFactory(typeName(), std::make_unique, std::make_shared); + } + + template + inline bool + PersistenceStore::persistType() const + { + if constexpr (!std::is_abstract_v) { + [[maybe_unused]] constexpr auto f = &Persistable::addFactory; + } + return true; + } + + template struct SelectionT> : public SelectionV> { + using Ptr = std::unique_ptr; + 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())) { + this->v.reset(static_cast(no.release())); + } + } + }; + + using SelectionV::SelectionV; + + SelectionPtr + select(const std::string & mbr) override + { + using namespace std::literals; + if (mbr == "@typeid"sv) { + if (this->v) { + throw std::runtime_error("cannot set object type after creation"); + } + return this->template make_s(this->v); + } + else { + if (!this->v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot select member of null object"); + } + else { + this->v = std::make_unique(); + } + } + PersistenceStore ps {mbr}; + if (this->v->persist(ps)) { + throw std::runtime_error("cannot find member: " + mbr); + } + return std::move(ps.sel); + } + } + + void + endObject(Stack & stk) override + { + if (!this->v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot default create abstract object"); + } + else { + this->v = std::make_unique(); + } + } + stk.pop(); + } + }; + + using SelectionV::SelectionV; + + void + setValue(const std::nullptr_t &) override + { + this->v.reset(); + } + + void + beginObject(Stack & stk) override + { + stk.push(this->template make_s(this->v)); + } + + void + endObject(Stack & stk) override + { + stk.pop(); + } + }; + + // TODO Move this + using SharedObjects = std::map>; + inline SharedObjects sharedObjects; + + template struct SelectionT> : public SelectionV> { + using Ptr = std::shared_ptr; + struct SelectionObj : public SelectionV { + struct MakeObjectByTypeName : public SelectionV { + using SelectionV::SelectionV; + + void + setValue(std::string & type) override + { + auto no = Persistable::callSharedFactory(type); + if (auto tno = std::dynamic_pointer_cast(no)) { + this->v = std::move(tno); + } + } + }; + + struct RememberObjectById : public SelectionV { + using SelectionV::SelectionV; + + void + setValue(std::string & id) override + { + sharedObjects.emplace(id, this->v); + } + }; + + using SelectionV::SelectionV; + + SelectionPtr + select(const std::string & mbr) override + { + using namespace std::literals; + if (mbr == "@typeid"sv) { + if (this->v) { + throw std::runtime_error("cannot set object type after creation"); + } + return this->template make_s(this->v); + } + else if (mbr == "@id"sv) { + return this->template make_s(this->v); + } + else { + if (!this->v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot select member of null object"); + } + else { + this->v = std::make_shared(); + } + } + PersistenceStore ps {mbr}; + if (this->v->persist(ps)) { + throw std::runtime_error("cannot find member: " + mbr); + } + return std::move(ps.sel); + } + } + + void + endObject(Stack & stk) override + { + if (!this->v) { + if constexpr (std::is_abstract_v) { + throw std::runtime_error("cannot default create abstract object"); + } + else { + this->v = std::make_shared(); + } + } + stk.pop(); + } + }; + + using SelectionV::SelectionV; + + void + setValue(const std::nullptr_t &) override + { + this->v.reset(); + } + + void + setValue(std::string & id) override + { + if (auto teo = std::dynamic_pointer_cast(sharedObjects.at(id))) { + this->v = std::move(teo); + } + } + + void + beginObject(Stack & stk) override + { + stk.push(this->template make_s(this->v)); + } + + void + endObject(Stack & stk) override + { + stk.pop(); + } + }; +} + +#define STORE_TYPE store.persistType>() +#define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr) + +#endif -- cgit v1.2.3