Menu

[r2398]: / framework / trunk / src / plugin.cpp  Maximize  Restore  History

Download this file

137 lines (121 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2016 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/plugin.h>
#include <map>
#include <booster/thread.h>
namespace cppcms {
namespace plugin {
namespace {
struct init {
init() { manager::instance(); }
} init_inst;
}
struct single_entry {
single_entry(manager::entry_point_type ep=NULL,char const *sig="") : entry(ep),signature(sig) {}
manager::entry_point_type entry;
std::string signature;
};
typedef std::map<std::string,single_entry> entries_type;
typedef std::map<std::string,entries_type> plugins_type;
struct manager::_data {
plugins_type plugins;
booster::mutex lock;
};
manager &manager::instance()
{
static manager m;
return m;
}
manager::manager() : d(new manager::_data())
{
}
manager::~manager()
{
}
void manager::add_entry(char const *plugin,char const *name,manager::entry_point_type e,char const *sig)
{
try {
booster::unique_lock<booster::mutex> guard(d->lock);
d->plugins[plugin].insert(std::make_pair<std::string,single_entry>(name,single_entry(e,sig)));
}
catch(...){}
}
void manager::remove_entry(manager::entry_point_type e)
{
try {
booster::unique_lock<booster::mutex> guard(d->lock);
for(plugins_type::iterator p=d->plugins.begin();p!=d->plugins.end();++p) {
for(entries_type::iterator it = p->second.begin();it!=p->second.end();++it) {
if(it->second.entry==e) {
p->second.erase(it);
if(p->second.empty())
d->plugins.erase(p);
return;
}
}
}
}
catch(...){}
}
manager::entry_point_type manager::get_entry(std::string const &plugin,std::string const &name)
{
booster::unique_lock<booster::mutex> guard(d->lock);
plugins_type::const_iterator p=d->plugins.find(plugin);
if(p==d->plugins.end())
return 0;
entries_type::const_iterator it=p->second.find(name);
if(it!=p->second.end())
return it->second.entry;
return 0;
}
std::string manager::signature(std::string const &plugin,std::string const &name)
{
booster::unique_lock<booster::mutex> guard(d->lock);
plugins_type::const_iterator p=d->plugins.find(plugin);
if(p==d->plugins.end())
return "";
entries_type::const_iterator it=p->second.find(name);
if(it!=p->second.end())
return it->second.signature;
return "";
}
std::set<std::string> manager::plugins()
{
std::set<std::string> r;
booster::unique_lock<booster::mutex> guard(d->lock);
for(plugins_type::const_iterator p=d->plugins.begin();p!=d->plugins.end();++p) {
r.insert(p->first);
}
return r;
}
bool manager::has_plugin(std::string const &plugin)
{
booster::unique_lock<booster::mutex> guard(d->lock);
return d->plugins.find(plugin)!=d->plugins.end();
}
std::set<std::string> manager::entries(std::string const &name)
{
std::set<std::string> r;
booster::unique_lock<booster::mutex> guard(d->lock);
plugins_type::const_iterator pit = d->plugins.find(name);
if(pit==d->plugins.end())
return r;
for(entries_type::const_iterator p=pit->second.begin();p!=pit->second.end();++p) {
r.insert(p->first);
}
return r;
}
signature_error::signature_error(std::string const &msg) : msg_(msg) {}
signature_error::~signature_error() throw() {}
char const *signature_error::what() const throw()
{
return msg_.c_str();
}
} // plugin
} // cppcms
MongoDB Logo MongoDB