10 #ifndef __PION_PLUGIN_MANAGER_HEADER__ 11 #define __PION_PLUGIN_MANAGER_HEADER__ 15 #include <boost/cstdint.hpp> 16 #include <boost/assert.hpp> 17 #include <boost/function.hpp> 18 #include <boost/function/function1.hpp> 19 #include <boost/thread/mutex.hpp> 20 #include <pion/config.hpp> 21 #include <pion/error.hpp> 22 #include <pion/plugin.hpp> 30 template <
typename PluginType>
66 inline void add(
const std::string& plugin_id, PluginType *plugin_object_ptr);
73 inline void remove(
const std::string& plugin_id);
89 inline PluginType *
clone(
const std::string& plugin_id);
99 inline PluginType *
load(
const std::string& plugin_id,
const std::string& plugin_type);
107 inline PluginType *
get(
const std::string& plugin_id);
115 inline const PluginType *
get(
const std::string& plugin_id)
const;
131 inline PluginType *
find(
const std::string& resource);
138 inline void run(PluginRunFunction run_func);
146 inline void run(
const std::string& plugin_id, PluginRunFunction run_func);
153 inline boost::uint64_t
get_statistic(PluginStatFunction stat_func)
const;
161 inline boost::uint64_t
get_statistic(
const std::string& plugin_id,
162 PluginStatFunction stat_func)
const;
169 :
public std::map<std::string, std::pair<PluginType *, plugin_ptr<PluginType> > >
172 inline void clear(
void);
173 virtual ~
map_type() { map_type::clear(); }
187 template <
typename PluginType>
189 PluginType *plugin_object_ptr)
194 std::make_pair(plugin_object_ptr, plugin_ptr)));
197 template <
typename PluginType>
204 if (i->second.second.is_open()) {
205 i->second.second.destroy(i->second.first);
207 delete i->second.first;
212 template <
typename PluginType>
215 BOOST_ASSERT(plugin_ptr);
220 if (i->second.second.is_open()) {
221 i->second.second.destroy(i->second.first);
223 delete i->second.first;
225 i->second.first = plugin_ptr;
228 template <
typename PluginType>
235 return i->second.second.create();
238 template <
typename PluginType>
240 const std::string& plugin_type)
248 plugin_ptr.
open(plugin_type);
251 PluginType *plugin_object_ptr(plugin_ptr.
create());
256 std::make_pair(plugin_object_ptr, plugin_ptr)));
258 return plugin_object_ptr;
261 template <
typename PluginType>
264 PluginType *plugin_object_ptr = NULL;
268 plugin_object_ptr = i->second.first;
269 return plugin_object_ptr;
272 template <
typename PluginType>
275 const PluginType *plugin_object_ptr = NULL;
279 plugin_object_ptr = i->second.first;
280 return plugin_object_ptr;
283 template <
typename PluginType>
290 plugin_ptr = i->second.second;
294 template <
typename PluginType>
298 PluginType *plugin_object_ptr = NULL;
312 if (resource.compare(0, i->first.size(), i->first) != 0) {
318 if (j->first.size() < i->first.size())
327 if (resource.size() == i->first.size() || resource[i->first.size()]==
'/') {
328 plugin_object_ptr = i->second.first;
333 return plugin_object_ptr;
336 template <
typename PluginType>
343 run_func(i->second.first);
347 template <
typename PluginType>
352 PluginType *plugin_object_ptr =
get(plugin_id);
353 if (plugin_object_ptr == NULL)
355 run_func(plugin_object_ptr);
358 template <
typename PluginType>
361 boost::uint64_t stat_value = 0;
366 stat_value += stat_func(i->second.first);
371 template <
typename PluginType>
377 if (plugin_object_ptr == NULL)
379 return stat_func(plugin_object_ptr);
385 template <
typename PluginType>
391 i != std::map<std::string, std::pair<PluginType *, plugin_ptr<PluginType> > >::end(); ++i)
393 if (i->second.second.is_open()) {
394 i->second.second.destroy(i->second.first);
396 delete i->second.first;
virtual ~plugin_manager()
default destructor
void run(PluginRunFunction run_func)
void add(const std::string &plugin_id, PluginType *plugin_object_ptr)
PluginType * load(const std::string &plugin_id, const std::string &plugin_type)
exception thrown if we try to add or load a duplicate plugin
PluginType * find(const std::string &resource)
plugin_manager(void)
default constructor
InterfaceClassType * create(void)
creates a new instance of the plug-in object
plugin_ptr< PluginType > get_lib_ptr(const std::string &plugin_id) const
map_type m_plugin_map
collection of plug-in objects being managed
boost::function1< void, PluginType * > PluginRunFunction
data type for a function that may be called by the run() method
exception thrown if a plugin cannot be found
PluginType * clone(const std::string &plugin_id)
PluginType * get(const std::string &plugin_id)
void remove(const std::string &plugin_id)
void clear(void)
clears all the plug-in objects being managed
void open(const std::string &plugin_name)
data type that maps identifiers to plug-in objects
boost::function1< boost::uint64_t, const PluginType * > PluginStatFunction
data type for a function that may be called by the getStat() method
boost::uint64_t get_statistic(PluginStatFunction stat_func) const
bool empty(void) const
returns true if there are no plug-in objects being managed
boost::mutex m_plugin_mutex
mutex to make class thread-safe
void replace(const std::string &plugin_id, PluginType *plugin_ptr)