实现动态插件管理器



我正在尝试为 c++ 创建一个插件管理器,用于检查共享库的目录 (dll,so,dylib( 并检查是否有插件接口可用于存储插件并在以后检索它。

我正在使用 boost::d ll 导入 dll,但我不知道如何更好地设置模板。

如何使用模板或其他方法将插件接口添加到插件管理器,以便它将检查共享库中的导出别名并存储共享库句柄?

插件接口.hpp

class PluginInterface {
public:
virtual void testPlugin() = 0;
};

plugin.hpp source for "plugins_directory/plugin.dll">

#include <boost/config.hpp> // for BOOST_SYMBOL_EXPORT
#include "pluginInterface.hpp"
namespace PluginNamespace {
class PluginClass : public PluginInterface {
void testPlugin(){
printf("Hello World!");
}
}
extern "C" BOOST_SYMBOL_EXPORT PluginClass pluginName;
PluginClass pluginName;
}

插件管理器.hpp

class InterfaceMethodsBase {
public:
std::string pluginName;
InterfaceMethodsBase(std::string s) { pluginName = s; }
~InterfaceMethodsBase() {}
virtual void addPath(std::filesystem::path p) = 0;
};
template <class T> class InterfaceMethods : public InterfaceMethodsBase {
public:
InterfaceMethods(std::string s) : InterfaceMethodsBase(s) {}
~InterfaceMethods() {}
std::vector<boost::shared_ptr<T>> pluginPtrs;
void addPath(std::filesystem::path p) {
boost::filesystem::path lib_path(p.string().c_str());
std::cout << "PLUGIN: Loading " << p << "n";
boost::shared_ptr<T> plugin;
try {
plugin = boost::dll::import<T>(lib_path, pluginName,
boost::dll::load_mode::default_mode);
} catch (...) {
std::cout << "PLUGIN: Loading FAILED " << p << "n";
}
if (plugin) {
std::cout << "PLUGIN: Loading SUCCESS " << p << "n";
pluginPtrs.push_back(plugin);
}
}
};
class PluginManager {
private:
std::unordered_map<std::string, InterfaceMethodsBase *> interfaceMap;
public:
template <class T> void addPluginInterface(std::string pluginName) {
InterfaceMethods<T> *interface = new InterfaceMethods<T>(pluginName);
InterfaceMethodsBase *interfaceBase = (InterfaceMethodsBase *)interface;
interfaceMap.insert({pluginName, interface});
}
void loadPlugins(std::string directoryPathStr) {
for (auto &p :
std::filesystem::recursive_directory_iterator(directoryPathStr)) {
std::cout << "PLUGIN: File Found " << p.path() << "n";
if (p.is_regular_file() &&
(p.path().extension() == ".dll" || p.path().extension() == ".dylib" ||
p.path().extension() == ".so")) {
for (auto pairs : interfaceMap) {
pairs.second->addPath(p.path());
}
}
}
}
template <class T> boost::shared_ptr<T> getPlugin(std::string pluginName) {
InterfaceMethods<T> *interface =
dynamic_cast<InterfaceMethods<T> *>(interfaceMap.at(pluginName));
if (interface->pluginPtrs.empty()) {
return nullptr;
}
return interface->pluginPtrs.front();
}
};

主.cpp

#include "PluginManager.hpp"
#include "PluginInterface.hpp"
int main(){
PluginManager pluginManagerObj;
pluginManagerObj.addPluginInterface<PluginInterface>("pluginName");
pluginManagerObj.loadPlugins("plugins_directory");
boost::shared_ptr<PluginInterface> plugin = pluginManagerObj.getPlugin<PluginInterface>("pluginName");
plugin->testPlugin();
}

编辑:我在Windows上使用MinGW编译器

我最初的目的是为方法找到某种方法,loadPlugins()智能地查找和加载目录中的所有插件,并存储它们的句柄以供以后使用。

在提出最初的问题并弄乱可能的解决方案之后,我发现我可以使用模板来创建继承InterfaceMethodsBase的类InterfaceMethods,该类将处理单个接口类型的识别并将所有类实例存储在类型为InterfaceMethodsBase的哈希表中。调用loadPlugins()后,我可以从哈希表中检索发现的插件。

我将工作代码编辑回原始帖子。我希望将来有人可以从此解决方案中受益。

最新更新