#ifndef PERSISTANCE_H #define PERSISTANCE_H #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 { // virtual bool persistType(const std::type_info &) = 0; 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; static void addFactory(const std::string_view, std::function()>); static std::unique_ptr callFactory(const std::string_view); }; 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(); } }; } #define STORE_TYPE store.persistType(typeid(*this)) #define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr) #endif