From 8cc7ef8b72e408cd7d0b7f347cd508137d471208 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 29 Apr 2021 17:42:05 +0100 Subject: Automatic type registration --- lib/persistance.h | 32 ++++++++++++++++++++++++++++++-- test/test-persistance.cpp | 16 +++++----------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/persistance.h b/lib/persistance.h index 04da744..f797157 100644 --- a/lib/persistance.h +++ b/lib/persistance.h @@ -75,7 +75,8 @@ namespace Persistanace { }; struct PersistanceStore { - // virtual bool persistType(const std::type_info &) = 0; + template inline bool persistType() const; + template inline bool persistValue(const std::string_view key, T & value) @@ -144,10 +145,37 @@ namespace Persistanace { virtual bool persist(PersistanceStore & store) = 0; + template + constexpr static auto + typeName() + { + constexpr std::string_view name {__PRETTY_FUNCTION__}; + constexpr auto s {name.find("T = ") + 4}, e {name.rfind(']')}; + return name.substr(s, e - s); + } + + template static void addFactory() __attribute__((constructor)); static void addFactory(const std::string_view, std::function()>); static std::unique_ptr callFactory(const std::string_view); }; + template + void + Persistable::addFactory() + { + addFactory(typeName(), std::make_unique); + } + + template + inline bool + PersistanceStore::persistType() const + { + if constexpr (!std::is_abstract_v) { + [[maybe_unused]] constexpr auto f = &Persistable::addFactory; + } + return true; + } + template struct SelectionT> : public SelectionV> { using Ptr = std::unique_ptr; struct SelectionObj : public SelectionV { @@ -230,7 +258,7 @@ namespace Persistanace { }; } -#define STORE_TYPE store.persistType(typeid(*this)) +#define STORE_TYPE store.persistType>() #define STORE_MEMBER(mbr) store.persistValue(#mbr, mbr) #endif diff --git a/test/test-persistance.cpp b/test/test-persistance.cpp index ffc8a3c..a4fda31 100644 --- a/test/test-persistance.cpp +++ b/test/test-persistance.cpp @@ -16,7 +16,7 @@ struct AbsObject : public Persistanace::Persistable { bool persist(Persistanace::PersistanceStore & store) override { - return STORE_MEMBER(base); + return STORE_TYPE && STORE_MEMBER(base); } virtual void dummy() const = 0; @@ -28,7 +28,7 @@ struct SubObject : public AbsObject { bool persist(Persistanace::PersistanceStore & store) override { - return AbsObject::persist(store) && STORE_MEMBER(sub); + return AbsObject::persist(store) && STORE_TYPE && STORE_MEMBER(sub); } void @@ -54,18 +54,12 @@ struct TestObject : public Persistanace::Persistable { bool persist(Persistanace::PersistanceStore & store) override { - 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); + return STORE_TYPE && 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); } }; -BOOST_AUTO_TEST_CASE(setup) -{ - Persistanace::Persistable::addFactory("TestObject", std::make_unique); - Persistanace::Persistable::addFactory("SubObject", std::make_unique); -} - struct JPP : public Persistanace::JsonParsePersistance { template T -- cgit v1.2.3