From 96f16e151d6a889e0d825e9c1042958b5226db91 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 12 May 2018 23:27:16 +0100 Subject: string_view plugin/factory Uses string_view over strings in the plugin and factory interfaces. Soft breaking change in general, but changes the signature for plugin resolvers. --- libadhocutil/factory.h | 4 ++-- libadhocutil/factory.impl.h | 4 ++-- libadhocutil/plugins.cpp | 22 +++++++++------------- libadhocutil/plugins.h | 24 ++++++++++++------------ libadhocutil/plugins.impl.h | 18 +++++++++--------- libadhocutil/unittests/testPluginsRuntime.cpp | 12 ++++++------ 6 files changed, 40 insertions(+), 44 deletions(-) diff --git a/libadhocutil/factory.h b/libadhocutil/factory.h index 4aaf335..fe3f836 100644 --- a/libadhocutil/factory.h +++ b/libadhocutil/factory.h @@ -40,13 +40,13 @@ namespace AdHoc { * Helper to get the factory for a specific implementation. * @param name The name of the implementation. */ - static std::shared_ptr get(const std::string & name); + static std::shared_ptr get(const std::string_view & name); /** * Helper to create a new instance from a specific factory. * @param name The name of the implementation. * @param p The parameters to pass to the constructor. */ - static std::shared_ptr createNew(const std::string & name, const Params & ... p); + static std::shared_ptr createNew(const std::string_view & name, const Params & ... p); }; } diff --git a/libadhocutil/factory.impl.h b/libadhocutil/factory.impl.h index bd4ed38..aec3d31 100644 --- a/libadhocutil/factory.impl.h +++ b/libadhocutil/factory.impl.h @@ -10,14 +10,14 @@ namespace AdHoc { template std::shared_ptr> - Factory::get(const std::string & name) + Factory::get(const std::string_view & name) { return PluginManager::getDefault()->get(name)->implementation(); } template std::shared_ptr - Factory::createNew(const std::string & name, const Params & ... p) + Factory::createNew(const std::string_view & name, const Params & ... p) { return get(name)->create(p...); } diff --git a/libadhocutil/plugins.cpp b/libadhocutil/plugins.cpp index dc92ca3..4251ba1 100644 --- a/libadhocutil/plugins.cpp +++ b/libadhocutil/plugins.cpp @@ -32,7 +32,7 @@ namespace AdHoc { AbstractPluginImplementation::~AbstractPluginImplementation() = default; - Plugin::Plugin(const std::string & n, const std::string & f, int l) : + Plugin::Plugin(const std::string_view & n, const std::string_view & f, int l) : name(n), filename(f), lineno(l) @@ -40,7 +40,7 @@ namespace AdHoc { } AdHocFormatter(NoSuchPluginExceptionMsg, "No such plugin: %? of type %?"); - NoSuchPluginException::NoSuchPluginException(const std::string & n, const std::type_info & t) : + NoSuchPluginException::NoSuchPluginException(const std::string_view & n, const std::type_info & t) : std::runtime_error(NoSuchPluginExceptionMsg::get(n, t)) { } @@ -59,21 +59,21 @@ namespace AdHoc { } AdHocFormatter(LoadLibraryExceptionMsg, "Failed to load library [%?]; %?"); - LoadLibraryException::LoadLibraryException(const std::string & f, const char * msg) : + LoadLibraryException::LoadLibraryException(const std::string_view & f, const std::string_view & msg) : std::runtime_error(LoadLibraryExceptionMsg::get(f, msg)) { } class PluginManager::PluginStore : public boost::multi_index_container>, + boost::multi_index::ordered_non_unique, std::less<>>, boost::multi_index::ordered_non_unique>, boost::multi_index::ordered_unique< boost::multi_index::composite_key< Plugin, boost::multi_index::member, boost::multi_index::const_mem_fun - >> + >, std::less<>> >> { }; @@ -102,14 +102,14 @@ namespace AdHoc { } void - PluginManager::remove(const std::string & n, const std::type_info & t) + PluginManager::remove(const std::string_view & n, const std::type_info & t) { auto r = plugins->get<2>().equal_range(std::make_tuple(n, std::cref(t))); plugins->get<2>().erase(r.first, r.second); } PluginPtr - PluginManager::get(const std::string & n, const std::type_info & t) const + PluginManager::get(const std::string_view & n, const std::type_info & t) const { auto r = plugins->get<2>().equal_range(std::make_tuple(n, std::cref(t))); if (r.first == r.second) { @@ -139,18 +139,14 @@ namespace AdHoc { std::set PluginManager::getAll() const { - std::set all; - for(const auto & p : *plugins) { - all.insert(p); - } - return all; + return { plugins->begin(), plugins->end() }; } std::set PluginManager::getAll(const std::type_info & t) const { auto r = plugins->get<1>().equal_range(t); - return std::set(r.first, r.second); + return { r.first, r.second }; } size_t diff --git a/libadhocutil/plugins.h b/libadhocutil/plugins.h index d51ad2b..d9ed4a3 100644 --- a/libadhocutil/plugins.h +++ b/libadhocutil/plugins.h @@ -22,7 +22,7 @@ namespace AdHoc { class NoSuchPluginException : public std::runtime_error { public: /// Constructor taking name and type of plugin requested. - NoSuchPluginException(const std::string &, const std::type_info &); + NoSuchPluginException(const std::string_view &, const std::type_info &); }; /// Base class for all plugin implementations. @@ -35,7 +35,7 @@ namespace AdHoc { class DLL_PUBLIC Plugin { public: /// Constructor taking name, filename and line of install. - Plugin(const std::string &, const std::string &, int); + Plugin(const std::string_view &, const std::string_view &, int); virtual ~Plugin() = default; /// Get the plugin type from the subclass. @@ -71,7 +71,7 @@ namespace AdHoc { class LoadLibraryException : public std::runtime_error { public: /// Constuctor taking syscall error details. - LoadLibraryException(const std::string & f, const char * msg); + LoadLibraryException(const std::string_view & f, const std::string_view & msg); }; template @@ -79,7 +79,7 @@ namespace AdHoc { class DLL_PUBLIC PluginOf : public Plugin { public: /// Constructor taking an instance and name, filename and line of install for Plugin. - PluginOf(const std::shared_ptr & t, const std::string & n, const std::string & f, int l); + PluginOf(const std::shared_ptr & t, const std::string_view & n, const std::string_view & f, int l); ~PluginOf() = default; /// Get the type of this plugin. @@ -98,7 +98,7 @@ namespace AdHoc { public: /// Callback definition to resolve a plugin type and name to a potential library /// containing an implementation. - typedef std::function (const std::type_info &, const std::string &)> PluginResolver; + typedef std::function (const std::type_info &, const std::string_view &)> PluginResolver; PluginManager(); virtual ~PluginManager() = default; @@ -106,9 +106,9 @@ namespace AdHoc { /// Install a plugin. void add(const PluginPtr &); /// Uninstall a plugin. - void remove(const std::string &, const std::type_info &); + void remove(const std::string_view &, const std::type_info &); /// Get a specific plugin. - PluginPtr get(const std::string &, const std::type_info &) const; + PluginPtr get(const std::string_view &, const std::type_info &) const; /// Get all plugins. std::set getAll() const; /// Get all plugins of a specific type. @@ -121,7 +121,7 @@ namespace AdHoc { * @param f Filename of plugin. * @param l Line number. */ - template void add(const std::shared_ptr & i, const std::string & n, const std::string & f, int l); + template void add(const std::shared_ptr & i, const std::string_view & n, const std::string_view & f, int l); /** * Create and install a plugin @@ -133,7 +133,7 @@ namespace AdHoc { * @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) + template void create(const std::string_view & n, const std::string_view & f, int l, const Args & ... args) { add(std::make_shared(args...), n, f, l); } @@ -142,19 +142,19 @@ namespace AdHoc { * Uninstall a plugin. * @param n Name of plugin. */ - template void remove(const std::string & n); + template void remove(const std::string_view & n); /** * Get a specific plugin. * @param n Name of plugin. */ - template std::shared_ptr> get(const std::string & n) const; + template std::shared_ptr> get(const std::string_view & n) const; /** * Get the implementation from specific plugin. * @param n Name of plugin. */ - template std::shared_ptr getImplementation(const std::string & n) const; + template std::shared_ptr getImplementation(const std::string_view & n) const; /** * Get all plugins of a given time. diff --git a/libadhocutil/plugins.impl.h b/libadhocutil/plugins.impl.h index 6f8da05..8a2ca8a 100644 --- a/libadhocutil/plugins.impl.h +++ b/libadhocutil/plugins.impl.h @@ -5,7 +5,7 @@ namespace AdHoc { template - PluginOf::PluginOf(const std::shared_ptr & t, const std::string & n, const std::string & f, int l) : + PluginOf::PluginOf(const std::shared_ptr & t, const std::string_view & n, const std::string_view & f, int l) : Plugin(n, f, l), impl(t) { @@ -36,28 +36,28 @@ namespace AdHoc { template void - PluginManager::add(const std::shared_ptr & i, const std::string & n, const std::string & f, int l) + PluginManager::add(const std::shared_ptr & i, const std::string_view & n, const std::string_view & f, int l) { add(std::make_shared>(i, n, f, l)); } template void - PluginManager::remove(const std::string & n) + PluginManager::remove(const std::string_view & n) { remove(n, typeid(T)); } template std::shared_ptr> - PluginManager::get(const std::string & n) const + PluginManager::get(const std::string_view & n) const { return std::dynamic_pointer_cast>(get(n, typeid(T))); } template std::shared_ptr - PluginManager::getImplementation(const std::string & n) const + PluginManager::getImplementation(const std::string_view & n) const { return std::static_pointer_cast(get(n)->implementation()); } @@ -92,10 +92,10 @@ namespace AdHoc { #define INSTANTIATEPLUGINOF(...) \ template class AdHoc::PluginOf<__VA_ARGS__>; \ - template void AdHoc::PluginManager::add<__VA_ARGS__>(const std::shared_ptr<__VA_ARGS__> &, const std::string &, const std::string &, int); \ - template void AdHoc::PluginManager::remove<__VA_ARGS__>(const std::string &); \ - template std::shared_ptr> AdHoc::PluginManager::get<__VA_ARGS__>(const std::string &) const; \ - template std::shared_ptr<__VA_ARGS__> AdHoc::PluginManager::getImplementation<__VA_ARGS__>(const std::string &) const; \ + template void AdHoc::PluginManager::add<__VA_ARGS__>(const std::shared_ptr<__VA_ARGS__> &, const std::string_view &, const std::string_view &, int); \ + template void AdHoc::PluginManager::remove<__VA_ARGS__>(const std::string_view &); \ + template std::shared_ptr> AdHoc::PluginManager::get<__VA_ARGS__>(const std::string_view &) const; \ + template std::shared_ptr<__VA_ARGS__> AdHoc::PluginManager::getImplementation<__VA_ARGS__>(const std::string_view &) const; \ template std::set>> AdHoc::PluginManager::getAll<__VA_ARGS__>() const; \ template void AdHoc::PluginManager::addResolver<__VA_ARGS__>(const AdHoc::PluginManager::PluginResolver & f); \ template void AdHoc::PluginManager::removeResolver<__VA_ARGS__>(); \ diff --git a/libadhocutil/unittests/testPluginsRuntime.cpp b/libadhocutil/unittests/testPluginsRuntime.cpp index b333104..cfcc977 100644 --- a/libadhocutil/unittests/testPluginsRuntime.cpp +++ b/libadhocutil/unittests/testPluginsRuntime.cpp @@ -19,9 +19,9 @@ struct GetLibPath { }; BOOST_TEST_GLOBAL_FIXTURE(GetLibPath); -static std::optional nullResolver(const std::type_info &, const std::string &); -static std::optional badResolver(const std::type_info &, const std::string &); -static std::optional goodResolver(const std::type_info &, const std::string &); +static std::optional nullResolver(const std::type_info &, const std::string_view &); +static std::optional badResolver(const std::type_info &, const std::string_view &); +static std::optional goodResolver(const std::type_info &, const std::string_view &); BOOST_AUTO_TEST_CASE( ready ) { @@ -44,19 +44,19 @@ BOOST_AUTO_TEST_CASE( loadAndUnloadlib ) } std::optional -nullResolver(const std::type_info &, const std::string &) +nullResolver(const std::type_info &, const std::string_view &) { return {}; } std::optional -badResolver(const std::type_info &, const std::string &) +badResolver(const std::type_info &, const std::string_view &) { return std::string("dontexist"); } std::optional -goodResolver(const std::type_info & t, const std::string & n) +goodResolver(const std::type_info & t, const std::string_view & n) { BOOST_REQUIRE_EQUAL(typeid(BaseThing), t); BOOST_REQUIRE_EQUAL("ImplOfThing", n); -- cgit v1.2.3