CodeCompass

Форк
0
/
pluginhandler.cpp 
139 строк · 3.2 Кб
1
#include <parser/pluginhandler.h>
2

3
#include <boost/filesystem.hpp>
4
#include <boost/program_options.hpp>
5

6
#include <util/logutil.h>
7

8
namespace fs = ::boost::filesystem;
9

10
namespace
11
{
12
  /**
13
   * This function returns the real plugin name from path by removing the
14
   * file extension and the `lib` prefix.
15
   */
16
  std::string getPluginName(const boost::filesystem::path& path_)
17
  {
18
    // Filename without extension.
19
    std::string filename = path_.stem().string();
20
    // Remove "lib" from filename.
21
    filename.erase(filename.begin(), filename.begin() + 3);
22

23
    return filename;
24
  }
25
}
26

27
namespace cc
28
{
29
namespace parser
30
{
31

32
PluginHandler::PluginHandler(const std::string& pluginDir_)
33
  : _pluginDir(pluginDir_)
34
{
35
  if (!fs::exists(_pluginDir) || !fs::is_directory(_pluginDir))
36
    throw std::runtime_error(_pluginDir + " is not a directory!");
37
}
38

39
void PluginHandler::loadPlugins(std::vector<std::string>& skipParserList_)
40
{
41
  fs::directory_iterator endIter;
42
  for (fs::directory_iterator dirIter(_pluginDir);
43
    dirIter != endIter;
44
    ++dirIter)
45
  {
46
    if (fs::is_regular_file(dirIter->status()) &&
47
        fs::extension(*dirIter) == util::DynamicLibrary::extension())
48
    {
49
      std::string filename = getPluginName(dirIter->path());
50

51
      if (std::find(skipParserList_.begin(), skipParserList_.end(), filename) ==
52
        skipParserList_.end())
53
      {
54
        std::string dynamicLibraryPath = dirIter->path().string();
55
        _dynamicLibraries[filename] = util::DynamicLibraryPtr(
56
          new util::DynamicLibrary(dynamicLibraryPath));
57
      }
58
      else
59
      {
60
        LOG(info) << "[" << filename << "] skipped!";
61
      }
62
    }
63
  }
64
}
65

66
std::vector<std::string> PluginHandler::getLoadedPluginNames() const
67
{
68
  std::vector<std::string> plugins;
69

70
  for (const auto& lib : _dynamicLibraries)
71
  {
72
    plugins.push_back(lib.first);
73
  }
74

75
  return plugins;
76
}
77

78
std::vector<std::string> PluginHandler::getPluginNames() const
79
{
80
  std::vector<std::string> plugins;
81

82
  fs::directory_iterator endIter;
83
  for (fs::directory_iterator dirIter(_pluginDir);
84
    dirIter != endIter;
85
    ++dirIter)
86
  {
87
    if (fs::is_regular_file(dirIter->status()) &&
88
        fs::extension(*dirIter) == util::DynamicLibrary::extension())
89
    {
90
      plugins.push_back(getPluginName(dirIter->path()));
91
    }
92
  }
93

94
  return plugins;
95
}
96

97
bool PluginHandler::createPlugins(ParserContext& ctx_)
98
{
99
  for (const auto& lib : _dynamicLibraries)
100
  {
101
    typedef std::shared_ptr<AbstractParser> (*makeParser)(ParserContext& _ctx);
102
    auto make = reinterpret_cast<makeParser>(lib.second->getSymbol("make"));
103
    std::shared_ptr<AbstractParser> parser = make(ctx_);
104
    _parsers[lib.first] = parser;
105
  }
106
  return true;
107
}
108

109
boost::program_options::options_description PluginHandler::getOptions() const
110
{
111
  namespace po = ::boost::program_options;
112

113
  po::options_description desc("Options of plugins");
114
  for (const auto& lib : _dynamicLibraries)
115
  {
116
    typedef po::options_description (*GetOptsFuncPtr)();
117

118
    GetOptsFuncPtr getOptions = reinterpret_cast<GetOptsFuncPtr>(
119
      lib.second->getSymbol("getOptions"));
120

121
    desc.add(getOptions());
122
  }
123

124
  return desc;
125
}
126

127
std::shared_ptr<AbstractParser>& PluginHandler::getParser(
128
  const std::string& parserName_)
129
{
130
  return _parsers.at(parserName_);
131
}
132

133
PluginHandler::~PluginHandler()
134
{
135
  _parsers.clear();
136
}
137

138
}
139
}
140

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.