开发者

Plugin system for text editor writen in C++

开发者 https://www.devze.com 2023-04-12 14:36 出处:网络
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 pl

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 matt开发者_运维百科ers, 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


Here is a best thread you will find on internet

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号