插件API设计 [英] Plugin API design

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

问题描述

所以我有一个基于 QT API 的应用程序,它使用 QPlugin 系统.使用起来相当简单,您定义一个继承自接口的类,当插件加载时,您将获得该类的实例.最后它会归结为 dlopen/dlsymLoadLibrary/GetProcAddress,任何适合于操作系统.我在这里没有任何问题,一切都按预期进行.

那么,进入正题.有很多功能涉及需要引用主应用程序提供的数据/功能的插件.例如,我的应用程序有一个 GUI,所以我的应用程序中有一个plugin::v1::gui"函数,它返回一个 QWidget *.如果我希望插件能够向我的 UI 中添加内容,或者甚至将它的对话框设为我的 UI 的子级,则需要一个指向它的指针.

我开始在 Linux 上进行开发,很快就遇到了这样一个事实:默认情况下,加载程序不会使用加载它的应用程序中的符号填充共享对象中未解析的符号.没问题,轻松解决.将-rdynamic"添加到我的标志并继续.一切顺利.

现在我发现在 Windows 上似乎没有等价物 :(.那么什么是好的解决方案?

到目前为止,我想出的最好的方法是在我的主应用程序中填充一个结构,该结构具有指向插件可能关心的每个对象/函数的指针.然后将其传递给插件的init()"函数,现在它具有指向所有内容的正确指针,但这是一个烦人的解决方案,因为现在每当我添加内容时,我都必须在多个地方进行更改.>

有更好的解决方案吗?SO 社区是如何处理这个问题的?

解决方案

为应用程序公开的主要交互对象创建一组接口,并在它们自己的 lib/dll 中构建它们,并在其上实现这些接口应用程序中的类.该库还应该包含一个插件接口,其中可能只有一个插件对象将实现的初始化"方法.

应用程序和插件显然将链接到该 DLL,一旦应用程序加载插件,应用程序应调用插件的 initialize 方法,该方法将具有接受控制应用程序所需的任何对象的参数.

简化此操作的一种流行方法是在您的应用程序中实现类似于 DOM 的层次结构,以便插件可以从一个根对象访问您应用程序中的所有相关对象.

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.

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.

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.

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

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.

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

解决方案

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.

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.

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天全站免登陆