Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpromise.h
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
ifndef
QPROMISE_H
6
#
define
QPROMISE_H
7
8
#
include
<
QtCore
/
qglobal
.
h
>
9
#
include
<
QtCore
/
qfutureinterface
.
h
>
10
11
#
include
<
utility
>
12
13
QT_REQUIRE_CONFIG
(
future
);
14
15
QT_BEGIN_NAMESPACE
16
17
namespace
QtPrivate
{
18
19
template
<
class
T
,
class
U
>
20
using
EnableIfSameOrConvertible
=
std
::
enable_if_t
<
std
::
is_convertible_v
<
T
,
U
>>;
21
22
}
// namespace QtPrivate
23
24
template
<
typename
T>
25
class
QPromise
26
{
27
static_assert
(
std
::is_move_constructible_v<T>
28
||
std
::is_same_v<T,
void
>,
29
"A move-constructible type or type void is required"
);
30
public
:
31
QPromise
() =
default
;
32
Q_DISABLE_COPY
(
QPromise
)
33
QPromise
(
QPromise
<
T
> &&
other
) =
default
;
34
QPromise
(
const
QFutureInterface<T> &other) :
d
(
other
) {}
35
QPromise
(QFutureInterface<T> &&other)
noexcept
:
d
(
std
::
move
(
other
)) {}
36
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP
(
QPromise
)
37
~
QPromise
()
38
{
39
// If computation is not finished at this point, cancel
40
// potential waits
41
if
(
d
.
d
&& !(
d
.
loadState
() &
QFutureInterfaceBase
::
State
::
Finished
)) {
42
d
.
cancelAndFinish
();
// cancel and finalize the state
43
d
.
runContinuation
();
44
}
45
d
.
cleanContinuation
();
46
}
47
48
// Core QPromise APIs
49
QFuture
<
T
>
future
()
const
{
return
d
.
future
(); }
50
template
<
typename
...
Args
,
std
::
enable_if_t
<
std
::
is_constructible_v
<
T
,
Args
...>,
bool
> =
true
>
51
bool
emplaceResultAt
(
int
index
,
Args
&&...
args
)
52
{
53
return
d
.
reportAndEmplaceResult
(
index
,
std
::
forward
<
Args
>(
args
)...);
54
}
55
template
<
typename
...
Args
,
std
::
enable_if_t
<
std
::
is_constructible_v
<
T
,
Args
...>,
bool
> =
true
>
56
bool
emplaceResult
(
Args
&&...
args
)
57
{
58
return
d
.
reportAndEmplaceResult
(-1,
std
::
forward
<
Args
>(
args
)...);
59
}
60
template
<
typename
U
=
T
,
typename
=
QtPrivate
::
EnableIfSameOrConvertible
<
U
,
T
>>
61
bool
addResult
(
U
&&
result
,
int
index
= -1)
62
{
63
return
d
.
reportAndEmplaceResult
(
index
,
std
::
forward
<
U
>(
result
));
64
}
65
bool
addResults
(
const
QList
<
T
> &
result
)
66
{
return
d
.
reportResults
(
result
); }
67
#
ifndef
QT_NO_EXCEPTIONS
68
void
setException
(
const
QException
&
e
) {
d
.
reportException
(
e
); }
69
#
if
QT_VERSION
<
QT_VERSION_CHECK
(
7
,
0
,
0
)
70
void
setException
(
std
::
exception_ptr
e
) {
d
.
reportException
(
e
); }
71
#
else
72
void
setException
(
const
std
::
exception_ptr
&
e
) {
d
.
reportException
(
e
); }
73
#
endif
74
#
endif
75
void
start
() {
d
.
reportStarted
(); }
76
void
finish
() {
d
.
reportFinished
(); }
77
78
void
suspendIfRequested
() {
d
.
suspendIfRequested
(); }
79
80
bool
isCanceled
()
const
{
return
d
.
isCanceled
(); }
81
82
// Progress methods
83
void
setProgressRange
(
int
minimum
,
int
maximum
) {
d
.
setProgressRange
(
minimum
,
maximum
); }
84
void
setProgressValue
(
int
progressValue
) {
d
.
setProgressValue
(
progressValue
); }
85
void
setProgressValueAndText
(
int
progressValue
,
const
QString
&
progressText
)
86
{
87
d
.
setProgressValueAndText
(
progressValue
,
progressText
);
88
}
89
90
void
swap
(
QPromise
<
T
> &
other
)
noexcept
91
{
92
d
.
swap
(
other
.
d
);
93
}
94
95
#
if
defined
(
Q_QDOC
)
// documentation-only simplified signatures
96
bool
addResult
(
const
T
&
result
,
int
index
= -1) { }
97
bool
addResult
(
T
&&
result
,
int
index
= -1) { }
98
#
endif
99
private
:
100
mutable
QFutureInterface
<
T
>
d
;
101
};
102
103
template
<
typename
T>
104
inline
void
swap
(
QPromise
<T> &a,
QPromise
<T> &b)
noexcept
105
{
106
a.swap(b);
107
}
108
109
QT_END_NAMESPACE
110
111
#
endif
// QPROMISE_H
QPromise
Definition
qpromise.h:26
QPromise::QPromise
QPromise(const QFutureInterface< T > &other)
Definition
qpromise.h:34
QPromise::QPromise
QPromise()=default
QtPrivate
Definition
qalloc.h:27
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
swap
void swap(QPromise< T > &a, QPromise< T > &b) noexcept
Definition
qpromise.h:104
future
QFuture< void > future
[5]
Definition
src_concurrent_qtconcurrentfilter.cpp:22
qtbase
src
corelib
thread
qpromise.h
Generated on
for Qt by
1.14.0