aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/futuresynchronizer.h
blob: 29b1f5e4563ca3e9f0de012cedda2df170739cb5 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include <QFuture>
#include <QList>
#include <QtGlobal>

namespace Utils {

class QTCREATOR_UTILS_EXPORT FutureSynchronizer final
{
public:
    FutureSynchronizer() = default;
    ~FutureSynchronizer();

    template <typename T>
    void addFuture(const QFuture<T> &future)
    {
        m_futures.append(QFuture<void>(future));
        flushFinishedFutures();
    }

    bool isEmpty() const;

    void waitForFinished();
    void cancelAllFutures();
    void clearFutures();

    void setCancelOnWait(bool enabled);
    // Note: The QFutureSynchronizer contains cancelOnWait(), what suggests action, not a getter.
    bool isCancelOnWait() const;

    void flushFinishedFutures();

private:
    QList<QFuture<void>> m_futures;
    // Note: This default value is different than QFutureSynchronizer's one. True makes more sense.
    bool m_cancelOnWait = true;
};

} // namespace Utils