6 #ifndef PLUGINFACTORY_HPP_INCLUDE
7 #define PLUGINFACTORY_HPP_INCLUDE
40 const std::map<std::string, std::string> &
dictionary = m_empty_dictionary)
42 auto result = m_name_func_map.emplace(plugin_name,
make_plugin);
44 throw Exception(
"PluginFactory::register_plugin(): name: \"" +
45 plugin_name +
"\" has been previously registered",
49 m_plugin_names.push_back(plugin_name);
59 std::unique_ptr<T>
make_plugin(
const std::string &plugin_name)
const
61 auto it = m_name_func_map.find(plugin_name);
62 if (it == m_name_func_map.end()) {
63 throw Exception(
"PluginFactory::make_plugin(): name: \"" +
64 plugin_name +
"\" has not been previously registered",
76 return m_plugin_names;
85 const std::map<std::string, std::string> &
dictionary(
const std::string &plugin_name)
const
87 auto it = m_dictionary.find(plugin_name);
88 if (it == m_dictionary.end()) {
89 throw Exception(
"PluginFactory::dictionary(): Plugin named \"" + plugin_name +
90 "\" has not been registered with the factory.",
96 std::map<std::string, std::function<std::unique_ptr<T>()> > m_name_func_map;
97 std::vector<std::string> m_plugin_names;
98 std::map<std::string, const std::map<std::string, std::string> > m_dictionary;
99 static const std::map<std::string, std::string> m_empty_dictionary;
102 template <
class Type>
103 const std::map<std::string, std::string> PluginFactory<Type>::m_empty_dictionary = {};
Class for all GEOPM-specific exceptions.
Definition: Exception.hpp:48
Definition: PluginFactory.hpp:22
virtual ~PluginFactory()=default
PluginFactory(const PluginFactory &other)=delete
std::vector< std::string > plugin_names(void) const
Returns a list of all valid plugin names registered with the factory in the order they were registere...
Definition: PluginFactory.hpp:74
const std::map< std::string, std::string > & dictionary(const std::string &plugin_name) const
Returns the dictionary of static metadata about a registered type. If the type was not registered,...
Definition: PluginFactory.hpp:85
PluginFactory & operator=(const PluginFactory &other)=delete
std::unique_ptr< T > make_plugin(const std::string &plugin_name) const
Create an object of the requested type. If the type was not registered, throws an exception.
Definition: PluginFactory.hpp:59
void register_plugin(const std::string &plugin_name, std::function< std::unique_ptr< T >()> make_plugin, const std::map< std::string, std::string > &dictionary=m_empty_dictionary)
Add a plugin to the factory.
Definition: PluginFactory.hpp:38
@ GEOPM_ERROR_INVALID
Definition: geopm_error.h:20