File indexing completed on 2026-01-07 10:15:51
0001
0002
0003
0004
0005 #ifndef QTBASE_QTTASKBUILDER_H
0006 #define QTBASE_QTTASKBUILDER_H
0007
0008 #if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
0009
0010 #include <QtConcurrent/qtconcurrentstoredfunctioncall.h>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 #ifdef Q_QDOC
0015
0016 namespace QtConcurrent {
0017
0018 enum class FutureResult { Ignore };
0019
0020 using InvokeResultType = int;
0021
0022 template <class Task, class ...Args>
0023 class QTaskBuilder
0024 {
0025 public:
0026 [[nodiscard]]
0027 QFuture<InvokeResultType> spawn();
0028
0029 void spawn(FutureResult);
0030
0031 template <class ...ExtraArgs>
0032 [[nodiscard]]
0033 QTaskBuilder<Task, ExtraArgs...> withArguments(ExtraArgs &&...args);
0034
0035 [[nodiscard]]
0036 QTaskBuilder<Task, Args...> &onThreadPool(QThreadPool &newThreadPool);
0037
0038 [[nodiscard]]
0039 QTaskBuilder<Task, Args...> &withPriority(int newPriority);
0040 };
0041
0042 }
0043
0044 #else
0045
0046 namespace QtConcurrent {
0047
0048 enum class FutureResult { Ignore };
0049
0050 template <class Task, class ...Args>
0051 class QTaskBuilder
0052 {
0053 public:
0054 [[nodiscard]]
0055 auto spawn()
0056 {
0057 return TaskResolver<std::decay_t<Task>, std::decay_t<Args>...>::run(
0058 std::move(taskWithArgs), startParameters);
0059 }
0060
0061
0062 void spawn(FutureResult)
0063 {
0064 (new StoredFunctionCall<Task, Args...>(std::move(taskWithArgs)))
0065 ->start(startParameters);
0066 }
0067
0068 template <class ...ExtraArgs>
0069 [[nodiscard]]
0070 constexpr auto withArguments(ExtraArgs &&...args)
0071 {
0072 static_assert(std::tuple_size_v<TaskWithArgs> == 1,
0073 "This function cannot be invoked if "
0074 "arguments have already been passed.");
0075
0076 static_assert(sizeof...(ExtraArgs) >= 1,
0077 "One or more arguments must be passed.");
0078
0079
0080 return QTaskBuilder<Task, ExtraArgs...>(
0081 startParameters,
0082 std::get<0>(std::move(taskWithArgs)),
0083 std::forward<ExtraArgs>(args)...
0084 );
0085 }
0086
0087 [[nodiscard]]
0088 constexpr auto &onThreadPool(QThreadPool &newThreadPool)
0089 {
0090 startParameters.threadPool = &newThreadPool;
0091 return *this;
0092 }
0093
0094 [[nodiscard]]
0095 constexpr auto &withPriority(int newPriority)
0096 {
0097 startParameters.priority = newPriority;
0098 return *this;
0099 }
0100
0101 protected:
0102 constexpr explicit QTaskBuilder(Task &&task, Args &&...arguments)
0103 : taskWithArgs{std::forward<Task>(task), std::forward<Args>(arguments)...}
0104 {}
0105
0106 constexpr QTaskBuilder(
0107 const TaskStartParameters ¶meters, Task &&task, Args &&...arguments)
0108 : taskWithArgs{std::forward<Task>(task), std::forward<Args>(arguments)...}
0109 , startParameters{parameters}
0110 {}
0111
0112 private:
0113
0114 template <class T>
0115 friend constexpr auto task(T &&t);
0116
0117
0118 template <class T, class ...A>
0119 friend class QTaskBuilder;
0120
0121 private:
0122 using TaskWithArgs = DecayedTuple<Task, Args...>;
0123
0124 TaskWithArgs taskWithArgs;
0125 TaskStartParameters startParameters;
0126 };
0127
0128 }
0129
0130 #endif
0131
0132 QT_END_NAMESPACE
0133
0134 #endif
0135
0136 #endif