gpt4 book ai didi

c++ - 实现动态插件管理器

转载 作者:行者123 更新时间:2023-12-02 10:20:56 45 4
gpt4 key购买 nike

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

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

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

插件接口(interface).hpp

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

“plugins_directory/plugin.dll”的 plugin.hpp 源

#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();
}
};

主文件

#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()找到一些方法。智能地查找并加载目录中的所有插件并存储它们的句柄以供以后使用。

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

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

关于c++ - 实现动态插件管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220913/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com