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 | #define QTCONCURRENT_RUN_NODISCARD \ |
39 | Q_NODISCARD_X("Use QThreadPool::start(Callable&&) if you don't need the returned QFuture") |
40 | |
41 | template <class Function, class ...Args> |
42 | QTCONCURRENT_RUN_NODISCARD |
43 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
44 | { |
45 | DecayedTuple<Function, Args...> tuple { std::forward<Function>(f), |
46 | std::forward<Args>(args)... }; |
47 | return TaskResolver<std::decay_t<Function>, std::decay_t<Args>...>::run( |
48 | std::move(tuple), TaskStartParameters { .threadPool: pool }); |
49 | } |
50 | |
51 | template <class Function, class ...Args> |
52 | QTCONCURRENT_RUN_NODISCARD |
53 | auto run(QThreadPool *pool, std::reference_wrapper<const Function> &&functionWrapper, |
54 | Args &&...args) |
55 | { |
56 | return run(pool, std::forward<const Function>(functionWrapper.get()), |
57 | std::forward<Args>(args)...); |
58 | } |
59 | |
60 | template <class Function, class ...Args> |
61 | QTCONCURRENT_RUN_NODISCARD |
62 | auto run(Function &&f, Args &&...args) |
63 | { |
64 | return run(QThreadPool::globalInstance(), std::forward<Function>(f), |
65 | std::forward<Args>(args)...); |
66 | } |
67 | |
68 | // overload with a Promise Type hint, takes thread pool |
69 | template <class PromiseType, class Function, class ...Args> |
70 | QTCONCURRENT_RUN_NODISCARD |
71 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
72 | { |
73 | return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>( |
74 | std::forward<Function>(f), std::forward<Args>(args)...))->start(pool); |
75 | } |
76 | |
77 | // overload with a Promise Type hint, uses global thread pool |
78 | template <class PromiseType, class Function, class ...Args> |
79 | QTCONCURRENT_RUN_NODISCARD |
80 | auto run(Function &&f, Args &&...args) |
81 | { |
82 | return run<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f), |
83 | std::forward<Args>(args)...); |
84 | } |
85 | |
86 | #undef QTCONCURRENT_RUN_NODISCARD |
87 | |
88 | } //namespace QtConcurrent |
89 | |
90 | #endif // Q_QDOC |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // QT_NO_CONCURRENT |
95 | |
96 | #endif |
97 | |