Changeset View
Changeset View
Standalone View
Standalone View
src/pendingvalue.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com> | ||||
3 | * | ||||
4 | * SPDX-License-Identifier: LGPL-2.0-or-later | ||||
5 | */ | ||||
6 | | ||||
7 | #include "pendingvalue.h" | ||||
8 | | ||||
9 | #include <QEventLoop> | ||||
10 | | ||||
11 | PendingValue::PendingValue(QObject *parent) : QObject(parent) {} | ||||
12 | | ||||
13 | QVariant PendingValue::await() | ||||
14 | { | ||||
15 | auto value = m_future.result(); | ||||
16 | m_value = value; | ||||
17 | m_isReady = true; | ||||
18 | emit ready(); | ||||
19 | return value; | ||||
20 | } | ||||
21 | | ||||
22 | QFuture<QVariant> PendingValue::future() | ||||
23 | { | ||||
24 | return m_future; | ||||
25 | } | ||||
26 | | ||||
27 | void PendingValue::setFuture(QFuture<QVariant> value) | ||||
28 | { | ||||
29 | m_futureWatcher = new QFutureWatcher<QVariant>(this); | ||||
30 | m_future = value; | ||||
31 | m_futureWatcher->setFuture(m_future); | ||||
32 | connect(m_futureWatcher, &QFutureWatcher<QVariant>::finished, [=]() { | ||||
33 | m_value = m_future.result(); | ||||
34 | m_isReady = true; | ||||
35 | emit ready(); | ||||
36 | }); | ||||
37 | } | ||||
38 | | ||||
39 | QVariant PendingValue::value() | ||||
40 | { | ||||
41 | return m_value; | ||||
42 | } | ||||
43 | | ||||
44 | bool PendingValue::isReady() | ||||
45 | { | ||||
46 | return m_isReady; | ||||
47 | } | ||||
48 | No newline at end of file |