diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-04-27 00:53:10 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-11-07 16:41:37 +0000 |
commit | 4053cddc33e29b300f8aa457fade194a7331a711 (patch) | |
tree | 745f6c8004cb722e6b79ed49499bc78d2d2d2fcb /lib | |
parent | Fix the odd difference with arrays and vectors and objects (diff) | |
download | ilt-4053cddc33e29b300f8aa457fade194a7331a711.tar.bz2 ilt-4053cddc33e29b300f8aa457fade194a7331a711.tar.xz ilt-4053cddc33e29b300f8aa457fade194a7331a711.zip |
Simplify object creation code
Diffstat (limited to 'lib')
-rw-r--r-- | lib/persistance.h | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/lib/persistance.h b/lib/persistance.h index 2568e8e..866be51 100644 --- a/lib/persistance.h +++ b/lib/persistance.h @@ -36,6 +36,9 @@ namespace Persistanace { virtual void EndObject(Stack &); virtual void beforeValue(Stack &); virtual SelectionPtr select(const std::string &); + + template<typename T> static SelectionPtr make(T & value); + template<typename S, typename T> static SelectionPtr make_s(T & value); }; template<typename T> struct SelectionT : public Selection { @@ -55,6 +58,20 @@ namespace Persistanace { T & v; }; + template<typename T> + SelectionPtr + Selection::make(T & value) + { + return make_s<SelectionT<T>>(value); + } + + template<typename S, typename T> + SelectionPtr + Selection::make_s(T & value) + { + return std::make_unique<S>(value); + } + struct PersistanceStore { // virtual bool persistType(const std::type_info &) = 0; template<typename T> @@ -62,7 +79,7 @@ namespace Persistanace { persistValue(const std::string_view key, T & value) { if (key == name) { - sel = std::make_unique<SelectionT<T>>(std::ref(value)); + sel = Selection::make(value); return false; } return true; @@ -80,7 +97,7 @@ namespace Persistanace { void beforeValue(Stack & stk) override { - stk.push(std::make_unique<SelectionT<T>>(std::ref(v[idx++]))); + stk.push(make(v[idx++])); } V & v; @@ -92,7 +109,7 @@ namespace Persistanace { void BeginArray(Stack & stk) override { - stk.push(std::make_unique<Members>(std::ref(v))); + stk.push(make_s<Members>(v)); } void @@ -112,7 +129,7 @@ namespace Persistanace { void beforeValue(Stack & stk) override { - stk.push(std::make_unique<SelectionT<T>>(std::ref(v.emplace_back()))); + stk.push(make(v.emplace_back())); } V & v; @@ -123,7 +140,7 @@ namespace Persistanace { void BeginArray(Stack & stk) override { - stk.push(std::make_unique<Members>(std::ref(v))); + stk.push(make_s<Members>(v)); } void @@ -178,7 +195,7 @@ namespace Persistanace { if (v) { throw std::runtime_error("cannot set object type after creation"); } - return std::make_unique<MakeObjectByTypeName>(std::ref(v)); + return make_s<MakeObjectByTypeName>(v); } else { if (!v) { @@ -230,7 +247,7 @@ namespace Persistanace { void BeginObject(Stack & stk) override { - stk.push(std::make_unique<SelectionObj>(v)); + stk.push(make_s<SelectionObj>(v)); } void |