diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/jsonParse-persistence.h | 4 | ||||
-rw-r--r-- | lib/persistence.cpp | 5 | ||||
-rw-r--r-- | lib/persistence.h | 35 | ||||
-rw-r--r-- | lib/saxParse-persistence.h | 5 |
4 files changed, 38 insertions, 11 deletions
diff --git a/lib/jsonParse-persistence.h b/lib/jsonParse-persistence.h index fa5e772..a676282 100644 --- a/lib/jsonParse-persistence.h +++ b/lib/jsonParse-persistence.h @@ -9,7 +9,7 @@ #include <string_view> namespace Persistence { - class JsonParsePersistence : public json::jsonParser { + class JsonParsePersistence : public json::jsonParser, ParseBase { public: template<typename T> inline T @@ -34,8 +34,6 @@ namespace Persistence { void endArray() override; void endObject() override; - Stack stk; - template<typename T> inline void pushValue(T && value); inline SelectionPtr & current(); }; diff --git a/lib/persistence.cpp b/lib/persistence.cpp index 543f2fd..8c7c6a4 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -157,4 +157,9 @@ namespace Persistence { throw std::logic_error("Default write op shouldn't ever get called"); } /// LCOV_EXCL_STOP + + ParseBase::ParseBase() : sharedObjectsInstance {std::make_shared<SharedObjects>()} + { + sharedObjects = sharedObjectsInstance; + } } diff --git a/lib/persistence.h b/lib/persistence.h index 458ba4d..05cb49b 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -375,9 +375,36 @@ namespace Persistence { return true; } + class ParseBase { + public: + using SharedObjects = std::map<std::string, std::shared_ptr<Persistable>>; + using SharedObjectsWPtr = std::weak_ptr<SharedObjects>; + using SharedObjectsPtr = std::shared_ptr<SharedObjects>; + + ParseBase(); + DEFAULT_MOVE_NO_COPY(ParseBase); + + template<typename T> + static auto + getShared(auto && k) + { + return std::dynamic_pointer_cast<T>(Persistence::ParseBase::sharedObjects.lock()->at(k)); + } + template<typename... T> + static auto + emplaceShared(T &&... v) + { + return sharedObjects.lock()->emplace(std::forward<T>(v)...); + } + + protected: + Stack stk; + + private: + inline static thread_local SharedObjectsWPtr sharedObjects; + SharedObjectsPtr sharedObjectsInstance; + }; // TODO Move these - using SharedObjects = std::map<std::string, std::shared_ptr<Persistable>>; - inline SharedObjects sharedObjects; using SeenSharedObjects = std::map<void *, std::string>; inline SeenSharedObjects seenSharedObjects; @@ -417,7 +444,7 @@ namespace Persistence { void setValue(std::string && id) override { - sharedObjects.emplace(id, this->v); + ParseBase::emplaceShared(id, this->v); } }; @@ -538,7 +565,7 @@ namespace Persistence { void setValue(std::string && id) override { - if (auto teo = std::dynamic_pointer_cast<T>(sharedObjects.at(id))) { + if (auto teo = ParseBase::getShared<T>(id)) { this->v = std::move(teo); } else { diff --git a/lib/saxParse-persistence.h b/lib/saxParse-persistence.h index 91daecc..6043b25 100644 --- a/lib/saxParse-persistence.h +++ b/lib/saxParse-persistence.h @@ -6,7 +6,7 @@ #include <mxml.h> namespace Persistence { - class SAXParsePersistence : public SAXParse { + class SAXParsePersistence : public SAXParse, ParseBase { private: template<typename T> struct Root : public Persistable { T t {}; @@ -36,8 +36,5 @@ namespace Persistence { void data(mxml_node_t *) override; void directive(mxml_node_t *) override; void cdata(mxml_node_t *) override; - - private: - Stack stk; }; } |