forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginhandler.cpp
More file actions
139 lines (115 loc) · 3.26 KB
/
pluginhandler.cpp
File metadata and controls
139 lines (115 loc) · 3.26 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
137
138
139
#include <parser/pluginhandler.h>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <util/logutil.h>
namespace fs = ::boost::filesystem;
namespace
{
/**
* This function returns the real plugin name from path by removing the
* file extension and the `lib` prefix.
*/
std::string getPluginName(const boost::filesystem::path& path_)
{
// Filename without extension.
std::string filename = path_.stem().string();
// Remove "lib" from filename.
filename.erase(filename.begin(), filename.begin() + 3);
return filename;
}
}
namespace cc
{
namespace parser
{
PluginHandler::PluginHandler(const std::string& pluginDir_)
: _pluginDir(pluginDir_)
{
if (!fs::exists(_pluginDir) || !fs::is_directory(_pluginDir))
throw std::runtime_error(_pluginDir + " is not a directory!");
}
void PluginHandler::loadPlugins(std::vector<std::string>& skipParserList_)
{
fs::directory_iterator endIter;
for (fs::directory_iterator dirIter(_pluginDir);
dirIter != endIter;
++dirIter)
{
if (fs::is_regular_file(dirIter->status()) &&
fs::path(*dirIter).extension() == util::DynamicLibrary::extension())
{
std::string filename = getPluginName(dirIter->path());
if (std::find(skipParserList_.begin(), skipParserList_.end(), filename) ==
skipParserList_.end())
{
std::string dynamicLibraryPath = dirIter->path().string();
_dynamicLibraries[filename] = util::DynamicLibraryPtr(
new util::DynamicLibrary(dynamicLibraryPath));
}
else
{
LOG(info) << "[" << filename << "] skipped!";
}
}
}
}
std::vector<std::string> PluginHandler::getLoadedPluginNames() const
{
std::vector<std::string> plugins;
for (const auto& lib : _dynamicLibraries)
{
plugins.push_back(lib.first);
}
return plugins;
}
std::vector<std::string> PluginHandler::getPluginNames() const
{
std::vector<std::string> plugins;
fs::directory_iterator endIter;
for (fs::directory_iterator dirIter(_pluginDir);
dirIter != endIter;
++dirIter)
{
if (fs::is_regular_file(dirIter->status()) &&
fs::path(*dirIter).extension() == util::DynamicLibrary::extension())
{
plugins.push_back(getPluginName(dirIter->path()));
}
}
return plugins;
}
bool PluginHandler::createPlugins(ParserContext& ctx_)
{
for (const auto& lib : _dynamicLibraries)
{
typedef std::shared_ptr<AbstractParser> (*makeParser)(ParserContext& _ctx);
auto make = reinterpret_cast<makeParser>(lib.second->getSymbol("make"));
std::shared_ptr<AbstractParser> parser = make(ctx_);
_parsers[lib.first] = parser;
}
return true;
}
boost::program_options::options_description PluginHandler::getOptions() const
{
namespace po = ::boost::program_options;
po::options_description desc("Options of plugins");
for (const auto& lib : _dynamicLibraries)
{
typedef po::options_description (*GetOptsFuncPtr)();
GetOptsFuncPtr getOptions = reinterpret_cast<GetOptsFuncPtr>(
lib.second->getSymbol("getOptions"));
desc.add(getOptions());
}
return desc;
}
std::shared_ptr<AbstractParser>& PluginHandler::getParser(
const std::string& parserName_)
{
return _parsers.at(parserName_);
}
PluginHandler::~PluginHandler()
{
_parsers.clear();
}
}
}