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)
192 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
193 m_plugin_map.insert(std::make_pair(plugin_id,
194 std::make_pair(plugin_object_ptr, plugin_ptr)));
197 template <
typename PluginType>
200 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
202 if (i == m_plugin_map.end())
204 if (i->second.second.is_open()) {
205 i->second.second.destroy(i->second.first);
207 delete i->second.first;
209 m_plugin_map.erase(i);
212 template <
typename PluginType>
215 BOOST_ASSERT(plugin_ptr);
216 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
218 if (i == m_plugin_map.end())
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>
231 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
233 if (i == m_plugin_map.end())
235 return i->second.second.create();
238 template <
typename PluginType>
240 const std::string& plugin_type)
243 if (m_plugin_map.find(plugin_id) != m_plugin_map.end())
248 plugin_ptr.
open(plugin_type);
251 PluginType *plugin_object_ptr(plugin_ptr.
create());
254 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
255 m_plugin_map.insert(std::make_pair(plugin_id,
256 std::make_pair(plugin_object_ptr, plugin_ptr)));
258 return plugin_object_ptr;
261 template <
typename PluginType>
264 PluginType *plugin_object_ptr = NULL;
265 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
267 if (i != m_plugin_map.end())
268 plugin_object_ptr = i->second.first;
269 return plugin_object_ptr;
272 template <
typename PluginType>
275 const PluginType *plugin_object_ptr = NULL;
276 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
278 if (i != m_plugin_map.end())
279 plugin_object_ptr = i->second.first;
280 return plugin_object_ptr;
283 template <
typename PluginType>
287 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
289 if (i != m_plugin_map.end())
290 plugin_ptr = i->second.second;
294 template <
typename PluginType>
298 PluginType *plugin_object_ptr = NULL;
301 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
304 if (m_plugin_map.empty())
return plugin_object_ptr;
308 while (i != m_plugin_map.begin()) {
312 if (resource.compare(0, i->first.size(), i->first) != 0) {
314 if (i != m_plugin_map.begin()) {
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>
339 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
341 i != m_plugin_map.end(); ++i)
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;
362 boost::mutex::scoped_lock plugins_lock(m_plugin_mutex);
364 i != m_plugin_map.end(); ++i)
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;
399 this->erase(std::map<std::string, std::pair<PluginType *, plugin_ptr<PluginType> > >::begin(),
400 std::map<std::string, std::pair<PluginType *, plugin_ptr<PluginType> > >::end());
virtual ~plugin_manager()
default destructor
void run(PluginRunFunction run_func)
boost::uint64_t get_statistic(PluginStatFunction stat_func) const
void add(const std::string &plugin_id, PluginType *plugin_object_ptr)
PluginType * load(const std::string &plugin_id, const std::string &plugin_type)
bool empty(void) const
returns true if there are no plug-in objects being managed
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
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
plugin_ptr< PluginType > get_lib_ptr(const std::string &plugin_id) const
boost::function1< boost::uint64_t, const PluginType * > PluginStatFunction
data type for a function that may be called by the getStat() method
boost::mutex m_plugin_mutex
mutex to make class thread-safe
void replace(const std::string &plugin_id, PluginType *plugin_ptr)