基于Qt的函数对象(泛化仿函数)设计,可在线程中运行并通过信号返回QVariant

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


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值