diff options
-rw-r--r-- | lib/persistance.h | 32 | ||||
-rw-r--r-- | 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<typename T> inline bool persistType() const; + template<typename T> inline bool persistValue(const std::string_view key, T & value) @@ -144,10 +145,37 @@ namespace Persistanace { virtual bool persist(PersistanceStore & store) = 0; + template<typename T> + 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<typename T> static void addFactory() __attribute__((constructor)); static void addFactory(const std::string_view, std::function<std::unique_ptr<Persistable>()>); static std::unique_ptr<Persistable> callFactory(const std::string_view); }; + template<typename T> + void + Persistable::addFactory() + { + addFactory(typeName<T>(), std::make_unique<T>); + } + + template<typename T> + inline bool + PersistanceStore::persistType() const + { + if constexpr (!std::is_abstract_v<T>) { + [[maybe_unused]] constexpr auto f = &Persistable::addFactory<T>; + } + return true; + } + template<typename T> struct SelectionT<std::unique_ptr<T>> : public SelectionV<std::unique_ptr<T>> { using Ptr = std::unique_ptr<T>; struct SelectionObj : public SelectionV<Ptr> { @@ -230,7 +258,7 @@ namespace Persistanace { }; } -#define STORE_TYPE store.persistType(typeid(*this)) +#define STORE_TYPE store.persistType<std::decay_t<decltype(*this)>>() #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<TestObject>); - Persistanace::Persistable::addFactory("SubObject", std::make_unique<SubObject>); -} - struct JPP : public Persistanace::JsonParsePersistance { template<typename T> T |