Class PluginLoader

Class Documentation

class PluginLoader

This is a utility class for loading plugins.

The library_name should not include the prefix ‘lib’ or suffix ‘.so’. It will add the correct prefix and suffix based on the OS.

It supports providing additional search paths and set environment variable which should be used when searching for plugins.

The plugin must be exported using the macro EXPORT_CLASS_SECTIONED. In the example below, the first parameter is the derived object and the second is the assigned symbol name which is used for loading Example: EXPORT_CLASS_SECTIONED(my_namespace::MyPlugin, plugin, section)

PluginLoader loader; loader.search_libraries.insert(“my_plugin”); // libmy_plugin.so std::shared_ptr<PluginBase> p = loader.instantiate<PluginBase>(“plugin”);

Public Functions

template<class PluginBase>
std::shared_ptr<PluginBase> createInstance(const std::string &plugin_name) const

Loads a shared instance of a plugin of a specified type.

Throws:

If – the plugin is not found

Parameters:

plugin_name – The plugin name to find

Returns:

A shared instance

template<class PluginBase>
std::enable_if_t<has_getSection<PluginBase>::value, std::vector<std::string>> getAvailablePlugins() const

Lists all available plugins of a specified base type.

This method requires that each plugin interface definition define a static string member called section. This string is used to denote symbols (i.e. plugin classes) in a library, such that all symbols a given section name can be found by the plugin loader. It is useful to specify a unique section name to each plugin interface class in order to find all implementations of that plugin interface in the libraries containing plugins.

inline bool isPluginAvailable(const std::string &plugin_name) const

Check if plugin is available.

Parameters:

plugin_name – The plugin name to find

Returns:

True if plugin is found

inline std::vector<std::string> getAvailablePlugins(const std::string &section) const

Get the available plugins under the provided section.

Parameters:

section – The section name to get all available plugins

Returns:

A list of available plugins under the provided section

inline std::vector<std::string> getAvailableSections(bool include_hidden = false) const

Get the available sections within the provided search libraries.

Returns:

A list of available sections

inline int count() const

The number of plugins stored. The size of plugins variable.

Returns:

The number of plugins.

inline bool empty() const

Check if empty.

Returns:

True if no search libraries exist

Public Members

bool search_system_folders = {true}

Indicate is system folders may be search if plugin is not found in any of the paths.

std::set<std::string> search_paths

A list of paths to search for plugins.

std::set<std::string> search_libraries

A list of library names without the prefix or suffix that contain plugins.

std::string search_paths_env

The environment variable containing plugin search paths.

std::string search_libraries_env

The environment variable containing plugins.

The plugins are store ins the following format. The library name does not contain prefix or suffix Format: library_name:library_name1:library_name2

Protected Functions

template<typename PluginBase>
void reportErrorCommon(std::ostream &msg, const std::string &plugin_name, bool search_system_folders, const std::set<std::string> &search_paths, const std::set<std::string> &search_libraries) const
template<typename PluginBase>
std::enable_if_t<!has_getSection<PluginBase>::value, void> reportError(std::ostream &msg, const std::string &plugin_name, bool search_system_folders, const std::set<std::string> &search_paths, const std::set<std::string> &search_libraries) const
template<typename PluginBase>
std::enable_if_t<has_getSection<PluginBase>::value, void> reportError(std::ostream &msg, const std::string &plugin_name, bool search_system_folders, const std::set<std::string> &search_paths, const std::set<std::string> &search_libraries) const