修改QtConcurrent::run支持任意参数

原QtConcurrent::run最多只支持传入5个参数,目标为实现任意数量的参数;

不支持传入带&的类型(如:int &xx, class& ...),这情况只能使用指针。

void __task_creator( MainWindow* _this, QString * srcDirPath,QString * m_dstPath

, QFileInfoList *fileList, QThreadPool* m_threadPool

, bool *m_stop, bool *m_pause, bool *m_detailed,

QMutex *m_mutex, QWaitCondition *m_pauseCondition

);

QtConcurrent::run( __task_1, 10 );

QtConcurrent::run( __task_2, 10 );

// 为每个文件创建复制任务

QtConcurrent::run( __task_creator, this

, &m_srcPath, &m_dstPath

, &fileList, &m_threadPool

, &m_stop, &m_pause, &m_detailed

, &m_mutex, &m_pauseCondition );

cpp 复制代码
#ifndef MY_QTCONCURRENT_H
#define MY_QTCONCURRENT_H

//---------------------------------
/// 新建头文件中加入以下代码即可
/// 使用方法不变
/// 不支持传入带&的类型(如:int &xx, class& ...),这情况只能使用指针
#include <QtConcurrent/QtConcurrent>
namespace QtConcurrent {

// 为C++11提供自定义的index_sequence实现
#if __cplusplus >= 201402L
// C++14及以上使用标准库
namespace detail {
    using std::index_sequence;
    using std::make_index_sequence;
}
#else
// C++11自定义实现
namespace detail {
    template<std::size_t... Is> struct index_sequence {};

    template<std::size_t N, std::size_t... Is>
    struct make_index_sequence_impl : make_index_sequence_impl<N-1, N-1, Is...> {};

    template<std::size_t... Is>
    struct make_index_sequence_impl<0, Is...> : index_sequence<Is...> {};

    template<std::size_t N>
    using make_index_sequence = make_index_sequence_impl<N>;
}
#endif

// 统一的模板实现
template <typename T, typename FunctionPointer, typename... FuncArgs>
struct _StoredFunctorCall : public QtConcurrent::RunFunctionTask<T> {
    inline _StoredFunctorCall(FunctionPointer _function, FuncArgs&&... args)
        : function(_function), arguments_(std::forward<FuncArgs>(args)...) {}

    void runFunctor() override {
        // 在C++11中,我们需要用函数重载或模板特化来替代if constexpr
        runFunctorImpl(typename std::is_void<T>::type());
    }

private:
    // 辅助函数:实现参数包展开
    template<typename F, typename Tuple, std::size_t... I>
    auto apply_impl(F&& f, Tuple&& t, detail::index_sequence<I...>)
    -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)) {
        return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
    }

    // 主函数:tuple应用函数
    template<typename F, typename Tuple>
    auto tuple_apply(F&& f, Tuple&& t)
    -> decltype(apply_impl(
        std::forward<F>(f),
        std::forward<Tuple>(t),
        detail::make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>()
    )) {
        return apply_impl(
            std::forward<F>(f),
            std::forward<Tuple>(t),
            detail::make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>()
        );
    }

    // void返回类型的处理
    void runFunctorImpl(std::true_type) { // std::true_type 表示 void
        // qDebug()<<"std::true_type 表示 void";
        tuple_apply(function, arguments_);
    }

    // 非void返回类型的处理
    void runFunctorImpl(std::false_type) { // std::false_type 表示 非void
        // qDebug()<<"std::false_type 表示 非void";
        this->result = tuple_apply(function, arguments_);
    }

public:
    FunctionPointer function;
    std::tuple<typename std::decay<FuncArgs>::type...> arguments_;
};

// 简化的run函数模板
template <typename T, typename... Args>
QFuture<T> run( T (*functionPointer)(Args...), Args&&... args) {
    return (new _StoredFunctorCall<T, T(*)(Args...), Args...>
            (functionPointer, std::forward<Args>(args)...))->start();
}

}; // namespace QtConcurrent
//---------------------------------

#endif // MY_QTCONCURRENT_H
相关推荐
CHANG_THE_WORLD2 小时前
编写 CMakeLists查找库的findxxx.cmake文件
c++
lkbhua莱克瓦242 小时前
IO流——打印流
java·开发语言·前端·学习方法
赵得C2 小时前
软件设计师前沿考点精讲:新兴技术与性能优化实战
java·开发语言·分布式·算法·设计模式·性能优化
汉克老师2 小时前
2023年海淀区中小学信息学竞赛复赛(小学组试题第七题 赛车游戏(car))
c++·游戏·双指针·海淀区中小学信息竞赛
缘三水2 小时前
【C语言】17.字符函数和字符串函数
c语言·开发语言·语法
MediaTea2 小时前
Python 的设计哲学P08:可读性与人类语言
开发语言·python
qq_251533592 小时前
如何使用 Python 正则表达式去除空格/制表符/换行符?
开发语言·python·正则表达式
Azxcc02 小时前
c++ core guidelines解析--让接口易于使用
开发语言·c++
亭上秋和景清2 小时前
指针进阶: 回调函数
开发语言·前端·javascript