summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qbasicfuturewatcher.cpp
blob: 2602995e8facb21e8bf2f1916632d4fda1246ec6 (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
// Copyright (C) 2023 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

#include "qbasicfuturewatcher.h"
#include "qcoreapplication.h"
#include "qfutureinterface.h"
#include "qfutureinterface_p.h"

#include <QtCore/private/qobject_p.h>

QT_BEGIN_NAMESPACE

namespace QtPrivate {

class QBasicFutureWatcherPrivate : public QObjectPrivate, QFutureCallOutInterface
{
public:
    Q_DECLARE_PUBLIC(QBasicFutureWatcher)

    QFutureInterfaceBase future;

    void postCallOutEvent(const QFutureCallOutEvent &event) override;
    void callOutInterfaceDisconnected() override;
};

void QBasicFutureWatcherPrivate::postCallOutEvent(const QFutureCallOutEvent &event)
{
    Q_Q(QBasicFutureWatcher);
    if (q->thread() == QThread::currentThread()) {
        // If we are in the same thread, don't queue up anything.
        std::unique_ptr<QFutureCallOutEvent> clonedEvent(event.clone());
        QCoreApplication::sendEvent(q, clonedEvent.get());
    } else {
        QCoreApplication::postEvent(q, event.clone());
    }
}

void QBasicFutureWatcherPrivate::callOutInterfaceDisconnected()
{
    Q_Q(QBasicFutureWatcher);
    QCoreApplication::removePostedEvents(q, QEvent::FutureCallOut);
}

/*
 * QBasicFutureWatcher is a more lightweight version of QFutureWatcher for internal use
 */
QBasicFutureWatcher::QBasicFutureWatcher(QObject *parent)
    : QObject(*new QBasicFutureWatcherPrivate, parent)
{
}

QBasicFutureWatcher::~QBasicFutureWatcher()
{
    Q_D(QBasicFutureWatcher);
    d->future.d->disconnectOutputInterface(d);
}

void QBasicFutureWatcher::setFuture(QFutureInterfaceBase &fi)
{
    Q_D(QBasicFutureWatcher);
    d->future = fi;
    d->future.d->connectOutputInterface(d);
}

bool QtPrivate::QBasicFutureWatcher::event(QEvent *event)
{
    if (event->type() == QEvent::FutureCallOut) {
        QFutureCallOutEvent *callOutEvent = static_cast<QFutureCallOutEvent *>(event);
        if (callOutEvent->callOutType == QFutureCallOutEvent::Finished)
            emit finished();
        return true;
    }
    return QObject::event(event);
}

} // namespace QtPrivate

QT_END_NAMESPACE

#include "moc_qbasicfuturewatcher.cpp"