//复制以下代码,放到一个.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();}