From 2d6772cb1c592e4bd75be40f1fdfa924bbbc3c07 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 14 Mar 2023 18:14:54 +0000 Subject: Add postLoad support to persistence --- lib/persistence.cpp | 5 +++++ lib/persistence.h | 10 ++++++++-- test/test-persistence.cpp | 7 +++++++ test/testStructures.cpp | 6 ++++++ test/testStructures.h | 3 +++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/persistence.cpp b/lib/persistence.cpp index 8c7c6a4..06e40c7 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -41,6 +41,11 @@ namespace Persistence { return ss.str(); } + void + Persistable::postLoad() + { + } + PersistenceSelect::PersistenceSelect(const std::string & n) : name {n} { } PersistenceStore::NameAction diff --git a/lib/persistence.h b/lib/persistence.h index 35d60ca..1cb3af0 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -311,8 +311,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr map.emplace(std::invoke(Key, s), std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -327,8 +328,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr container.emplace_back(std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -342,6 +344,7 @@ namespace Persistence { DEFAULT_MOVE_COPY(Persistable); virtual bool persist(PersistenceStore & store) = 0; + virtual void postLoad(); [[nodiscard]] virtual std::string getId() const; @@ -484,6 +487,9 @@ namespace Persistence { endObject(Stack & stk) override { make_default_as_needed(this->v); + if (this->v) { + this->v->postLoad(); + } stk.pop(); } diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp index a72c481..38bbf2f 100644 --- a/test/test-persistence.cpp +++ b/test/test-persistence.cpp @@ -34,6 +34,7 @@ struct JPP { BOOST_FIXTURE_TEST_CASE(load_object, JPP) { auto to = load_json>(FIXTURESDIR "json/load_object.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_CHECK_CLOSE(to->flt, 3.14, 0.01); BOOST_CHECK_EQUAL(to->str, "Lovely string"); BOOST_CHECK_EQUAL(to->bl, true); @@ -69,6 +70,7 @@ BOOST_FIXTURE_TEST_CASE(load_object, JPP) BOOST_FIXTURE_TEST_CASE(load_nested_object, JPP) { auto to = load_json>(FIXTURESDIR "json/nested.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_CHECK_EQUAL(to->flt, 1.F); BOOST_CHECK_EQUAL(to->str, "one"); BOOST_REQUIRE(to->ptr); @@ -86,6 +88,7 @@ BOOST_FIXTURE_TEST_CASE(load_nested_object, JPP) BOOST_FIXTURE_TEST_CASE(load_implicit_object, JPP) { auto to = load_json>(FIXTURESDIR "json/implicit.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_CHECK(to->ptr); BOOST_CHECK_EQUAL(to->flt, 1.F); BOOST_CHECK_EQUAL(to->ptr->str, "trigger"); @@ -95,6 +98,7 @@ BOOST_FIXTURE_TEST_CASE(load_implicit_object, JPP) BOOST_FIXTURE_TEST_CASE(load_empty_object, JPP) { auto to = load_json>(FIXTURESDIR "json/empty.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_CHECK_EQUAL(to->flt, 1.F); BOOST_CHECK(to->ptr); BOOST_CHECK_EQUAL(to->str, "after"); @@ -119,6 +123,7 @@ BOOST_FIXTURE_TEST_CASE(load_obj_no_such_type, JPP) BOOST_FIXTURE_TEST_CASE(load_abs_object, JPP) { auto to = load_json>(FIXTURESDIR "json/abs.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_REQUIRE(to->aptr); BOOST_CHECK_NO_THROW(to->aptr->dummy()); BOOST_CHECK_EQUAL(to->aptr->base, "set base"); @@ -130,6 +135,7 @@ BOOST_FIXTURE_TEST_CASE(load_abs_object, JPP) BOOST_FIXTURE_TEST_CASE(load_vector_ptr, JPP) { auto to = load_json>(FIXTURESDIR "json/vector_ptr.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_CHECK(to->str.empty()); BOOST_CHECK_EQUAL(to->vptr.size(), 4); BOOST_CHECK_EQUAL(to->vptr.at(0)->str, "type"); @@ -141,6 +147,7 @@ BOOST_FIXTURE_TEST_CASE(load_vector_ptr, JPP) BOOST_FIXTURE_TEST_CASE(test_conversion, JPP) { auto to = load_json>(FIXTURESDIR "json/conv.json"); + BOOST_CHECK_EQUAL(to->postLoadCalled, 1); BOOST_REQUIRE(to); BOOST_CHECK_EQUAL(to->bl, true); BOOST_CHECK_EQUAL(to->flt, 3.14F); diff --git a/test/testStructures.cpp b/test/testStructures.cpp index 8305078..469ec37 100644 --- a/test/testStructures.cpp +++ b/test/testStructures.cpp @@ -42,6 +42,12 @@ TestObject::persist(Persistence::PersistenceStore & store) && STORE_MEMBER(vptr); } +void +TestObject::postLoad() +{ + postLoadCalled++; +} + bool SharedTestObject::persist(Persistence::PersistenceStore & store) { diff --git a/test/testStructures.h b/test/testStructures.h index 666562e..6966052 100644 --- a/test/testStructures.h +++ b/test/testStructures.h @@ -39,7 +39,10 @@ struct TestObject : public Persistence::Persistable { std::unique_ptr aptr; std::vector> vptr; + unsigned int postLoadCalled {}; + bool persist(Persistence::PersistenceStore & store) override; + void postLoad() override; }; struct SharedTestObject : public Persistence::Persistable { -- cgit v1.2.3