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
qtaskbuilder.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 QTBASE_QTTASKBUILDER_H
6#define QTBASE_QTTASKBUILDER_H
7
8#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
9
10#include <QtConcurrent/qtconcurrentstoredfunctioncall.h>
11
12QT_BEGIN_NAMESPACE
13
14#ifdef Q_QDOC
15
16namespace QtConcurrent {
17
18enum class FutureResult { Ignore };
19
20using InvokeResultType = int;
21
22template <class Task, class ...Args>
23class QTaskBuilder
24{
25public:
26 [[nodiscard]]
27 QFuture<InvokeResultType> spawn();
28
29 void spawn(FutureResult);
30
31 template <class ...ExtraArgs>
32 [[nodiscard]]
33 QTaskBuilder<Task, ExtraArgs...> withArguments(ExtraArgs &&...args);
34
35 [[nodiscard]]
36 QTaskBuilder<Task, Args...> &onThreadPool(QThreadPool &newThreadPool);
37
38 [[nodiscard]]
39 QTaskBuilder<Task, Args...> &withPriority(int newPriority);
40};
41
42} // namespace QtConcurrent
43
44#else
45
46namespace QtConcurrent {
47
48enum class FutureResult { Ignore };
49
50template <class Task, class ...Args>
52{
53public:
54 [[nodiscard]]
55 auto spawn()
56 {
57 return TaskResolver<std::decay_t<Task>, std::decay_t<Args>...>::run(
58 std::move(taskWithArgs), startParameters);
59 }
60
61 // We don't want to run with promise when we don't return a QFuture
63 {
64 (new StoredFunctionCall<Task, Args...>(std::move(taskWithArgs)))
65 ->start(startParameters);
66 }
67
68 template <class ...ExtraArgs>
69 [[nodiscard]]
70 constexpr auto withArguments(ExtraArgs &&...args)
71 {
72 static_assert(std::tuple_size_v<TaskWithArgs> == 1,
73 "This function cannot be invoked if "
74 "arguments have already been passed.");
75
76 static_assert(sizeof...(ExtraArgs) >= 1,
77 "One or more arguments must be passed.");
78
79 // We have to re-create a builder, because the type has changed
80 return QTaskBuilder<Task, ExtraArgs...>(
81 startParameters,
82 std::get<0>(std::move(taskWithArgs)),
83 std::forward<ExtraArgs>(args)...
84 );
85 }
86
87 [[nodiscard]]
88 constexpr auto &onThreadPool(QThreadPool &newThreadPool)
89 {
90 startParameters.threadPool = &newThreadPool;
91 return *this;
92 }
93
94 [[nodiscard]]
95 constexpr auto &withPriority(int newPriority)
96 {
97 startParameters.priority = newPriority;
98 return *this;
99 }
100
101protected: // Methods
102 constexpr explicit QTaskBuilder(Task &&task, Args &&...arguments)
104 {}
105
106 constexpr QTaskBuilder(
107 const TaskStartParameters &parameters, Task &&task, Args &&...arguments)
110 {}
111
112private: // Methods
113 // Required for creating a builder from "task" function
114 template <class T>
115 friend constexpr auto task(T &&t);
116
117 // Required for creating a new builder from "withArguments" function
118 template <class T, class ...A>
119 friend class QTaskBuilder;
120
121private: // Data
122 using TaskWithArgs = DecayedTuple<Task, Args...>;
123
124 TaskWithArgs taskWithArgs;
125 TaskStartParameters startParameters;
126};
127
128} // namespace QtConcurrent
129
130#endif // Q_QDOC
131
132QT_END_NAMESPACE
133
134#endif // !defined(QT_NO_CONCURRENT)
135
136#endif //QTBASE_QTTASKBUILDER_H
constexpr auto withArguments(ExtraArgs &&...args)
friend constexpr auto task(T &&t)
void spawn(FutureResult)
constexpr QTaskBuilder(Task &&task, Args &&...arguments)
constexpr QTaskBuilder(const TaskStartParameters &parameters, Task &&task, Args &&...arguments)
constexpr auto & onThreadPool(QThreadPool &newThreadPool)
constexpr auto & withPriority(int newPriority)
\inmodule QtConcurrent