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 ++++++++++++++++++++++++++++++++++++++++++ test/Jamfile.jam | 2 +- test/test-persistance.cpp | 213 ------------------------ test/test-persistence.cpp | 213 ++++++++++++++++++++++++ 11 files changed, 796 insertions(+), 796 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 delete mode 100644 test/test-persistance.cpp create mode 100644 test/test-persistence.cpp 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 diff --git a/test/Jamfile.jam b/test/Jamfile.jam index cfa1f21..2c09b8a 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -19,4 +19,4 @@ run test-collection.cpp ; run test-obj.cpp ; run test-maths.cpp ; run test-network.cpp ; -run test-persistance.cpp : -- : [ sequence.insertion-sort [ glob fixtures/json/*.json ] ] ; +run test-persistence.cpp : -- : [ sequence.insertion-sort [ glob fixtures/json/*.json ] ] ; diff --git a/test/test-persistance.cpp b/test/test-persistance.cpp deleted file mode 100644 index 2c06c48..0000000 --- a/test/test-persistance.cpp +++ /dev/null @@ -1,213 +0,0 @@ -#define BOOST_TEST_MODULE test_persistance - -#include - -#include -#include -#include -#include -#include -#include -#include - -struct AbsObject : public Persistanace::Persistable { - std::string base; - - bool - persist(Persistanace::PersistanceStore & store) override - { - return STORE_TYPE && STORE_MEMBER(base); - } - - virtual void dummy() const = 0; -}; - -struct SubObject : public AbsObject { - std::string sub; - - bool - persist(Persistanace::PersistanceStore & store) override - { - return AbsObject::persist(store) && STORE_TYPE && STORE_MEMBER(sub); - } - - void - dummy() const override - { - } -}; - -struct TestObject : public Persistanace::Persistable { - TestObject() = default; - - float flt {}; - std::string str {}; - bool bl {}; - glm::vec3 pos {}; - std::vector flts; - std::vector poss; - std::vector>> nest; - std::unique_ptr ptr; - std::unique_ptr aptr; - std::vector> vptr; - - bool - persist(Persistanace::PersistanceStore & store) override - { - return STORE_TYPE && STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos) - && STORE_MEMBER(flts) && STORE_MEMBER(poss) && STORE_MEMBER(nest) && STORE_MEMBER(ptr) - && STORE_MEMBER(aptr) && STORE_MEMBER(vptr); - } -}; - -struct JPP : public Persistanace::JsonParsePersistance { - template - T - load_json(const char * path) - { - BOOST_TEST_CONTEXT(path) { - std::ifstream ss {path}; - auto to = loadState(ss); - Persistanace::sharedObjects.clear(); - BOOST_CHECK(stk.empty()); - BOOST_REQUIRE(to); - return to; - } - // Presumably BOOST_TEST_CONTEXT is implemented as an if (...) { } - throw std::logic_error("We shouldn't ever get here, but apparently we can!"); - } -}; - -BOOST_FIXTURE_TEST_CASE(load_object, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/load_object.json"); - BOOST_CHECK_CLOSE(to->flt, 3.14, 0.01); - BOOST_CHECK_EQUAL(to->str, "Lovely string"); - BOOST_CHECK_EQUAL(to->bl, true); - BOOST_CHECK_CLOSE(to->pos[0], 3.14, 0.01); - BOOST_CHECK_CLOSE(to->pos[1], 6.28, 0.01); - BOOST_CHECK_CLOSE(to->pos[2], 1.57, 0.01); - BOOST_REQUIRE_EQUAL(to->flts.size(), 6); - BOOST_CHECK_CLOSE(to->flts[0], 3.14, 0.01); - BOOST_CHECK_CLOSE(to->flts[1], 6.28, 0.01); - BOOST_CHECK_CLOSE(to->flts[2], 1.57, 0.01); - BOOST_CHECK_CLOSE(to->flts[3], 0, 0.01); - BOOST_CHECK_CLOSE(to->flts[4], -1, 0.01); - BOOST_CHECK_CLOSE(to->flts[5], -3.14, 0.01); - BOOST_REQUIRE_EQUAL(to->poss.size(), 2); - BOOST_CHECK_CLOSE(to->poss[0][0], 3.14, 0.01); - BOOST_CHECK_CLOSE(to->poss[0][1], 6.28, 0.01); - BOOST_CHECK_CLOSE(to->poss[0][2], 1.57, 0.01); - BOOST_CHECK_CLOSE(to->poss[1][0], 0, 0.01); - BOOST_CHECK_CLOSE(to->poss[1][1], -1, 0.01); - BOOST_CHECK_CLOSE(to->poss[1][2], -3.14, 0.01); - BOOST_REQUIRE_EQUAL(to->nest.size(), 3); - BOOST_REQUIRE_EQUAL(to->nest.at(0).size(), 2); - BOOST_REQUIRE_EQUAL(to->nest.at(0).at(0).size(), 2); - BOOST_REQUIRE_EQUAL(to->nest.at(0).at(1).size(), 3); - BOOST_REQUIRE_EQUAL(to->nest.at(1).size(), 1); - BOOST_REQUIRE_EQUAL(to->nest.at(1).at(0).size(), 1); - BOOST_REQUIRE_EQUAL(to->nest.at(2).size(), 0); - BOOST_REQUIRE(to->ptr); - BOOST_CHECK_CLOSE(to->ptr->flt, 3.14, 0.01); - BOOST_CHECK_EQUAL(to->ptr->str, "Lovely string"); -} - -BOOST_FIXTURE_TEST_CASE(load_nested_object, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/nested.json"); - BOOST_CHECK_EQUAL(to->flt, 1.F); - BOOST_CHECK_EQUAL(to->str, "one"); - BOOST_REQUIRE(to->ptr); - BOOST_CHECK_EQUAL(to->ptr->flt, 2.F); - BOOST_CHECK_EQUAL(to->ptr->str, "two"); - BOOST_REQUIRE(to->ptr->ptr); - BOOST_CHECK_EQUAL(to->ptr->ptr->flt, 3.F); - BOOST_CHECK_EQUAL(to->ptr->ptr->str, "three"); - BOOST_REQUIRE(to->ptr->ptr->ptr); - BOOST_CHECK_EQUAL(to->ptr->ptr->ptr->flt, 4.F); - BOOST_CHECK_EQUAL(to->ptr->ptr->ptr->str, "four"); - BOOST_REQUIRE(!to->ptr->ptr->ptr->ptr); -} - -BOOST_FIXTURE_TEST_CASE(load_implicit_object, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/implicit.json"); - BOOST_CHECK(to->ptr); - BOOST_CHECK_EQUAL(to->flt, 1.F); - BOOST_CHECK_EQUAL(to->ptr->str, "trigger"); - BOOST_CHECK_EQUAL(to->str, "after"); -} - -BOOST_FIXTURE_TEST_CASE(load_empty_object, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/empty.json"); - BOOST_CHECK_EQUAL(to->flt, 1.F); - BOOST_CHECK(to->ptr); - BOOST_CHECK_EQUAL(to->str, "after"); -} - -BOOST_FIXTURE_TEST_CASE(fail_implicit_abs_object, JPP) -{ - BOOST_CHECK_THROW(load_json>(FIXTURESDIR "json/implicit_abs.json"), std::runtime_error); -} - -BOOST_FIXTURE_TEST_CASE(fail_empty_abs_object, JPP) -{ - BOOST_CHECK_THROW(load_json>(FIXTURESDIR "json/empty_abs.json"), std::runtime_error); -} - -BOOST_FIXTURE_TEST_CASE(load_abs_object, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/abs.json"); - BOOST_REQUIRE(to->aptr); - BOOST_CHECK_NO_THROW(to->aptr->dummy()); - BOOST_CHECK_EQUAL(to->aptr->base, "set base"); - auto s = dynamic_cast(to->aptr.get()); - BOOST_REQUIRE(s); - BOOST_CHECK_EQUAL(s->sub, "set sub"); -} - -BOOST_FIXTURE_TEST_CASE(load_vector_ptr, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/vector_ptr.json"); - BOOST_CHECK(to->str.empty()); - BOOST_CHECK_EQUAL(to->vptr.size(), 4); - BOOST_CHECK_EQUAL(to->vptr.at(0)->str, "type"); - BOOST_CHECK_CLOSE(to->vptr.at(1)->flt, 3.14, .01); - BOOST_CHECK(!to->vptr.at(2)); - BOOST_CHECK(to->vptr.at(3)->str.empty()); -} - -struct SharedTestObject : public Persistanace::Persistable { - SharedTestObject() = default; - - std::shared_ptr sptr; - std::shared_ptr ssptr; - - bool - persist(Persistanace::PersistanceStore & store) override - { - return STORE_TYPE && STORE_MEMBER(sptr) && STORE_MEMBER(ssptr); - } -}; - -BOOST_FIXTURE_TEST_CASE(load_shared_object1, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/shared_ptr1.json"); - BOOST_CHECK(to->sptr); - BOOST_CHECK(to->ssptr); - BOOST_CHECK_NE(to->sptr, to->ssptr); - BOOST_CHECK_EQUAL(to->sptr.use_count(), 1); - BOOST_CHECK_EQUAL(to->ssptr.use_count(), 1); -} - -BOOST_FIXTURE_TEST_CASE(load_shared_object2, JPP) -{ - auto to = load_json>(FIXTURESDIR "json/shared_ptr2.json"); - BOOST_CHECK(to->sptr); - BOOST_CHECK(to->ssptr); - BOOST_CHECK_EQUAL(to->sptr, to->ssptr); - BOOST_CHECK_EQUAL(to->sptr.use_count(), 2); - BOOST_CHECK_EQUAL(to->ssptr.use_count(), 2); -} diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp new file mode 100644 index 0000000..d5910e9 --- /dev/null +++ b/test/test-persistence.cpp @@ -0,0 +1,213 @@ +#define BOOST_TEST_MODULE test_persistence + +#include + +#include +#include +#include +#include +#include +#include +#include + +struct AbsObject : public Persistence::Persistable { + std::string base; + + bool + persist(Persistence::PersistenceStore & store) override + { + return STORE_TYPE && STORE_MEMBER(base); + } + + virtual void dummy() const = 0; +}; + +struct SubObject : public AbsObject { + std::string sub; + + bool + persist(Persistence::PersistenceStore & store) override + { + return AbsObject::persist(store) && STORE_TYPE && STORE_MEMBER(sub); + } + + void + dummy() const override + { + } +}; + +struct TestObject : public Persistence::Persistable { + TestObject() = default; + + float flt {}; + std::string str {}; + bool bl {}; + glm::vec3 pos {}; + std::vector flts; + std::vector poss; + std::vector>> nest; + std::unique_ptr ptr; + std::unique_ptr aptr; + std::vector> vptr; + + bool + persist(Persistence::PersistenceStore & store) override + { + return STORE_TYPE && STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos) + && STORE_MEMBER(flts) && STORE_MEMBER(poss) && STORE_MEMBER(nest) && STORE_MEMBER(ptr) + && STORE_MEMBER(aptr) && STORE_MEMBER(vptr); + } +}; + +struct JPP : public Persistence::JsonParsePersistence { + template + T + load_json(const char * path) + { + BOOST_TEST_CONTEXT(path) { + std::ifstream ss {path}; + auto to = loadState(ss); + Persistence::sharedObjects.clear(); + BOOST_CHECK(stk.empty()); + BOOST_REQUIRE(to); + return to; + } + // Presumably BOOST_TEST_CONTEXT is implemented as an if (...) { } + throw std::logic_error("We shouldn't ever get here, but apparently we can!"); + } +}; + +BOOST_FIXTURE_TEST_CASE(load_object, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/load_object.json"); + BOOST_CHECK_CLOSE(to->flt, 3.14, 0.01); + BOOST_CHECK_EQUAL(to->str, "Lovely string"); + BOOST_CHECK_EQUAL(to->bl, true); + BOOST_CHECK_CLOSE(to->pos[0], 3.14, 0.01); + BOOST_CHECK_CLOSE(to->pos[1], 6.28, 0.01); + BOOST_CHECK_CLOSE(to->pos[2], 1.57, 0.01); + BOOST_REQUIRE_EQUAL(to->flts.size(), 6); + BOOST_CHECK_CLOSE(to->flts[0], 3.14, 0.01); + BOOST_CHECK_CLOSE(to->flts[1], 6.28, 0.01); + BOOST_CHECK_CLOSE(to->flts[2], 1.57, 0.01); + BOOST_CHECK_CLOSE(to->flts[3], 0, 0.01); + BOOST_CHECK_CLOSE(to->flts[4], -1, 0.01); + BOOST_CHECK_CLOSE(to->flts[5], -3.14, 0.01); + BOOST_REQUIRE_EQUAL(to->poss.size(), 2); + BOOST_CHECK_CLOSE(to->poss[0][0], 3.14, 0.01); + BOOST_CHECK_CLOSE(to->poss[0][1], 6.28, 0.01); + BOOST_CHECK_CLOSE(to->poss[0][2], 1.57, 0.01); + BOOST_CHECK_CLOSE(to->poss[1][0], 0, 0.01); + BOOST_CHECK_CLOSE(to->poss[1][1], -1, 0.01); + BOOST_CHECK_CLOSE(to->poss[1][2], -3.14, 0.01); + BOOST_REQUIRE_EQUAL(to->nest.size(), 3); + BOOST_REQUIRE_EQUAL(to->nest.at(0).size(), 2); + BOOST_REQUIRE_EQUAL(to->nest.at(0).at(0).size(), 2); + BOOST_REQUIRE_EQUAL(to->nest.at(0).at(1).size(), 3); + BOOST_REQUIRE_EQUAL(to->nest.at(1).size(), 1); + BOOST_REQUIRE_EQUAL(to->nest.at(1).at(0).size(), 1); + BOOST_REQUIRE_EQUAL(to->nest.at(2).size(), 0); + BOOST_REQUIRE(to->ptr); + BOOST_CHECK_CLOSE(to->ptr->flt, 3.14, 0.01); + BOOST_CHECK_EQUAL(to->ptr->str, "Lovely string"); +} + +BOOST_FIXTURE_TEST_CASE(load_nested_object, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/nested.json"); + BOOST_CHECK_EQUAL(to->flt, 1.F); + BOOST_CHECK_EQUAL(to->str, "one"); + BOOST_REQUIRE(to->ptr); + BOOST_CHECK_EQUAL(to->ptr->flt, 2.F); + BOOST_CHECK_EQUAL(to->ptr->str, "two"); + BOOST_REQUIRE(to->ptr->ptr); + BOOST_CHECK_EQUAL(to->ptr->ptr->flt, 3.F); + BOOST_CHECK_EQUAL(to->ptr->ptr->str, "three"); + BOOST_REQUIRE(to->ptr->ptr->ptr); + BOOST_CHECK_EQUAL(to->ptr->ptr->ptr->flt, 4.F); + BOOST_CHECK_EQUAL(to->ptr->ptr->ptr->str, "four"); + BOOST_REQUIRE(!to->ptr->ptr->ptr->ptr); +} + +BOOST_FIXTURE_TEST_CASE(load_implicit_object, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/implicit.json"); + BOOST_CHECK(to->ptr); + BOOST_CHECK_EQUAL(to->flt, 1.F); + BOOST_CHECK_EQUAL(to->ptr->str, "trigger"); + BOOST_CHECK_EQUAL(to->str, "after"); +} + +BOOST_FIXTURE_TEST_CASE(load_empty_object, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/empty.json"); + BOOST_CHECK_EQUAL(to->flt, 1.F); + BOOST_CHECK(to->ptr); + BOOST_CHECK_EQUAL(to->str, "after"); +} + +BOOST_FIXTURE_TEST_CASE(fail_implicit_abs_object, JPP) +{ + BOOST_CHECK_THROW(load_json>(FIXTURESDIR "json/implicit_abs.json"), std::runtime_error); +} + +BOOST_FIXTURE_TEST_CASE(fail_empty_abs_object, JPP) +{ + BOOST_CHECK_THROW(load_json>(FIXTURESDIR "json/empty_abs.json"), std::runtime_error); +} + +BOOST_FIXTURE_TEST_CASE(load_abs_object, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/abs.json"); + BOOST_REQUIRE(to->aptr); + BOOST_CHECK_NO_THROW(to->aptr->dummy()); + BOOST_CHECK_EQUAL(to->aptr->base, "set base"); + auto s = dynamic_cast(to->aptr.get()); + BOOST_REQUIRE(s); + BOOST_CHECK_EQUAL(s->sub, "set sub"); +} + +BOOST_FIXTURE_TEST_CASE(load_vector_ptr, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/vector_ptr.json"); + BOOST_CHECK(to->str.empty()); + BOOST_CHECK_EQUAL(to->vptr.size(), 4); + BOOST_CHECK_EQUAL(to->vptr.at(0)->str, "type"); + BOOST_CHECK_CLOSE(to->vptr.at(1)->flt, 3.14, .01); + BOOST_CHECK(!to->vptr.at(2)); + BOOST_CHECK(to->vptr.at(3)->str.empty()); +} + +struct SharedTestObject : public Persistence::Persistable { + SharedTestObject() = default; + + std::shared_ptr sptr; + std::shared_ptr ssptr; + + bool + persist(Persistence::PersistenceStore & store) override + { + return STORE_TYPE && STORE_MEMBER(sptr) && STORE_MEMBER(ssptr); + } +}; + +BOOST_FIXTURE_TEST_CASE(load_shared_object1, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/shared_ptr1.json"); + BOOST_CHECK(to->sptr); + BOOST_CHECK(to->ssptr); + BOOST_CHECK_NE(to->sptr, to->ssptr); + BOOST_CHECK_EQUAL(to->sptr.use_count(), 1); + BOOST_CHECK_EQUAL(to->ssptr.use_count(), 1); +} + +BOOST_FIXTURE_TEST_CASE(load_shared_object2, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/shared_ptr2.json"); + BOOST_CHECK(to->sptr); + BOOST_CHECK(to->ssptr); + BOOST_CHECK_EQUAL(to->sptr, to->ssptr); + BOOST_CHECK_EQUAL(to->sptr.use_count(), 2); + BOOST_CHECK_EQUAL(to->ssptr.use_count(), 2); +} -- cgit v1.2.3