diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-04-29 19:38:05 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-11-07 16:41:37 +0000 |
commit | a6f3f335e931acd12b908ca0b4028a08c102627c (patch) | |
tree | 4f44f9532f583f6c40211a56bebd5e51e416fd60 /lib | |
parent | Automatic type registration (diff) | |
download | ilt-a6f3f335e931acd12b908ca0b4028a08c102627c.tar.bz2 ilt-a6f3f335e931acd12b908ca0b4028a08c102627c.tar.xz ilt-a6f3f335e931acd12b908ca0b4028a08c102627c.zip |
Extend factory map to include make_shared
Diffstat (limited to 'lib')
-rw-r--r-- | lib/persistance.cpp | 17 | ||||
-rw-r--r-- | 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 <map> namespace Persistanace { - using NamedTypeFactories = std::map<std::string_view, std::function<std::unique_ptr<Persistable>()>>; + using Factories + = std::pair<std::function<std::unique_ptr<Persistable>()>, std::function<std::shared_ptr<Persistable>()>>; + using NamedTypeFactories = std::map<std::string_view, Factories>; static NamedTypeFactories namedTypeFactories; void - Persistable::addFactory(const std::string_view t, std::function<std::unique_ptr<Persistable>()> f) + Persistable::addFactory(const std::string_view t, std::function<std::unique_ptr<Persistable>()> fu, + std::function<std::shared_ptr<Persistable>()> fs) { - namedTypeFactories.emplace(t, std::move(f)); + namedTypeFactories.emplace(t, std::make_pair(std::move(fu), std::move(fs))); } std::unique_ptr<Persistable> Persistable::callFactory(const std::string_view t) { - return namedTypeFactories.at(t)(); + return namedTypeFactories.at(t).first(); + } + + std::shared_ptr<Persistable> + 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 <stdexcept> #include <string> #include <string_view> +#include <type_traits> #include <utility> #include <vector> @@ -155,15 +156,17 @@ namespace Persistanace { } template<typename T> static void addFactory() __attribute__((constructor)); - static void addFactory(const std::string_view, std::function<std::unique_ptr<Persistable>()>); + static void addFactory(const std::string_view, std::function<std::unique_ptr<Persistable>()>, + std::function<std::shared_ptr<Persistable>()>); static std::unique_ptr<Persistable> callFactory(const std::string_view); + static std::shared_ptr<Persistable> callSharedFactory(const std::string_view); }; template<typename T> void Persistable::addFactory() { - addFactory(typeName<T>(), std::make_unique<T>); + addFactory(typeName<T>(), std::make_unique<T>, std::make_shared<T>); } template<typename T> |