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