From 4053cddc33e29b300f8aa457fade194a7331a711 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 27 Apr 2021 00:53:10 +0100 Subject: Simplify object creation code --- lib/persistance.h | 31 ++++++++++++++++++++++++------- 1 file 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 static SelectionPtr make(T & value); + template static SelectionPtr make_s(T & value); }; template struct SelectionT : public Selection { @@ -55,6 +58,20 @@ namespace Persistanace { T & v; }; + template + SelectionPtr + Selection::make(T & value) + { + return make_s>(value); + } + + template + SelectionPtr + Selection::make_s(T & value) + { + return std::make_unique(value); + } + struct PersistanceStore { // virtual bool persistType(const std::type_info &) = 0; template @@ -62,7 +79,7 @@ namespace Persistanace { persistValue(const std::string_view key, T & value) { if (key == name) { - sel = std::make_unique>(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>(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(std::ref(v))); + stk.push(make_s(v)); } void @@ -112,7 +129,7 @@ namespace Persistanace { void beforeValue(Stack & stk) override { - stk.push(std::make_unique>(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(std::ref(v))); + stk.push(make_s(v)); } void @@ -178,7 +195,7 @@ namespace Persistanace { if (v) { throw std::runtime_error("cannot set object type after creation"); } - return std::make_unique(std::ref(v)); + return make_s(v); } else { if (!v) { @@ -230,7 +247,7 @@ namespace Persistanace { void BeginObject(Stack & stk) override { - stk.push(std::make_unique(v)); + stk.push(make_s(v)); } void -- cgit v1.2.3