2020-03-16 15:11:40 +01:00
|
|
|
#ifndef _plugin_H_
|
|
|
|
#define _plugin_H_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
2021-10-05 14:25:04 +02:00
|
|
|
#include <random>
|
2020-03-16 15:11:40 +01:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <dirent.h>
|
2020-12-16 13:43:19 +01:00
|
|
|
#include <libscerror.hpp>
|
2020-03-16 15:11:40 +01:00
|
|
|
|
|
|
|
namespace sc {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class plugin {
|
2021-10-05 14:25:04 +02:00
|
|
|
public:
|
|
|
|
static void scan_plugins(const std::string& dir, const std::string& ext) {
|
2022-09-14 09:55:51 +02:00
|
|
|
DIR* plugins_dir = opendir(dir.c_str());
|
2021-10-05 14:25:04 +02:00
|
|
|
throw_if_null_msg(plugins_dir, (dir + " could not be opened").c_str());
|
|
|
|
struct dirent* info;
|
2022-09-14 09:55:51 +02:00
|
|
|
while ((info = readdir(plugins_dir)) != nullptr) {
|
2021-10-05 14:25:04 +02:00
|
|
|
if (info->d_name[0] != '.') {
|
|
|
|
std::string plugname {info->d_name};
|
|
|
|
std::string::size_type lastdot = plugname.rfind(".");
|
|
|
|
if (plugname.substr(lastdot + 1) == ext) {
|
|
|
|
plugin plugin {dir + '/' + plugname};
|
2021-10-06 14:12:38 +02:00
|
|
|
plugin.name(plugname.substr(0, lastdot));
|
|
|
|
plugins.emplace(plugin.name(), std::move(plugin));
|
2021-10-05 14:25:04 +02:00
|
|
|
}
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-14 09:55:51 +02:00
|
|
|
throw_if_min1(closedir(plugins_dir));
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
using processor = std::function<void(const plugin&)>;
|
|
|
|
static void foreach(processor proc) {
|
|
|
|
for (const auto& elem : plugins) {
|
|
|
|
proc(elem.second);
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 15:11:40 +01:00
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
static plugin& get(const std::string& name) {
|
|
|
|
return plugins.at(name);
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
2021-10-05 14:25:04 +02:00
|
|
|
|
|
|
|
static std::vector<std::string> names() {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
for (const auto& elem : plugins) {
|
|
|
|
result.emplace_back(elem.first);
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
2021-10-05 14:25:04 +02:00
|
|
|
return result;
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
static size_t count() { return plugins.size(); }
|
|
|
|
|
|
|
|
plugin(const std::string& path) : _path {path} {
|
2022-09-14 09:55:51 +02:00
|
|
|
_plugin = dlopen(path.c_str(), RTLD_LAZY);
|
2021-10-05 14:25:04 +02:00
|
|
|
|
2022-09-14 09:55:51 +02:00
|
|
|
const char* err {dlerror()};
|
2021-10-05 14:25:04 +02:00
|
|
|
if (err) std::cerr << err << '\n';
|
|
|
|
if (_plugin) {
|
2022-09-14 09:55:51 +02:00
|
|
|
_factory = reinterpret_cast<factory>(dlsym(_plugin, "create_instance"));
|
2021-10-05 14:25:04 +02:00
|
|
|
if (!_factory) {
|
|
|
|
std::cerr << "create_instance() function not found in " << path << '\n';
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
~plugin() {
|
|
|
|
if (_plugin) {
|
2022-09-14 09:55:51 +02:00
|
|
|
dlclose(_plugin);
|
2021-10-05 14:25:04 +02:00
|
|
|
_plugin = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 15:11:40 +01:00
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
plugin(const plugin&) = delete;
|
|
|
|
plugin& operator=(const plugin&) = delete;
|
2020-03-16 15:11:40 +01:00
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
plugin(plugin&& other) {
|
2020-03-16 15:11:40 +01:00
|
|
|
_path = other._path;
|
|
|
|
_plugin = other._plugin;
|
|
|
|
_factory = other._factory;
|
|
|
|
other._plugin = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
plugin& operator=(plugin&& other) {
|
|
|
|
if (this != &other) {
|
|
|
|
_path = other._path;
|
|
|
|
_plugin = other._plugin;
|
|
|
|
_factory = other._factory;
|
|
|
|
other._plugin = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2020-03-16 15:11:40 +01:00
|
|
|
|
2021-10-06 14:12:38 +02:00
|
|
|
const std::string& path() const { return _path; }
|
|
|
|
|
|
|
|
const std::string& name() const { return _name; }
|
|
|
|
void name(const std::string& nm) { _name = nm; }
|
2021-10-05 14:25:04 +02:00
|
|
|
|
|
|
|
std::unique_ptr<T> operator()() const {
|
|
|
|
std::unique_ptr<T> obj;
|
|
|
|
if (_factory) {
|
|
|
|
obj.reset(_factory());
|
|
|
|
}
|
|
|
|
return obj;
|
2020-03-16 15:11:40 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
private:
|
|
|
|
static std::map<std::string, plugin<T>> plugins;
|
2020-03-16 15:11:40 +01:00
|
|
|
|
2021-10-05 14:25:04 +02:00
|
|
|
std::string _path;
|
2021-10-06 14:12:38 +02:00
|
|
|
std::string _name;
|
2021-10-05 14:25:04 +02:00
|
|
|
void* _plugin {nullptr};
|
|
|
|
using factory = T*(*)();
|
|
|
|
factory _factory {nullptr};
|
2020-03-16 15:11:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::map<std::string, plugin<T>> plugin<T>::plugins;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _plugin_H_
|