From a3ce88d45e535d1275d29d5033b0a423ab4fe326 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 11 Dec 2023 10:28:45 +0200 Subject: [PATCH] Simplify DynamicObjectLoader with an if constexpr --- Build | 2 +- source/dynamicobjectloader.h | 33 ++++++++------------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/Build b/Build index 5e962e6..0339dd2 100644 --- a/Build +++ b/Build @@ -8,7 +8,7 @@ package "mspdatafile" build_info { - standard CXX "c++14"; + standard CXX "c++17"; }; library "mspdatafile" diff --git a/source/dynamicobjectloader.h b/source/dynamicobjectloader.h index 43c4b1f..2cf1b36 100644 --- a/source/dynamicobjectloader.h +++ b/source/dynamicobjectloader.h @@ -52,14 +52,6 @@ public: protected: virtual void type(const Symbol &); -private: - template - typename std::enable_if::value, std::unique_ptr>::type create_object_loader(U &obj) const; - - template - typename std::enable_if::value, std::unique_ptr>::type create_object_loader(U &obj) const; - -protected: virtual const TypeRegistry &get_type_registry() const = 0; }; @@ -100,29 +92,20 @@ void DynamicObjectLoader::type(const Symbol &t) get_type_registry().invoke(t.name, *this); } -template -template -typename std::enable_if::value, std::unique_ptr>::type DynamicObjectLoader::create_object_loader(U &obj) const -{ - if(!coll) - throw no_collection(typeid(U)); - return std::make_unique(obj, *coll); -} - -template -template -typename std::enable_if::value, std::unique_ptr>::type DynamicObjectLoader::create_object_loader(U &obj) const -{ - return std::make_unique(obj); -} - template template void DynamicObjectLoader::CreateObject::operator()(const std::string &, DynamicObjectLoader &ldr) const { ldr.object = std::make_unique(); - ldr.obj_loader = ldr.create_object_loader(static_cast(*ldr.object)); + if constexpr(NeedsCollection::value) + { + if(!ldr.coll) + throw no_collection(typeid(U)); + ldr.obj_loader = std::make_unique(static_cast(*ldr.object), *ldr.coll); + } + else + ldr.obj_loader = std::make_unique(static_cast(*ldr.object)); ldr.add_auxiliary_loader(*ldr.obj_loader); ldr.store_func = [](Collection &c, const std::string &n, std::unique_ptr o){ c.add(n, std::unique_ptr(static_cast(o.release()))); }; } -- 2.45.2