aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/QtTaskTree/qconcurrentcalltask.h
blob: 62232a5e7f249b870e74a1753b182ba939375202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2025 Jarek Kobus
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QCONCURRENTCALLTASK_H
#define QCONCURRENTCALLTASK_H

#include <QtTaskTree/qtasktree.h>

#include <QtConcurrent/QtConcurrent>

QT_REQUIRE_CONFIG(concurrent);

QT_BEGIN_NAMESPACE

class QConcurrentCallBasePrivate;

class Q_TASKTREE_EXPORT QConcurrentCallBase : public QObject
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(QConcurrentCallBase)

public:
    QConcurrentCallBase(QObject *parent = nullptr);
    ~QConcurrentCallBase() override;

    void setThreadPool(QThreadPool *pool);
    QThreadPool *threadPool() const;

    void setAutoDelayedSync(bool on);
    bool isAutoDelayedSync() const;

    void setSyncSkipped(bool on);
    bool isSyncSkipped() const;

    static void syncAll();

Q_SIGNALS:
    void started();
    void done(QtTaskTree::DoneResult result);
    void resultReadyAt(int index);
    void resultsReadyAt(int beginIndex, int endIndex);
    void progressRangeChanged(int minimum, int maximum);
    void progressValueChanged(int value);
    void progressTextChanged(const QString &text);

protected:
    void storeFuture(const QFuture<void> &future);
};

template <typename ResultType>
class QConcurrentCall : public QConcurrentCallBase
{
    Q_DISABLE_COPY_MOVE(QConcurrentCall)

public:
    explicit QConcurrentCall(QObject *parent = nullptr)
        : QConcurrentCallBase(parent)
    {
        connect(&m_watcher, &QFutureWatcherBase::finished, this, [this] {
            Q_EMIT done(QtTaskTree::toDoneResult(!m_watcher.isCanceled()));
        });
        connect(&m_watcher, &QFutureWatcherBase::resultReadyAt,
                this, &QConcurrentCallBase::resultReadyAt);
        connect(&m_watcher, &QFutureWatcherBase::resultsReadyAt,
                this, &QConcurrentCallBase::resultsReadyAt);
        connect(&m_watcher, &QFutureWatcherBase::progressValueChanged,
                this, &QConcurrentCallBase::progressValueChanged);
        connect(&m_watcher, &QFutureWatcherBase::progressRangeChanged,
                this, &QConcurrentCallBase::progressRangeChanged);
        connect(&m_watcher, &QFutureWatcherBase::progressTextChanged,
                this, &QConcurrentCallBase::progressTextChanged);
    }

    ~QConcurrentCall() override
    {
        disconnect(&m_watcher);
    }

    template <typename Function, typename ...Args>
    void setConcurrentCallData(Function &&function, Args &&...args)
    {
        wrapConcurrent(std::forward<Function>(function), std::forward<Args>(args)...);
    }

    bool isDone() const { return m_watcher.isFinished(); }
    bool isResultAvailable() const { return future().resultCount(); }

    QFuture<ResultType> future() const { return m_watcher.future(); }
    ResultType result() const { return m_watcher.result(); }
    ResultType takeResult() const { return future().takeResult(); }
    ResultType resultAt(int index) const { return m_watcher.resultAt(index); }
    QList<ResultType> results() const { return future().results(); }

    void start()
    {
        if (future().isRunning()) {
            qWarning("QConcurrentCall: Can't start, the future is still running.");
            return;
        }

        if (!m_startHandler) {
            qWarning("QConcurrentCall: No start handler specified.");
            Q_EMIT done(QtTaskTree::DoneResult::Error);
            return;
        }

        m_watcher.setFuture(m_startHandler());
        storeFuture(QFuture<void>(future()));
        Q_EMIT started();
    }

#ifdef Q_QDOC
    void setThreadPool(QThreadPool *pool);
    QThreadPool *threadPool() const;

    void setAutoDelayedSync(bool on);
    bool isAutoDelayedSync() const;

Q_SIGNALS:
    void started();
    void done(QtTaskTree::DoneResult result);
    void resultReadyAt(int index);
    void resultsReadyAt(int beginIndex, int endIndex);
    void progressRangeChanged(int min, int max);
    void progressValueChanged(int value);
    void progressTextChanged(const QString &text);
#endif

private:
    template <typename Function, typename ...Args>
    void wrapConcurrent(Function &&function, Args &&...args)
    {
        m_startHandler = [this, function = std::forward<Function>(function), args...] {
            QThreadPool *pool = threadPool() ? threadPool() : QThreadPool::globalInstance();
            return QtConcurrent::run(pool, function, args...);
        };
    }

    template <typename Function, typename ...Args>
    void wrapConcurrent(std::reference_wrapper<const Function> &&wrapper, Args &&...args)
    {
        m_startHandler = [this, wrapper = std::forward<std::reference_wrapper<const Function>>(wrapper), args...] {
            QThreadPool *pool = threadPool() ? threadPool() : QThreadPool::globalInstance();
            return QtConcurrent::run(pool, std::forward<const Function>(wrapper.get()),
                                     args...);
        };
    }

    std::function<QFuture<ResultType>()> m_startHandler;
    QFutureWatcher<ResultType> m_watcher;
};

template <typename ResultType>
using QConcurrentCallTask = QCustomTask<QConcurrentCall<ResultType>>;

QT_END_NAMESPACE

#endif // QCONCURRENTCALLTASK_H