简体   繁体   中英

Plugin system for text editor writen in C++

I am creating a cross platform text editor in 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.

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).

Each plugin would inherit from this interface and you build this class eg: SpanishTranslateTransformPlugin in a dynamic library (.so file).

From the program you use dlopen to open the library (see here for a C++ example). 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.

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

That way you will get a TextTransformPlugin object which is the plugin. 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. There is similar functionality in the LoadLibrary API, but you would need to abstract the platforms yourself.

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. Here is an example of a pluggable paint application:

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:

http://docs.wxwidgets.org/2.8/wx_wxdynamiclibrary.html and a full example: http://wiki.wxwidgets.org/Programs_That_Support_Plugins

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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