| 1 | // Copyright (C) 2016 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 QTCONCURRENT_RUN_H |
| 6 | #define QTCONCURRENT_RUN_H |
| 7 | |
| 8 | #if 0 |
| 9 | #pragma qt_class(QtConcurrentRun) |
| 10 | #endif |
| 11 | |
| 12 | #include <QtConcurrent/qtconcurrentcompilertest.h> |
| 13 | |
| 14 | #if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC) |
| 15 | |
| 16 | #include <QtConcurrent/qtconcurrentrunbase.h> |
| 17 | #include <QtConcurrent/qtconcurrentstoredfunctioncall.h> |
| 18 | |
| 19 | QT_BEGIN_NAMESPACE |
| 20 | |
| 21 | #ifdef Q_QDOC |
| 22 | |
| 23 | typedef int Function; |
| 24 | |
| 25 | namespace QtConcurrent { |
| 26 | |
| 27 | template <typename T> |
| 28 | QFuture<T> run(Function function, ...); |
| 29 | |
| 30 | template <typename T> |
| 31 | QFuture<T> run(QThreadPool *pool, Function function, ...); |
| 32 | |
| 33 | } // namespace QtConcurrent |
| 34 | |
| 35 | #else |
| 36 | |
| 37 | namespace QtConcurrent { |
| 38 | |
| 39 | #define QTCONCURRENT_RUN_NODISCARD \ |
| 40 | Q_NODISCARD_X("Use QThreadPool::start(Callable&&) if you don't need the returned QFuture") |
| 41 | |
| 42 | template <class Function, class ...Args> |
| 43 | QTCONCURRENT_RUN_NODISCARD |
| 44 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
| 45 | { |
| 46 | DecayedTuple<Function, Args...> tuple { std::forward<Function>(f), |
| 47 | std::forward<Args>(args)... }; |
| 48 | return TaskResolver<std::decay_t<Function>, std::decay_t<Args>...>::run( |
| 49 | std::move(tuple), TaskStartParameters { .threadPool: pool }); |
| 50 | } |
| 51 | |
| 52 | template <class Function, class ...Args> |
| 53 | QTCONCURRENT_RUN_NODISCARD |
| 54 | auto run(QThreadPool *pool, std::reference_wrapper<const Function> &&functionWrapper, |
| 55 | Args &&...args) |
| 56 | { |
| 57 | return run(pool, std::forward<const Function>(functionWrapper.get()), |
| 58 | std::forward<Args>(args)...); |
| 59 | } |
| 60 | |
| 61 | template <class Function, class ...Args> |
| 62 | QTCONCURRENT_RUN_NODISCARD |
| 63 | auto run(Function &&f, Args &&...args) |
| 64 | { |
| 65 | return run(QThreadPool::globalInstance(), std::forward<Function>(f), |
| 66 | std::forward<Args>(args)...); |
| 67 | } |
| 68 | |
| 69 | // overload with a Promise Type hint, takes thread pool |
| 70 | template <class PromiseType, class Function, class ...Args> |
| 71 | QTCONCURRENT_RUN_NODISCARD |
| 72 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
| 73 | { |
| 74 | return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>( |
| 75 | std::forward<Function>(f), std::forward<Args>(args)...))->start(pool); |
| 76 | } |
| 77 | |
| 78 | // overload with a Promise Type hint, uses global thread pool |
| 79 | template <class PromiseType, class Function, class ...Args> |
| 80 | QTCONCURRENT_RUN_NODISCARD |
| 81 | auto run(Function &&f, Args &&...args) |
| 82 | { |
| 83 | return run<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f), |
| 84 | std::forward<Args>(args)...); |
| 85 | } |
| 86 | |
| 87 | #undef QTCONCURRENT_RUN_NODISCARD |
| 88 | |
| 89 | } //namespace QtConcurrent |
| 90 | |
| 91 | #endif // Q_QDOC |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |
| 95 | #endif // QT_NO_CONCURRENT |
| 96 | |
| 97 | #endif |
| 98 | |