summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2018-05-12 23:27:16 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2019-01-14 20:03:48 +0000
commit96f16e151d6a889e0d825e9c1042958b5226db91 (patch)
treef50f7e1ec4f876bc40a23bd1c7875ec28073ff97
parentSupport scprintf macro for format string with no args (diff)
downloadlibadhocutil-96f16e151d6a889e0d825e9c1042958b5226db91.tar.bz2
libadhocutil-96f16e151d6a889e0d825e9c1042958b5226db91.tar.xz
libadhocutil-96f16e151d6a889e0d825e9c1042958b5226db91.zip
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.
-rw-r--r--libadhocutil/factory.h4
-rw-r--r--libadhocutil/factory.impl.h4
-rw-r--r--libadhocutil/plugins.cpp22
-rw-r--r--libadhocutil/plugins.h24
-rw-r--r--libadhocutil/plugins.impl.h18
-rw-r--r--libadhocutil/unittests/testPluginsRuntime.cpp12
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<const Factory> get(const std::string & name);
+ static std::shared_ptr<const Factory> 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<Base> createNew(const std::string & name, const Params & ... p);
+ static std::shared_ptr<Base> 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 <typename Base, typename ... Params>
std::shared_ptr<const Factory<Base, Params...>>
- Factory<Base, Params...>::get(const std::string & name)
+ Factory<Base, Params...>::get(const std::string_view & name)
{
return PluginManager::getDefault()->get<Factory>(name)->implementation();
}
template <typename Base, typename ... Params>
std::shared_ptr<Base>
- Factory<Base, Params...>::createNew(const std::string & name, const Params & ... p)
+ Factory<Base, Params...>::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<PluginPtr,
boost::multi_index::indexed_by<
- boost::multi_index::ordered_non_unique<boost::multi_index::member<Plugin, const std::string, &Plugin::name>>,
+ boost::multi_index::ordered_non_unique<boost::multi_index::member<Plugin, const std::string, &Plugin::name>, std::less<>>,
boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<Plugin, const std::type_info &, &Plugin::type>>,
boost::multi_index::ordered_unique<
boost::multi_index::composite_key<
Plugin,
boost::multi_index::member<Plugin, const std::string, &Plugin::name>,
boost::multi_index::const_mem_fun<Plugin, const std::type_info &, &Plugin::type>
- >>
+ >, 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<PluginPtr>
PluginManager::getAll() const
{
- std::set<PluginPtr> all;
- for(const auto & p : *plugins) {
- all.insert(p);
- }
- return all;
+ return { plugins->begin(), plugins->end() };
}
std::set<PluginPtr>
PluginManager::getAll(const std::type_info & t) const
{
auto r = plugins->get<1>().equal_range(t);
- return std::set<PluginPtr>(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 <typename T>
@@ -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> & t, const std::string & n, const std::string & f, int l);
+ PluginOf(const std::shared_ptr<T> & 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<std::optional<std::string> (const std::type_info &, const std::string &)> PluginResolver;
+ typedef std::function<std::optional<std::string> (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<PluginPtr> 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<typename T> void add(const std::shared_ptr<T> & i, const std::string & n, const std::string & f, int l);
+ template<typename T> void add(const std::shared_ptr<T> & 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<typename T, typename I, typename ... Args> void create(const std::string & n, const std::string & f, int l, const Args & ... args)
+ template<typename T, typename I, typename ... Args> void create(const std::string_view & n, const std::string_view & f, int l, const Args & ... args)
{
add<T>(std::make_shared<I>(args...), n, f, l);
}
@@ -142,19 +142,19 @@ namespace AdHoc {
* Uninstall a plugin.
* @param n Name of plugin.
*/
- template<typename T> void remove(const std::string & n);
+ template<typename T> void remove(const std::string_view & n);
/**
* Get a specific plugin.
* @param n Name of plugin.
*/
- template<typename T> std::shared_ptr<const PluginOf<T>> get(const std::string & n) const;
+ template<typename T> std::shared_ptr<const PluginOf<T>> get(const std::string_view & n) const;
/**
* Get the implementation from specific plugin.
* @param n Name of plugin.
*/
- template<typename T> std::shared_ptr<T> getImplementation(const std::string & n) const;
+ template<typename T> std::shared_ptr<T> 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 <typename T>
- PluginOf<T>::PluginOf(const std::shared_ptr<T> & t, const std::string & n, const std::string & f, int l) :
+ PluginOf<T>::PluginOf(const std::shared_ptr<T> & 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 <typename T>
void
- PluginManager::add(const std::shared_ptr<T> & i, const std::string & n, const std::string & f, int l)
+ PluginManager::add(const std::shared_ptr<T> & i, const std::string_view & n, const std::string_view & f, int l)
{
add(std::make_shared<PluginOf<T>>(i, n, f, l));
}
template <typename T>
void
- PluginManager::remove(const std::string & n)
+ PluginManager::remove(const std::string_view & n)
{
remove(n, typeid(T));
}
template <typename T>
std::shared_ptr<const PluginOf<T>>
- PluginManager::get(const std::string & n) const
+ PluginManager::get(const std::string_view & n) const
{
return std::dynamic_pointer_cast<const PluginOf<T>>(get(n, typeid(T)));
}
template <typename T>
std::shared_ptr<T>
- PluginManager::getImplementation(const std::string & n) const
+ PluginManager::getImplementation(const std::string_view & n) const
{
return std::static_pointer_cast<T>(get<T>(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<const AdHoc::PluginOf<__VA_ARGS__>> 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<const AdHoc::PluginOf<__VA_ARGS__>> 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<std::shared_ptr<const AdHoc::PluginOf<__VA_ARGS__>>> 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<std::string> nullResolver(const std::type_info &, const std::string &);
-static std::optional<std::string> badResolver(const std::type_info &, const std::string &);
-static std::optional<std::string> goodResolver(const std::type_info &, const std::string &);
+static std::optional<std::string> nullResolver(const std::type_info &, const std::string_view &);
+static std::optional<std::string> badResolver(const std::type_info &, const std::string_view &);
+static std::optional<std::string> 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<std::string>
-nullResolver(const std::type_info &, const std::string &)
+nullResolver(const std::type_info &, const std::string_view &)
{
return {};
}
std::optional<std::string>
-badResolver(const std::type_info &, const std::string &)
+badResolver(const std::type_info &, const std::string_view &)
{
return std::string("dontexist");
}
std::optional<std::string>
-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);