如何插件系统的工作? [英] How do plugin systems work?

查看:105
本文介绍了如何插件系统的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我会找一个基本的插件系统非常有用。从本质上讲,我创建的基类,可以提供这样的基类插件开发者。然后,开发人员覆盖,并覆盖的方法。那么这是它变得有点我不清楚。它是如何从这里工作吗?我在哪里能找到的文档属于这种类型的系统的发展?

I'm working on a project where I would find a basic plugin system useful. Essentially, I create the base class and can provide this base class to a plugin developer. Then the developer overrides it and overrides the methods. Then this is where it becomes a bit unclear to me. How does it work from here? Where could I find documentation pertaining to the development of this type of system?

感谢

推荐答案

我知道所有的插件系统使用动态库。基本上,你需要定义系统内核和插件之间的小,有效握手。由于没有C ++ ABI,插件必须要么只使用一个C API或者使用非常相同的编译器(也可能是编译器的版本)作为系统的内核。

The plugin systems I know of all use dynamic libraries. Basically, you need to define a small, effective handshake between the system kernel and the plugins. Since there is no C++ ABI, plugins must either only use a C API or use the very same compiler (and probably compiler version) as the system's kernel.

最简单可以想象协议将是一个功能,即所有插件开发人员必须提供,它返回从基类派生的类的实例,返回一个基类指针。 (在的externC可确保功能将不会有一个错位的名称,因此更容易找到它的名称。)是这样的:

The simplest thinkable protocol would be a function, that all plugin developers would have to provide, which returns an instance of a class derived from your base class, returned as a base class pointer. (The extern "C" makes sure that function won't have a mangled name, and is thus easier to find by its name.) Something like:

extern "C" {
  plugin_base* get_plugin();
}

内核会然后尝试加载在指定的场所作为动态库中找到二进制文件,并试图找到 get_plugin()功能。如果成功,它调用这个函数,并用装插件实例结束。

The kernel would then try to load binaries found in designated places as dynamic libraries and attempt to find the get_plugin() function. If it succeeds, it calls this function and ends up with a loaded plugin instance.

当然,这将是很好也有功能检查插件是对主场迎战内核版本编译的API的版本。 (您可能需要更改基类,毕竟),你可能有其他的功能,这些功能返回有关插件的信息(或者你有这样的作为基类的虚函数)。这在很大程度上取决于你的系统的性质。

Of course, it would be nice to also have functions to check the version of the API the plugin was compiled against vs. the version of the kernel. (You might change that base class, after all.) And you might have other functions, which return information about the plugin (or you have this as virtuals in the base class). That depends a lot on the nature of your system.

这篇关于如何插件系统的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆