ian.IBasicModule/1.0"
Q_DECLARE_INTERFACE(IBasicModule, IBasicModule_iid)
QT_END_NAMESPACE
//Q_DECLARE_INTERFACE(IBasicModule,"com.twsz.tc.ningjian.IBasicModule/1.0");
#endif // BASIC_MODULE_INTERFACE_H
基本是纯虚类要在末尾添加 ?
/******************************************************************************************************
* Copyright (C) 2014, All right reserved.
* file
* version? 1.0
* author? NingJian (freegodly@gmail.com)
* brief
* detail
* TODO
* history? 2014-9-17 created by NingJian
*
* note
******************************************************************************************************/
#ifndef STT_BASIC_MOUDLE_H
#define STT_BASIC_MOUDLE_H
#include
#include
#include
#include
#include
#include
using namespace std;
class? STT_Basic_Moudle:public QObject,public IBasicModule
{
? ? Q_OBJECT
? ? Q_PLUGIN_METADATA(IID "com.twsz.tc.ningjian.IBasicModule/1.0" )
? ? Q_INTERFACES(IBasicModule)
public:
? ? STT_Basic_Moudle();
? ? // IBasicModule interface
public:
? ? bool init(std::map &test_info, std::map &test_fun_info, std::map &moudles_config, RUN_FUN run_fun) const;
? ? bool release() const;
? ? bool initiation(int test_id) const;
? ? bool finish(int test_id) const;
? ? std::string get_moudle_version() const;
? ? std::string get_moudle_describe() const;
? ? void reg_fun(int test_id, QScriptEngine *eng) const;
? ? void reg_ui_fun(REG_UI_FUN reg_ui_f) const;
? ? std::string get_moudle_name() const;
?
public:
? ? ///
? ? /// \brief G_Test_Info
? ? ///
? ? static std::map *STT_G_Test_Info;
? ? ///
? ? /// \brief G_Test_Fun_Info
? ? ///
? ? static std::map? *STT_G_Test_Fun_Info;
? ? ///
? ? /// \brief G_Test_Run_Fun
? ? ///
? ? static RUN_FUN STT_G_Test_Run_Fun;
? ? ///
? ? /// \brief STT_G_Moudles_Config
? ? ///
? ? static std::map? *STT_G_Moudles_Config;
};
std::string get_stt_variable(int test_id,std::string key);
void set_stt_variable(int test_id,std::string key,std::string value);
void add_fun(const char * moudle_name,const char * fun_name ,const char * fun_describe);
#endif // STT_BASIC_MOUDLE_H
3、使用插件
下面是遍历加载plugins目录下所有实现 IBasicModule 接口的插件 获取相应的实例就可以调用了
头文件记得添加
#include
//注册模块指令
? ? QDir plugindir = QDir(QDir::currentPath()+"/plugins");
? ? int i = 0;
? ? foreach(QString filename,plugindir.entryList(QDir::Files)){
? ? ? ? QPluginLoader loader(plugindir.absoluteFilePath(filename));
? ? ? ? if (IBasicModule * base_moudle = qobject_cast(loader.instance()))
? ? ? ? {
? ? ? ? ? ? qDebug()<get_moudle_name().c_str();
? ? ? ? ? ? STT_Global::basicModule_map.insert(std::pair(base_moudle->get_moudle_name(),base_moudle));
? ? ? ? ? ? STT_Global::PlugsList.push_back( base_moudle->get_moudle_name());
? ? ? ? ? ? base_moudle->init(G_Test_Info,G_Test_Fun_Info,G_Moudles_Config,G_STT_Run_Fun);
? ? ? ? ? ? base_moudle->reg_fun(-1,G_STT_Interpreter[-1]);
? ? ? ? ? ? base_moudle->reg_ui_fun(G_Reg_UI_FUN);
? ? ? ? ? ? i++;
? ? ? ? ? ? emit STT_Global::fl->signal_process(20+ 80 * i / plugindir.entryList(QDir::Files).size());
? ? ? ? }
? ? }