summaryrefslogtreecommitdiff
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
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
-rw-r--r--assetFactory/assetFactory.cpp2
-rw-r--r--assetFactory/style.cpp3
-rw-r--r--assetFactory/use.cpp3
-rw-r--r--lib/jsonParse-persistence.h4
-rw-r--r--lib/persistence.cpp5
-rw-r--r--lib/persistence.h35
-rw-r--r--lib/saxParse-persistence.h5
-rw-r--r--test/test-persistence.cpp10
8 files changed, 45 insertions, 22 deletions
diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp
index 70f5337..b9724c9 100644
--- a/assetFactory/assetFactory.cpp
+++ b/assetFactory/assetFactory.cpp
@@ -68,7 +68,7 @@ AssetFactory::parseColour(std::string_view in) const
});
return out;
}
- if (auto mf = std::dynamic_pointer_cast<const AssetFactory>(Persistence::sharedObjects.at("assetFactory"))) {
+ if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) {
return {colour->second, 1};
}
diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp
index b2a2cf7..fc5c34e 100644
--- a/assetFactory/style.cpp
+++ b/assetFactory/style.cpp
@@ -36,8 +36,7 @@ Style::persist(Persistence::PersistenceStore & store)
void
setValue(std::string && str) override
{
- if (auto mf
- = std::dynamic_pointer_cast<const AssetFactory>(Persistence::sharedObjects.at("assetFactory"))) {
+ if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
v = mf->parseColour(str);
}
}
diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp
index 53fc9b9..708e310 100644
--- a/assetFactory/use.cpp
+++ b/assetFactory/use.cpp
@@ -18,8 +18,7 @@ struct Lookup : public Persistence::SelectionV<Shape::CPtr> {
void
setValue(std::string && str) override
{
- if (auto mf = std::dynamic_pointer_cast<const AssetFactory>(
- Persistence::sharedObjects.at("assetFactory"))) {
+ if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
v = mf->shapes.at(str);
}
}
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;
};
}
diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp
index 6bee010..7bca91a 100644
--- a/test/test-persistence.cpp
+++ b/test/test-persistence.cpp
@@ -83,16 +83,14 @@ struct TestObject : public Persistence::Persistable {
}
};
-struct JPP : public Persistence::JsonParsePersistence {
+struct JPP {
template<typename T>
T
load_json(const std::filesystem::path & path)
{
BOOST_TEST_CONTEXT(path) {
std::ifstream ss {path};
- auto to = loadState<T>(ss);
- Persistence::sharedObjects.clear();
- BOOST_CHECK(stk.empty());
+ auto to = Persistence::JsonParsePersistence {}.loadState<T>(ss);
BOOST_REQUIRE(to);
return to;
}
@@ -289,10 +287,10 @@ auto const TEST_STRINGS_DECODE_ONLY = boost::unit_test::data::make<svs>({
{R"J("\u056b ARMENIAN SMALL LETTER INI")J", "ի ARMENIAN SMALL LETTER INI"},
{R"J("\u0833 SAMARITAN PUNCTUATION BAU")J", "࠳ SAMARITAN PUNCTUATION BAU"},
});
-BOOST_DATA_TEST_CASE_F(JPP, load_strings, TEST_STRINGS + TEST_STRINGS_DECODE_ONLY, in, exp)
+BOOST_DATA_TEST_CASE(load_strings, TEST_STRINGS + TEST_STRINGS_DECODE_ONLY, in, exp)
{
std::stringstream str {in};
- BOOST_CHECK_EQUAL(loadState<std::string>(str), exp);
+ BOOST_CHECK_EQUAL(Persistence::JsonParsePersistence {}.loadState<std::string>(str), exp);
}
using cpstr = std::tuple<unsigned long, std::string_view>;