From 66107ea15378cc30260224b668a0152acbcc353d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Apr 2018 11:30:10 +0100 Subject: C++17 Adds a friendly wrapper function to construct and install a plugin (similar interface std::make_...) --- libadhocutil/plugins.h | 15 +++++++++++++++ libadhocutil/unittests/testPlugins.cpp | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/libadhocutil/plugins.h b/libadhocutil/plugins.h index 0fab76e..fe86834 100644 --- a/libadhocutil/plugins.h +++ b/libadhocutil/plugins.h @@ -128,6 +128,21 @@ namespace AdHoc { */ template void add(const std::shared_ptr & i, const std::string & n, const std::string & f, int l); + /** + * Create and install a plugin + * @tparam T Base type of plugin + * @tparam I Implementation type of plugin + * @tparam Args Constructor arguments types + * @param n Name of plugin. + * @param f Filename of plugin. + * @param l Line number. + * @param args Arguments to construct an instance of I with. + */ + template void create(const std::string & n, const std::string & f, int l, const Args & ... args) + { + add(std::make_shared(args...), n, f, l); + } + /** * Uninstall a plugin. * @param n Name of plugin. diff --git a/libadhocutil/unittests/testPlugins.cpp b/libadhocutil/unittests/testPlugins.cpp index 84d00c7..a8380fb 100644 --- a/libadhocutil/unittests/testPlugins.cpp +++ b/libadhocutil/unittests/testPlugins.cpp @@ -72,6 +72,16 @@ BOOST_AUTO_TEST_CASE( removeManual ) BOOST_REQUIRE(PluginManager::getDefault()->get("ImplOfThing")); } +BOOST_AUTO_TEST_CASE( createAndRemove ) +{ + PluginManager::getDefault()->create("custom1", __FILE__, __LINE__); + BOOST_REQUIRE_EQUAL(2, PluginManager::getDefault()->count()); + auto c1 = PluginManager::getDefault()->get("custom1"); + BOOST_REQUIRE(c1); + PluginManager::getDefault()->remove("custom1"); + +} + BOOST_AUTO_TEST_CASE( nameAndTypeClash ) { // Same name, different type -- cgit v1.2.3