基于Qt的函数对象(泛化仿函数)设计,可在线程中运行并通过信号返回QVariant
2016-08-04 18:52
513 查看
//复制以下代码,放到一个.h文件中,即可应用。
//qxfunctionobject.h
#ifndef
FUNCTION_OBJECT_H
#include "qxfunctionobject.h"
//qxfunctionobject.h
#ifndef
FUNCTION_OBJECT_H
#define FUNCTION_OBJECT_H
#include <QObject>
#include <QVariant>
#include <QThread>
#include <QSemaphore>
class QxSemaphorer
{
QSemaphore* m_sem;
public:
QxSemaphorer(QSemaphore* sem):m_sem(sem){if(m_sem)m_sem->acquire();}
~QxSemaphorer(){if(m_sem)m_sem->release();}
};
class QxFunctionObject:public QObject
{
Q_OBJECT
public:
inline ~QxFunctionObject();
inline void runInThread(QObject *parent = nullptr);
inline void setSemaphore(QSemaphore* sem = nullptr);
inline void setAutoDelete(bool b);
inline bool autoDelete();
inline bool isRunning();
virtual QVariant run() = 0;
template <typename Functor, typename Arg1>
static QxFunctionObject* create(Functor functor, Arg1 arg1);
template <typename Functor, typename Arg1, typename Arg2>
static QxFunctionObject* create(Functor functor, Arg1 arg1, Arg2 arg2);
template <typename Functor, typename Arg1, typename Arg2, typename Arg3>
static QxFunctionObject* create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3);
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4>
static QxFunctionObject* create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4);
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4,typename Arg5>
static QxFunctionObject* create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5);
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4,typename Arg5,typename Arg6>
static QxFunctionObject* create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6);
private slots:
inline void function_run();
signals:
void returnValue(QVariant msg);
protected:
QxFunctionObject():m_sem(nullptr),m_thread(nullptr),m_autoDelete(true){}
private:
QSemaphore* m_sem;
QThread* m_thread;
bool m_autoDelete;
};
QxFunctionObject::~QxFunctionObject()
{
if(m_thread)
m_thread->deleteLater();
}
void QxFunctionObject::runInThread(QObject *parent)
{
if(m_thread == nullptr){
m_thread = new QThread(parent);
this->moveToThread(m_thread);
connect(m_thread,SIGNAL(started()),SLOT(function_run()));
} else if(parent != nullptr){
m_thread->setParent(parent);
}
m_thread->start();
}
void QxFunctionObject::setSemaphore(QSemaphore *sem)
{
m_sem = sem;
}
void QxFunctionObject::setAutoDelete(bool b)
{
m_autoDelete = b;
}
bool QxFunctionObject::autoDelete()
{
return m_autoDelete;
}
bool QxFunctionObject::isRunning()
{
if(m_thread)
return m_thread->isRunning();
return false;
}
void QxFunctionObject::function_run()
{
QxSemaphorer semer(m_sem);
emit returnValue(run());;
if(m_autoDelete)
this->deleteLater();
}
template <typename Functor,typename Arg1>
class __FunctionObjectPrivate_1:public QxFunctionObject
{
Functor function;Arg1 arg1;
QVariant run(){
return QVariant::fromValue(function(arg1));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_1(Functor function,Arg1 arg1):function(function),arg1(arg1){}
};
template <typename Functor,typename Arg1,typename Arg2>
class __FunctionObjectPrivate_2:public QxFunctionObject
{
Functor function;Arg1 arg1;Arg2 arg2;
QVariant run(){
return QVariant::fromValue(function(arg1,arg2));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_2(Functor function,Arg1 arg1,Arg2 arg2):function(function),arg1(arg1),arg2(arg2){}
};
template <typename Functor,typename Arg1,typename Arg2,typename Arg3>
class __FunctionObjectPrivate_3:public QxFunctionObject
{
Functor function;Arg1 arg1;Arg2 arg2;Arg3 arg3;
QVariant run(){
return QVariant::fromValue(function(arg1,arg2,arg3));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_3(Functor function,Arg1 arg1,Arg2 arg2,Arg3 arg3):function(function),arg1(arg1),arg2(arg2),arg3(arg3){}
};
template <typename Functor,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
class __FunctionObjectPrivate_4:public QxFunctionObject
{
Functor function;Arg1 arg1;Arg2 arg2;Arg3 arg3;Arg4 arg4;
QVariant run(){
return QVariant::fromValue(function(arg1,arg2,arg3,arg4));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_4(Functor function,Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4):function(function),arg1(arg1),arg2(arg2),arg3(arg3),arg4(arg4){}
};
template <typename Functor,typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5>
class __FunctionObjectPrivate_5:public QxFunctionObject
{
Functor function;Arg1 arg1;Arg2 arg2;Arg3 arg3;Arg4 arg4;Arg5 arg5;
QVariant run(){
return QVariant::fromValue(function(arg1,arg2,arg3,arg4,arg5));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_5(Functor function,Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4, Arg5 arg5):function(function),arg1(arg1),arg2(arg2),arg3(arg3),arg4(arg4),arg5(arg5){}
};
template <typename Functor,typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5,typename Arg6>
class __FunctionObjectPrivate_6:public QxFunctionObject
{
Functor function;Arg1 arg1;Arg2 arg2;Arg3 arg3;Arg4 arg4;Arg5 arg5;Arg6 arg6;
QVariant run(){
return QVariant::fromValue(function(arg1,arg2,arg3,arg4,arg5,arg6));
}
friend class QxFunctionObject;
__FunctionObjectPrivate_6(Functor function,Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4, Arg5 arg5, Arg6 arg6):function(function),arg1(arg1),arg2(arg2),arg3(arg3),arg4(arg4),arg5(arg5),arg6(arg6){}
};
template <typename Functor, typename Arg1>
QxFunctionObject *QxFunctionObject::create(Functor functor,Arg1 arg1)
{
return new __FunctionObjectPrivate_1<Functor,Arg1>(functor,arg1);
}
template <typename Functor, typename Arg1, typename Arg2>
QxFunctionObject* QxFunctionObject::create(Functor functor, Arg1 arg1, Arg2 arg2)
{
return new __FunctionObjectPrivate_2<Functor,Arg1,Arg2>(functor,arg1,arg2);
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3>
QxFunctionObject* QxFunctionObject::create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3)
{
return new __FunctionObjectPrivate_3<Functor,Arg1,Arg2,Arg3>(functor,arg1,arg2,arg3);
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4>
QxFunctionObject* QxFunctionObject::create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
{
return new __FunctionObjectPrivate_4<Functor,Arg1,Arg2,Arg3,Arg4>(functor,arg1,arg2,arg3,arg4);
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4,typename Arg5>
QxFunctionObject* QxFunctionObject::create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
{
return new __FunctionObjectPrivate_5<Functor,Arg1,Arg2,Arg3,Arg4,Arg5>(functor,arg1,arg2,arg3,arg4,arg5);
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3,typename Arg4,typename Arg5,typename Arg6>
QxFunctionObject* QxFunctionObject::create(Functor functor, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
{
return new __FunctionObjectPrivate_6<Functor,Arg1,Arg2,Arg3,Arg4,Arg5,Arg6>(functor,arg1,arg2,arg3,arg4,arg5,arg6);
}
#endif // FUNCTION_OBJECT_H //用例:
#include "qxfunctionobject.h"
int add(int a,int b){
return a + b;
}
int main(int argc, char *argv[])
{
QxFunctionObject* obj = QxFunctionObject::create(::add,100,200);
obj->run();
obj->runInThread();
}
相关文章推荐
- 详解 Qt 线程间共享数据(使用signal/slot传递数据,线程间传递信号会立刻返回,但也可通过connect改变)
- 基于linux下的QT设计和移植到开发运行
- 基于无线传感器网络的电机运行状态监测系统设计
- 基于信号完整性分析的高速数字PCB 的设计方法
- 基于单片机的交通灯信号控制器设计
- 基于 QT 和 mplayer 的 MP4播放器设计
- 基于 QtWebkit 的嵌入式 bs 架构设计
- 利用XML进行GUI设计与运行时显示------基于GTK+/Gtkmm的Glade/libglade/libglademm
- 基于Qt的嵌入式媒体播放器系统的设计
- 基于QT C++的网络缓冲区设计(一)——概括
- 操作系统课程设计--基于线程的多任务系统的实现
- ManualResetEvent 允许线程通过发信号互相通信
- 嵌入式Linux中基于 Qt/Embeded触摸屏驱动的设计
- 基于单片机的信号发生器设计
- 基于Qt/Embedded和Qtopia的GUI设计(转)
- 通过设置进程或线程的亲缘性(affinity),使进程或线程在指定的CPU(核)上运行
- 基于PXA255和嵌入式QT的IP视频电话设计
- 基于 TPS54310的雷达视频信号模拟器的电源设计
- 基于QtWebkit的嵌入式bs架构设计
- 写程序 控制cpu占用率-4 绘制CPU使用率的正弦曲线2 通过C#指定线程运行的 CPU