Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 10:15:51

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
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 } // namespace QtConcurrent
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     // We don't want to run with promise when we don't return a QFuture
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         // We have to re-create a builder, because the type has changed
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: // Methods
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 &parameters, Task &&task, Args &&...arguments)
0108         : taskWithArgs{std::forward<Task>(task), std::forward<Args>(arguments)...}
0109         , startParameters{parameters}
0110     {}
0111 
0112 private: // Methods
0113     // Required for creating a builder from "task" function
0114     template <class T>
0115     friend constexpr auto task(T &&t);
0116 
0117     // Required for creating a new builder from "withArguments" function
0118     template <class T, class ...A>
0119     friend class QTaskBuilder;
0120 
0121 private: // Data
0122     using TaskWithArgs = DecayedTuple<Task, Args...>;
0123 
0124     TaskWithArgs taskWithArgs;
0125     TaskStartParameters startParameters;
0126 };
0127 
0128 } // namespace QtConcurrent
0129 
0130 #endif // Q_QDOC
0131 
0132 QT_END_NAMESPACE
0133 
0134 #endif // !defined(QT_NO_CONCURRENT)
0135 
0136 #endif //QTBASE_QTTASKBUILDER_H