44 for (
auto& plugin : _plugins)
45 if (plugin.handles(initData))
53 for (
auto& plugin : _plugins)
54 if (plugin.handles(initData))
55 return plugin.construct(initData);
57 LBTHROW(std::runtime_error(
"No plugin implementation available for " +
58 std::to_string(initData)));
64 _plugins.push_back(plugin);
70 typename Plugins::iterator i =
71 std::find(_plugins.begin(), _plugins.end(), plugin);
72 if (i == _plugins.end())
83 for (
auto& plugin : _libraries)
91 std::string descriptions;
92 for (
const auto& plugin : _plugins)
94 (descriptions.empty() ?
"" :
"\n\n") + plugin.getDescription();
100 const std::string& pattern)
105 for (
const auto& path : unique)
106 load(version, path, pattern);
109 template <
typename T>
111 const std::string& pattern)
114 const std::string regex(pattern +
".dll");
116 const std::string regex(
"lib" + pattern +
".dylib");
118 const std::string regex(
"lib" + pattern +
".so");
122 for (
const auto& lib : libs)
131 typedef int (*GetVersion_t)();
132 typedef bool (*Register_t)();
134 GetVersion_t getVersion =
136 Register_t registerFunc =
138 const bool matchesVersion = getVersion && (getVersion() == version);
140 if (!getVersion || !registerFunc || !matchesVersion)
142 LBERROR <<
"Disable " << lib <<
": " 145 :
"Symbol for LunchboxPluginGetVersion missing ")
148 :
"Symbol for LunchboxPluginRegister missing ");
149 if (getVersion && !matchesVersion)
150 LBERROR <<
"Plugin version " << getVersion() <<
" does not" 151 <<
" match application version " << version;
160 _libraries.insert(std::make_pair(dso, _plugins.back()));
161 LBINFO <<
"Loaded plugin " << lib << std::endl;
Helper to access dynamic shared objects (DSO)
void * getFunctionPointer(const std::string &functionName) const
void deregisterAll()
Unregister all plugin types.
Strings searchDirectory(const std::string &directory, const std::string &pattern)
Retrieve a list of files in a directory matching a boost::regex pattern.
#define LBINFO
Output an informational message to the per-thread Log.
Factory for Plugin classes.
std::string getDescriptions() const
T * create(const typename T::InitDataT &initData)
Create a plugin instance.
void register_(const PluginT &plugin)
Register a plugin type.
std::vector< std::string > Strings
A vector of std::strings.
#define LBERROR
Output an error message to the per-thread Log stream.
void load(const int version, const std::string &path, const std::string &pattern)
Load all compatible plugin libraries from a directory matching a pattern.
Abstraction layer and common utilities for multi-threaded programming.
bool handles(const typename T::InitDataT &initData)
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.
static PluginFactory & getInstance()
Get the single class instance.
void usort(C &c)
Uniquely sort and eliminate duplicates in a container.
bool deregister(const PluginT &plugin)
Deregister a plugin type.