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/persistance.h | 373 ------------------------------------------------------ 1 file changed, 373 deletions(-) delete mode 100644 lib/persistance.h (limited to 'lib/persistance.h') 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 -- cgit v1.2.3