From a6f3f335e931acd12b908ca0b4028a08c102627c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 29 Apr 2021 19:38:05 +0100 Subject: Extend factory map to include make_shared --- lib/persistance.cpp | 17 +++++++++++++---- lib/persistance.h | 7 +++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/persistance.cpp b/lib/persistance.cpp index 0dd7cba..204e8f0 100644 --- a/lib/persistance.cpp +++ b/lib/persistance.cpp @@ -2,19 +2,28 @@ #include namespace Persistanace { - using NamedTypeFactories = std::map()>>; + using Factories + = std::pair()>, std::function()>>; + using NamedTypeFactories = std::map; static NamedTypeFactories namedTypeFactories; void - Persistable::addFactory(const std::string_view t, std::function()> f) + Persistable::addFactory(const std::string_view t, std::function()> fu, + std::function()> fs) { - namedTypeFactories.emplace(t, std::move(f)); + namedTypeFactories.emplace(t, std::make_pair(std::move(fu), std::move(fs))); } std::unique_ptr Persistable::callFactory(const std::string_view t) { - return namedTypeFactories.at(t)(); + return namedTypeFactories.at(t).first(); + } + + std::shared_ptr + Persistable::callSharedFactory(const std::string_view t) + { + return namedTypeFactories.at(t).second(); } void diff --git a/lib/persistance.h b/lib/persistance.h index f797157..6e23272 100644 --- a/lib/persistance.h +++ b/lib/persistance.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -155,15 +156,17 @@ namespace Persistanace { } template static void addFactory() __attribute__((constructor)); - static void addFactory(const std::string_view, std::function()>); + static void addFactory(const std::string_view, std::function()>, + std::function()>); static std::unique_ptr callFactory(const std::string_view); + static std::shared_ptr callSharedFactory(const std::string_view); }; template void Persistable::addFactory() { - addFactory(typeName(), std::make_unique); + addFactory(typeName(), std::make_unique, std::make_shared); } template -- cgit v1.2.3