插件API设计 [英] Plugin API design

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

问题描述

所以我有一个在很大程度上基于其使用QPlugin系统的QT API的应用程序。这是相当简单的使用,您可以定义从一个接口继承,当插件加载你得到的类的实例的类。最终它会归结为一个的dlopen / 则dlsym 调用LoadLibrary / GetProcAddress的,无论是适当的操作系统。我这里没有问题一切正常。

So I have an application that is based heavily on the QT API which using the QPlugin system. It's fairly simple to use, you define a class which inherit from an Interface and when the plugin is loaded you get an instance of that class. In the end it'll boil down to a dlopen/dlsym or LoadLibrary/GetProcAddress, whatever is appropriate for the OS. I have no problems here everything works as expected.

所以,到这个问题。有一个的很多的的,涉及一个插件需要提及由主应用程序提供的数据/功能的功能。例如,我的应用程序有一个GUI,所以我在我的应用程序中的插件:: V1的::鬼函数返回一个 QWidget的* 。如果我想要一个插件能够添加东西到我的UI,甚至使它的对话框我的UI的孩子,它会需要一个指针。

So, onto the issue. There is a lot of functionality that involves a plugin needing to refer to data/functions provided by the main application. For example, my application has a GUI, so I have in my application a "plugin::v1::gui" function which returns a QWidget *. If I want a plugin to be able to add things to my UI, or even make it's dialog a child of my UI it'll need a pointer to it.

我在Linux上开始发展,很快又遇到了一个事实,即默认加载程序不会在未解决的符号填写在从应用程序加载它的那些共享对象。没问题,容易解决。加上 -rdynamic 我的旗帜,继续前进。事情很好地工作。

I started development on Linux and quickly encountered the fact that by default the loader doesn't fill in unresolved symbols in shared objects with the ones from the application loading it. No problem, easy fix. add "-rdynamic" to my flags and move on. Thing work well.

现在我发现,似乎没有要在Windows :(等效。那么,什么是一个好的解决方案?

Now I've discovered that there doesn't appear to be an equivalent on Windows :(. So what is a good solution?

到目前为止,我想出了是有一个结构,我在有指向每一个对象我的主要应用填补了最佳/功能的插件可能会在意。然后顺便指出,以插件的的init()功能,现在它有适当的指针一切,但因为现在我不得不在多个地方,只要改变它是一个恼人的解决方案我补充一下。

So far the best I've come up with is having a structure I fill up in my main application that has pointers to every object/function a plugin might care about. Then passing that to the plugin's "init()" function and now it has proper pointers to everything, but it's an annoying solution since now I have to make changes in multiple places whenever I add something.

有没有更好的解决方案吗?如何把SO社区处理这个?

Is there a better solution? How has the SO community dealt with this?

推荐答案

为将你的应用程序要公开的主要交互对象来创建一组接口,并在自己的一个lib / DLL建立他们和实现这些接口上类在应用程序适当。该库还应该包括也许只是该插件对象将实行初始化方法的插件接口。

Create a set of interfaces for the main interaction objects that will be exposed by your application and build them in a lib/dll of their own and implement those interfaces on classes in your application as appropriate. The library should also include a plugin interface with perhaps just an "initialize" method that the plugin object will implement.

应用程序和插件会明显针对该DLL链接,一旦插件是由应用​​程序加载的应用程序应该调用里面会有参数,接受控制应用程序所需要的任何对象插件的initialize方法。

The application and plugin will obviously link against that DLL and once the plugin is loaded by the application the application should call the plugin's initialize method which will have parameters to accept any objects needed to control the application.

使得这个简单的一个流行的方式是实现一个层次类似于DOM在你的应用程序,以便插件可以从一个根对象到应用程序中的所有相关的对象。

A popular way of making this simple is to implement a hierarchy similar to the DOM in your application so that the plugin can get to all the relevant objects in your application from one root object.

这篇关于插件API设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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