简体   繁体   English

用C ++编写的用于文本编辑器的插件系统

[英]Plugin system for text editor writen in C++

I am creating a cross platform text editor in C++. 我正在用C ++创建一个跨平台的文本编辑器。 I would like to have a very basic base, then implement all the features through plug-ins. 我想有一个非常基础的基础,然后通过插件实现所有功能。 But, unfortunately I'm getting nowhere in designing the plug-in system. 但是,不幸的是,我在设计插件系统方面无济于事。 How is this normally done? 通常如何做? Can someone point me in the right direction? 有人可以指出我正确的方向吗?

Don't know if it matters, but I'm using the wxWidgets widget kit. 不知道这是否重要,但是我正在使用wxWidgets小部件套件。

You can start by having a base class defining a specific plugin interface ie: TextTransformPlugin, with a method taking a string and returning a string (virtual). 您可以从定义一个特定插件接口的基类开始,即:TextTransformPlugin,该方法带有一个字符串并返回一个字符串(虚拟)。

Each plugin would inherit from this interface and you build this class eg: SpanishTranslateTransformPlugin in a dynamic library (.so file). 每个插件都将从该接口继承,您可以构建此类,例如:动态库(.so文件)中的SpanishTranslateTransformPlugin。

From the program you use dlopen to open the library (see here for a C++ example). 使用dlopen在程序中打开库(有关C ++示例,请参见此处 )。 As you can't call the class constuctor, in the so library you define a standard function (same name for all plugins, lets say create() and give it C calling conventions so that you can then use dlsym to get the symbol and cast it to a function returning a TextTransformPlugin and call it. 由于无法调用类构造函数,因此在so库中定义了一个标准函数(所有插件的名称相同,可以说create()并为其指定C调用约定,以便随后可以使用dlsym获取符号并进行强制转换它返回一个返回TextTransformPlugin的函数并调用它。

extern "C" {
    TextTransformPlugin * create(); // this would return new SpanishTranslateTransformPlugin
}

That way you will get a TextTransformPlugin object which is the plugin. 这样,您将获得一个TextTransformPlugin对象,即插件。 As the interface methods are virtual, the concrete methods will be called. 由于接口方法是虚拟的,因此将调用具体方法。

You will have to take care of the lifecycle of the plugin, keeping them in a registry. 您将必须照顾插件的生命周期,并将其保存在注册表中。 Knowing when to use them, and finally destroying them and closing the libraries. 知道何时使用它们,最后销毁它们并关闭库。

Note that Windows does not have dlfcn.h where you find dlopen. 请注意,Windows在您找到dlopen的地方没有dlfcn.h。 There is similar functionality in the LoadLibrary API, but you would need to abstract the platforms yourself. LoadLibrary API中有类似的功能,但是您需要自己抽象平台。

If you use a multiplatform framework like Qt, you get a lot of the boilerplate for free and it would work across the supported platforms. 如果您使用像Qt这样的多平台框架,则可以免费获得很多样板,并且可以在支持的平台上运行。 Here is an example of a pluggable paint application: 这是可插拔油漆应用程序的示例:

http://doc.qt.nokia.com/latest/tools-plugandpaint.html http://doc.qt.nokia.com/latest/tools-plugandpaint.html

As you mentioned you are using wxWidgets, this should be the equivalent functionality taking care of the multiple platforms: 如您所提到的,您正在使用wxWidgets,这应该是照顾多个平台的等效功能:

http://docs.wxwidgets.org/2.8/wx_wxdynamiclibrary.html and a full example: http://wiki.wxwidgets.org/Programs_That_Support_Plugins http://docs.wxwidgets.org/2.8/wx_wxdynamiclibrary.html和完整示例: http : //wiki.wxwidgets.org/Programs_That_Support_Plugins

是您可以在互联网上找到的最佳线索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM