32 #include <Pt/System/Api.h>
33 #include <Pt/System/SystemError.h>
34 #include <Pt/System/Library.h>
62 const std::type_info&
iface()
const
67 virtual const char*
feature()
const = 0;
71 virtual const char*
info()
const = 0;
74 const std::type_info* _iface;
79 template <
typename Iface>
91 virtual Iface*
create() = 0;
95 virtual void destroy(Iface* instance) = 0;
114 template <
typename Class,
typename Iface>
127 {
return new Class; }
135 {
return _feature.c_str(); }
138 virtual const char*
info()
const
139 {
return _info.c_str(); }
142 std::string _feature;
148 template <
typename IfaceT,
typename PluginT = Plugin<IfaceT> >
152 typedef typename std::multimap< std::string, PluginT* > PluginMap;
153 typedef typename std::multimap< IfaceT*, PluginT* > InstanceMap;
168 Iterator(
typename PluginMap::const_iterator it)
174 { ++_it;
return *
this; }
178 {
return *(_it->second); }
182 {
return _it->second; }
186 {
return _it == it._it; }
190 {
return _it != it._it; }
194 typename PluginMap::const_iterator _it;
201 : _iface( typeid(IfaceT) )
205 : _iface( typeid(IfaceT) )
210 for(; *plugins != 0; ++plugins)
212 if( (*plugins)->iface() == _iface )
214 PluginT* p = (PluginT*)(*plugins);
226 void loadPlugin(
const std::string& sym,
const Path& path);
238 IfaceT*
create(
const std::string& feature);
242 IfaceT*
create(
const Iterator& feature);
251 {
return Iterator( _plugins.begin() ); }
256 {
return Iterator( _plugins.end() ); }
264 InstanceMap& instances()
265 {
return _instances; }
269 const std::type_info& _iface;
272 std::list<Library> _libs;
278 InstanceMap _instances;
282 template <
class IfaceT,
typename PluginT >
286 for(
typename InstanceMap::iterator it = _instances.begin(); it != _instances.end(); ++it)
288 it->second->destroy( it->first );
294 template <
class IfaceT,
typename PluginT >
299 void* symbol = shlib.
resolve( sym.c_str() );
305 for(; *plugins != 0; ++plugins)
307 if( (*plugins)->iface() == _iface )
309 PluginT* p = (PluginT*)(*plugins);
310 this->registerPlugin(*p);
314 _libs.push_back(shlib);
318 template <
class IfaceT,
typename PluginT >
321 typename PluginMap::value_type p(plugin.feature(), &plugin);
326 template <
class IfaceT,
typename PluginT >
329 typename PluginMap::iterator it = _plugins.find( plugin.feature() );
330 if( it != _plugins.end() )
337 template <
class IfaceT,
typename PluginT >
340 typename PluginMap::iterator it = _plugins.find(feature);
341 if( it == _plugins.end() )
346 PluginT* plugin = it->second;
347 IfaceT* iface = plugin->create();
350 typename InstanceMap::value_type elem(iface, plugin);
351 _instances.insert( elem );
358 template <
class IfaceT,
typename PluginT >
361 typename PluginMap::const_iterator it = pit._it;
363 PluginT* plugin = it->second;
364 IfaceT* iface = plugin->create();
367 _instances.insert( std::make_pair(iface, plugin) );
374 template <
class IfaceT,
typename PluginT >
377 typename InstanceMap::iterator it = _instances.find(inst);
378 if( it == _instances.end() )
383 it->second->destroy(inst);
384 _instances.erase(it);
392 #endif // Pt_Plugin_h
Iterator end() const
End of loaded plugins.
Definition: Plugin.h:255
Manages loaded plugins.
Definition: Plugin.h:149
BasicPlugin(const std::string &feature, const std::string &info=std::string())
Constructs with feature string and info.
Definition: Plugin.h:119
Iterator for loaded plugins.
Definition: Plugin.h:157
virtual const char * feature() const =0
Returns the plugin feature string.
virtual const char * feature() const
Returns the plugin feature string.
Definition: Plugin.h:134
Plugin()
Default Constructor.
Definition: Plugin.h:85
virtual const char * info() const =0
Returns the plugin info string.
void loadPlugin(const std::string &sym, const Path &path)
Loads plugins from a library.
Definition: Plugin.h:295
Represents a path in the file-system.
Definition: Path.h:47
IfaceT * create(const std::string &feature)
Creates an instance by name.
Definition: Plugin.h:338
Interface for plugins.
Definition: Plugin.h:80
ID for plugin exports.
Definition: Plugin.h:46
~PluginManager()
Destructor.
Definition: Plugin.h:283
Iterator()
Default Constructor.
Definition: Plugin.h:164
virtual Iface * create()=0
Creates an instance.
void destroy(IfaceT *inst)
Destroys an instance.
Definition: Plugin.h:375
const PluginId * operator->() const
Access iterator value.
Definition: Plugin.h:181
Exception class indication a system error.
Definition: SystemError.h:42
bool operator!=(const Iterator &it) const
Comparison operator.
Definition: Plugin.h:189
A plugin implementation.
Definition: Plugin.h:115
Shared library loader.
Definition: Library.h:106
virtual const char * info() const
Returns the plugin info string.
Definition: Plugin.h:138
virtual void destroy(Iface *instance)=0
Destroys an instance.
PluginManager()
Default Constructor.
Definition: Plugin.h:200
PluginId(const std::type_info &iface)
Construct with type info of plugin interface.
Definition: Plugin.h:51
void * resolve(const char *symbol) const
Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was n...
virtual ~PluginId()
Destructor.
Definition: Plugin.h:57
Iterator begin() const
Begin of loaded plugins.
Definition: Plugin.h:250
bool operator==(const Iterator &it) const
Comparison operator.
Definition: Plugin.h:185
Iterator & operator++()
Advances the iterator.
Definition: Plugin.h:173
Iface * create()
Creates an instance.
Definition: Plugin.h:126
const PluginId & operator*() const
Access iterator value.
Definition: Plugin.h:177
void unregisterPlugin(PluginT &plugin)
Unregisters a plugin.
Definition: Plugin.h:327
void registerPlugin(PluginT &plugin)
Registers a plugin.
Definition: Plugin.h:319
void destroy(Iface *instance)
Destroys an instance.
Definition: Plugin.h:130
const std::type_info & iface() const
Returns the type of the plugin interface.
Definition: Plugin.h:62