39 template<
typename T >
42 for(
auto& plugin : _plugins )
43 if( plugin.handles( initData ))
44 return plugin.construct( initData );
46 LBTHROW( std::runtime_error(
"No plugin implementation available for " +
47 std::to_string( initData )));
50 template<
typename T >
53 _plugins.push_back( plugin );
56 template<
typename T >
59 typename Plugins::iterator i = std::find( _plugins.begin(), _plugins.end(),
61 if( i == _plugins.end( ))
71 for(
auto& plugin : _libraries )
76 template<
typename T >
78 const std::string& pattern )
83 for(
const auto& path : unique )
84 load( version, path, pattern );
87 template<
typename T >
89 const std::string& pattern )
92 const std::string regex( pattern +
".dll" );
94 const std::string regex(
"lib" + pattern +
".dylib" );
96 const std::string regex(
"lib" + pattern +
".so" );
100 for(
const auto& lib : libs )
109 typedef int( *GetVersion_t )();
110 typedef bool( *Register_t )();
113 "LunchboxPluginGetVersion" );
115 "LunchboxPluginRegister" );
116 const bool matchesVersion = getVersion && (getVersion() == version);
118 if( !getVersion || !registerFunc || !matchesVersion )
120 LBERROR <<
"Disable " << lib <<
": " 121 << ( getVersion ?
"" :
122 "Symbol for LunchboxPluginGetVersion missing " )
123 << ( registerFunc ?
"" :
124 "Symbol for LunchboxPluginRegister missing " );
125 if( getVersion && !matchesVersion )
126 LBERROR <<
"Plugin version " << getVersion() <<
" does not" 127 <<
" match application version " << version;
136 _libraries.insert( std::make_pair( dso, _plugins.back( )));
137 LBINFO <<
"Enabled plugin " << lib << std::endl;
Helper to access dynamic shared objects (DSO)
void * getFunctionPointer(const std::string &functionName) const
void deregisterAll()
Unregister all plugin types.
std::vector< std::string > Strings
A vector of std::strings.
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.
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.
T * create(const typename T::InitDataT &initData)
Create a plugin instance.
void register_(const PluginT &plugin)
Register a plugin type.
#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.
Manages a class deriving from a T interface.
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.