Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:09:50

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