24 template<
typename PluginT,
typename InitDataT >
25 PluginFactory< PluginT, InitDataT >&
32 template<
typename PluginT,
typename InitDataT >
36 if( plugin.handles( initData ))
37 return plugin.constructor( initData );
39 LBTHROW( std::runtime_error(
"No plugin implementation available for " +
40 boost::lexical_cast<std::string>( initData )));
43 template<
typename PluginT,
typename InitDataT >
47 _plugins.push_back( plugin );
50 template<
typename PluginT,
typename InitDataT >
54 typename Plugins::iterator i =
55 std::find( _plugins.begin(), _plugins.end(), plugin );
56 if( i == _plugins.end( ))
63 template<
typename PluginT,
typename InitDataT >
67 BOOST_FOREACH(
typename PluginMap::value_type& plugin, _libraries )
72 template<
typename PluginT,
typename InitDataT >
75 const std::string& pattern )
81 BOOST_FOREACH(
const std::string& path, unique )
82 _load( result, version, path, pattern );
86 template< typename PluginT, typename InitDataT >
88 const std::
string& path,
89 const std::
string& pattern )
92 _load( loaded, version, path, pattern );
96 template<
typename PluginT,
typename InitDataT >
99 const std::string& path,
100 const std::string& pattern )
103 const std::string regex( pattern +
".dll" );
105 const std::string regex(
"lib" + pattern +
".dylib" );
107 const std::string regex(
"lib" + pattern +
".so" );
111 BOOST_FOREACH(
const std::string& lib, libs )
120 typedef int( *GetVersion_t )();
121 typedef bool( *Register_t )();
124 "LunchboxPluginGetVersion" );
126 "LunchboxPluginRegister" );
127 const bool matchesVersion = getVersion && (getVersion() == version);
129 if( !getVersion || !registerFunc || !matchesVersion )
131 LBERROR <<
"Disable " << lib <<
": "
132 << ( getVersion ?
"" :
133 "Symbol for LunchboxPluginGetVersion missing " )
134 << ( registerFunc ?
"" :
135 "Symbol for LunchboxPluginRegister missing " );
136 if( getVersion && !matchesVersion )
137 LBERROR <<
"Plugin version " << getVersion() <<
" does not"
138 <<
" match application version " << version;
147 _libraries.insert( std::make_pair( dso, _plugins.back( )));
148 result.push_back( dso );
149 LBINFO <<
"Enabled plugin " << lib << std::endl;
156 template<
typename PluginT,
typename InitDataT >
159 typename PluginMap::iterator i = _libraries.find( dso );
160 if( i == _libraries.end( ))
164 const bool ret = deregister( i->second );
165 _libraries.erase( i );
LUNCHBOX_API void * getFunctionPointer(const std::string &functionName) const
Helper to access dynamic shared objects (DSO)
LUNCHBOX_API bool isOpen() const
DSOs load(const int version, const std::string &path, const std::string &pattern)
Load all compatible plugin libraries from a directory matching a pattern.
std::vector< std::string > Strings
A vector of std::strings.
PluginT * create(const InitDataT &initData)
Create a plugin instance.
#define LBINFO
Output an informational message to the per-thread Log.
Factory for Plugin classes.
LUNCHBOX_API Strings searchDirectory(const std::string &directory, const std::string &pattern)
Retrieve a list of files in a directory matching a boost::regex pattern.
#define LBERROR
Output an error message to the per-thread Log stream.
bool deregister(const Plugin< PluginT, InitDataT > &plugin)
Deregister a plugin type.
bool unload(DSO *dso)
Unload and deregister a previously loaded plugin.
Manages a class deriving from a PluginT interface.
static PluginFactory & getInstance()
Get the single class instance.
void register_(const Plugin< PluginT, InitDataT > &plugin)
Register a plugin type.
void usort(C &c)
Uniquely sort and eliminate duplicates in a container.
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.
void deregisterAll()
Unregister all plugin types.