summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-04-25 19:39:26 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-11-07 16:41:37 +0000
commit8e8d707db19c12ffa7c018206ebe2582f0d86355 (patch)
tree68f9c54942730729db671286862247e8d3b42a82
parentErring toward complete JSON loader (diff)
downloadilt-8e8d707db19c12ffa7c018206ebe2582f0d86355.tar.bz2
ilt-8e8d707db19c12ffa7c018206ebe2582f0d86355.tar.xz
ilt-8e8d707db19c12ffa7c018206ebe2582f0d86355.zip
Support chaining persist calls for subclasses
-rw-r--r--lib/persistance.h11
-rw-r--r--test/test-persistance.cpp13
2 files changed, 13 insertions, 11 deletions
diff --git a/lib/persistance.h b/lib/persistance.h
index 7f28ad1..0082d4c 100644
--- a/lib/persistance.h
+++ b/lib/persistance.h
@@ -59,8 +59,8 @@ namespace Persistanace {
struct PersistanceStore {
// virtual bool persistType(const std::type_info &) = 0;
template<typename T>
- bool
- persistValue(const std::string_view & key, T & value)
+ inline bool
+ persistValue(const std::string_view key, T & value)
{
if (key == name) {
sel = std::make_unique<SelectionT<T>>(std::ref(value));
@@ -115,7 +115,7 @@ namespace Persistanace {
virtual ~Persistable() = default;
DEFAULT_MOVE_COPY(Persistable);
- virtual void persist(PersistanceStore & store) = 0;
+ virtual bool persist(PersistanceStore & store) = 0;
static void addFactory(const std::string_view, std::function<std::unique_ptr<Persistable>()>);
static std::unique_ptr<Persistable> callFactory(const std::string_view);
@@ -166,7 +166,9 @@ namespace Persistanace {
}
}
PersistanceStore ps {mbr};
- v->persist(ps);
+ if (v->persist(ps)) {
+ throw std::runtime_error("cannot find member: " + mbr);
+ }
return std::move(ps.sel);
}
}
@@ -206,6 +208,7 @@ namespace Persistanace {
{
stk.push(std::make_unique<SelectionObj>(v));
}
+
void
EndObject(Stack & stk) override
{
diff --git a/test/test-persistance.cpp b/test/test-persistance.cpp
index fb90de2..ffc8a3c 100644
--- a/test/test-persistance.cpp
+++ b/test/test-persistance.cpp
@@ -13,10 +13,10 @@
struct AbsObject : public Persistanace::Persistable {
std::string base;
- void
+ bool
persist(Persistanace::PersistanceStore & store) override
{
- STORE_MEMBER(base);
+ return STORE_MEMBER(base);
}
virtual void dummy() const = 0;
@@ -25,11 +25,10 @@ struct AbsObject : public Persistanace::Persistable {
struct SubObject : public AbsObject {
std::string sub;
- void
+ bool
persist(Persistanace::PersistanceStore & store) override
{
- AbsObject::persist(store);
- STORE_MEMBER(sub);
+ return AbsObject::persist(store) && STORE_MEMBER(sub);
}
void
@@ -52,10 +51,10 @@ struct TestObject : public Persistanace::Persistable {
std::unique_ptr<AbsObject> aptr;
std::vector<std::unique_ptr<TestObject>> vptr;
- void
+ bool
persist(Persistanace::PersistanceStore & store) override
{
- STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos) && STORE_MEMBER(flts)
+ return STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos) && STORE_MEMBER(flts)
&& STORE_MEMBER(poss) && STORE_MEMBER(nest) && STORE_MEMBER(ptr) && STORE_MEMBER(aptr)
&& STORE_MEMBER(vptr);
}