summaryrefslogtreecommitdiff
path: root/lib/persistence.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-03-02 18:36:34 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-03-02 18:36:34 +0000
commit17ac090dd1dd245cf1e24b62b7333ba9be571bde (patch)
tree488bb1dae6478dd72cc3cb8a433cbac736f9c387 /lib/persistence.h
parentParse colour values as they're read (diff)
downloadilt-17ac090dd1dd245cf1e24b62b7333ba9be571bde.tar.bz2
ilt-17ac090dd1dd245cf1e24b62b7333ba9be571bde.tar.xz
ilt-17ac090dd1dd245cf1e24b62b7333ba9be571bde.zip
Add ParseBase
Acts as a base class for persistence parser, encompasses the parse stack and manages shared objects
Diffstat (limited to 'lib/persistence.h')
-rw-r--r--lib/persistence.h35
1 files changed, 31 insertions, 4 deletions
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 {