CTK框架 - 通信 - 插件服务注册和调用
CTK框架 - 插件对外提供接口
接口函数声明
接口就是虚函数(也可以是纯虚函数),也就是最终的服务的前身。
接口对外暴露功能,比如我们给之前写的mainwindow 加一个界面类,并且对外暴露一个popWindow()的接口
定义接口文件:
imainwindow.h
#ifndef IMAINWINDOW_H #define IMAINWINDOW_H #include <QObject> class iMainWindow { public: virtual void popWindow() = 0; }; // 此宏将当前这个接口类声明为接口,后面的一长串就是这个接口的唯一标识。 Q_DECLARE_INTERFACE(iMainWindow, "interface_mainwindow") #endif // IMAINWINDOW_H
我们把接口类提供在插件框架的最外层,方便对第三方提供我们的接口。
接口的实现
插件就是实现对外接口的实现类,所以理论上有多少个实现类,就有多少个插件。
我们需要定义一个mainwindowplugin类来实现刚刚提供的接口类
mainwindowplugin.h
#ifndef CTKTEST_MAINWINDOWPLUGIN_H #define CTKTEST_MAINWINDOWPLUGIN_H #include <QObject> #include "../src/imainwindow.h" #include "ctkPluginContext.h" class MainWindowPlugin : public QObject, public iMainWindow { Q_OBJECT /* 此宏与Q_DECLARE_INTERFACE宏配合使用。 Q_DECLARE_INTERFACE:声明一个接口类 Q_INTERFACES:当一个类继承这个接口类,表明需要实现这个接口类 */ Q_INTERFACES(iMainWindow) public: MainWindowPlugin(ctkPluginContext *context); virtual void popWindow(); private: ctkPluginContext *m_context; }; #endif //CTKTEST_MAINWINDOWPLUGIN_H
mainwindowplugin.cpp
#include "mainwindowplugin.h" #include <iostream> MainWindowPlugin::MainWindowPlugin(ctkPluginContext *context) :m_context(context) { } void MainWindowPlugin::popWindow() { std::cout << "pop window" << std::endl; }
服务注册
我们在上一篇文章中提到了activator激活类,我们在激活类中提供该类的成员指针变量,在start的时候将该类new出来,头文件声明变量的部分不展示代码,这里展示new对象的代码。如果我们这边的类对象不是使用的只能指针则需要在stop的时候释放指针,同时声明的时候不要忘记初始化为空。
void MainWindowActivator::start(ctkPluginContext *context) { std::cout << "core start" << std::endl; plugin_ = new MainWindowPlugin(context); ctkDictionary dic; context->registerService<iMainWindow>(plugin_, dic); } void MainWindowActivator::stop(ctkPluginContext *context) { std::cout << "core stop" << std::endl; if(plugin_) { delete plugin_; plugin_ = nullptr; } }
context->registerService<iMainWindow>(plugin_, dic);我们可以看到这句代码,这句代码是将服务接口对象注册到实现类里面,这样这个接口在使用mainwindowactoivator调用的时候调用的就是plugin_对象里面的方法。
接口调用
接下来我们可以在main中调用这个代码,来测试我们的接口是否可以正常调用。在插件加载完成之后调用插件的服务,将下面代码插入正确的位置,我这里是在代码最后插入该段代码即可。
ctkPluginContext* context = framework->getPluginContext(); ctkServiceReference ref =context->getServiceReference<iMainWindow>(); iMainWindow* mainWindow{nullptr}; if(ref) mainWindow = context->getService<iMainWindow>(ref); if(mainWindow) mainWindow->popWindow();
网上说是有两种重载接口的方式,我这里只使用了第一种,代码看起来并不复杂,所以也不过多介绍。只给贴出来来
1、iMainWindow* ret = context->getService<iMainWindow>(reference); 2、iMainWindow* ret = qobject_cast<iMainWindow*>(context->getService(reference));
启动代码,如何可以正常打印pop window 则说明接口调用成功。
按照别人的写法,我这边也说说服务和接口的关系,接口其实就是插件对象。服务就是提供的接口类,我们使用注册器去注册服务,然后使用插件对象去实现对应的服务,调用的时候获取服务,之后按照虚基类提供的方法去调用即可调用到对应插件的服务。
接口,插件,服务的关系
- 一对一: 一个服务对应着一个插件类,就像我们上面写的一样,这边不做赘述。
- 多对一:一个插件类可以继承自两个实现类,输出两个服务,我们可以修改代码如下:
1.提供imainwindow2虚基类,然后将插件多继承一下该虚基类
#ifndef CTKTEST_IMAINWINDOW2_H #define CTKTEST_IMAINWINDOW2_H #include <QObject> class iMainWindow2 { public: virtual void popWindow2() = 0; }; //此宏将当前这个接口类声明为接口,后面的一长串就是这个接口的唯一标识。 Q_DECLARE_INTERFACE(iMainWindow2, "interface_mainwindow2") #endif //CTKTEST_IMAINWINDOW2_H
2.修改plugin如下,同时实现虚基类的接口函数
#ifndef CTKTEST_MAINWINDOWPLUGIN_H #define CTKTEST_MAINWINDOWPLUGIN_H #include <QObject> #include "../src/imainwindow.h" #include "../src/imainwindow2.h" #include "ctkPluginContext.h" class MainWindowPlugin : public QObject, public iMainWindow, public iMainWindow2 { Q_OBJECT /* 此宏与Q_DECLARE_INTERFACE宏配合使用。 Q_DECLARE_INTERFACE:声明一个接口类 Q_INTERFACES:当一个类继承这个接口类,表明需要实现这个接口类 */ Q_INTERFACES(iMainWindow iMainWindow2) public: MainWindowPlugin(ctkPluginContext *context); void popWindow() override; void popWindow2() override; private: ctkPluginContext *m_context; }; #endif //CTKTEST_MAINWINDOWPLUGIN_H void MainWindowPlugin::popWindow2() { std::cout << "pop window2" << std::endl; }
3.在激活类中注册iMainWindow2接口
void MainWindowActivator::start(ctkPluginContext *context) { std::cout << "core start" << std::endl; plugin_ = new MainWindowPlugin(context); ctkDictionary dic; context->registerService<iMainWindow>(plugin_, dic); context->registerService<iMainWindow2>(plugin_, dic); }
4.在调用的地方添加对应的调用
iMainWindow2* mainWindow2{nullptr}; ctkServiceReference ref2 =context->getServiceReference<iMainWindow2>(); if(ref2) { mainWindow2 = context->getService<iMainWindow2>(ref2); } if(mainWindow2) { mainWindow2->popWindow2(); }
最后发现可以正常输出pop window 和 pop window2。
- 一对多:这里的一对多是指的,一个dll中提供了两个plugin对象
1.实现mainwindowplugin2类
#ifndef CTKTEST_MAINWINDOWPLUGIN2_H #define CTKTEST_MAINWINDOWPLUGIN2_H #include <QObject> #include "../src/imainwindow.h" #include "ctkPluginContext.h" class MainwindowPlugin2 : public QObject, public iMainWindow { Q_OBJECT /* 此宏与Q_DECLARE_INTERFACE宏配合使用。 Q_DECLARE_INTERFACE:声明一个接口类 Q_INTERFACES:当一个类继承这个接口类,表明需要实现这个接口类 */ Q_INTERFACES(iMainWindow) public: MainwindowPlugin2(ctkPluginContext *context); void popWindow() override; private: ctkPluginContext *m_context; }; #endif //CTKTEST_MAINWINDOWPLUGIN2_H #include "mainwindowplugin2.h" #include <iostream> MainwindowPlugin2::MainwindowPlugin2(ctkPluginContext *context) :m_context(context) { } void MainwindowPlugin2::popWindow() { std::cout << "pop window2" << std::endl; }
2.将两个插件都注册到激活类中,只不过这个时候我们注册的时候需要对dic做一点调整,同时也不要忘记释放,代码如下:
void MainWindowActivator::start(ctkPluginContext *context) { std::cout << "core start" << std::endl; plugin_ = new MainWindowPlugin(context); plugin2_ = new MainwindowPlugin2(context); ctkDictionary dic; dic.insert(ctkPluginConstants::SERVICE_RANKING, 1); dic.insert("name", "CTK"); context->registerService<iMainWindow>(plugin_, dic); ctkDictionary dic2; dic2.insert(ctkPluginConstants::SERVICE_RANKING, 2); dic2.insert("name", "QT"); context->registerService<iMainWindow>(plugin2_, dic2); } void MainWindowActivator::stop(ctkPluginContext *context) { std::cout << "core stop" << std::endl; if(plugin_) { delete plugin_; plugin_ = nullptr; } if(plugin2_) { delete plugin2_; plugin2_ = nullptr; } }
4.获取服务并且调用服务,这个时候服务对应多个插件,所以不能像之前那样直接获取调用,需要按照下面的方式获取
// 获取所有服务 QList<ctkServiceReference> refs =context->getServiceReferences<iMainWindow>(); for(const auto &ref : refs) { std::cout << "Name:" << ref.getProperty("name").toString().toStdString() << " " << "Service ranking:" << ref.getProperty(ctkPluginConstants::SERVICE_RANKING).toLongLong() << " " << "Service id:" << ref.getProperty(ctkPluginConstants::SERVICE_ID).toLongLong() << std::endl; // WelcomeService* service = qobject_cast<WelcomeService *>(context->getService(ref)); // if (service != Q_NULLPTR) // service->welcome(); iMainWindow *mainWindow = context->getService<iMainWindow>(ref); mainWindow->popWindow(); }
上面的代码可以调用注册的两个接口,并且输出对应的输出。
// 使用过滤表达式,获取需要的服务 QList<ctkServiceReference> refs2 =context->getServiceReferences<iMainWindow>("(&(name=CTK))"); for(const auto &ref : refs2) { std::cout << "Name:" << ref.getProperty("name").toString().toStdString() << " " << "Service ranking:" << ref.getProperty(ctkPluginConstants::SERVICE_RANKING).toLongLong() << " " << "Service id:" << ref.getProperty(ctkPluginConstants::SERVICE_ID).toLongLong() << std::endl; // WelcomeService* service = qobject_cast<WelcomeService *>(context->getService(ref)); // if (service != Q_NULLPTR) // service->welcome(); iMainWindow *mainWindow = context->getService<iMainWindow>(ref); mainWindow->popWindow(); }
上述代码只会输出plugin_的结果,不会输出plugin2_的结果。
// 获取某一个服务 (由 Service Ranking 和 Service ID 决定) ctkServiceReference ref = context->getServiceReference<iMainWindow>(); if (ref) { iMainWindow* service = qobject_cast<iMainWindow *> (context->getService(ref)); if (service != Q_NULLPTR) service->popWindow(); }
上述代码会优先调用ID小的那个。