1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtConcurrent module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QTCONCURRENT_RUN_H |
41 | #define QTCONCURRENT_RUN_H |
42 | |
43 | #include <QtConcurrent/qtconcurrentcompilertest.h> |
44 | |
45 | #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) |
46 | |
47 | #include <QtConcurrent/qtconcurrentrunbase.h> |
48 | #include <QtConcurrent/qtconcurrentstoredfunctioncall.h> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | #ifdef Q_CLANG_QDOC |
53 | |
54 | typedef int Function; |
55 | |
56 | namespace QtConcurrent { |
57 | |
58 | template <typename T> |
59 | QFuture<T> run(Function function, ...); |
60 | |
61 | template <typename T> |
62 | QFuture<T> run(QThreadPool *pool, Function function, ...); |
63 | |
64 | template <typename T> |
65 | QFuture<T> runWithPromise(Function function, ...); |
66 | |
67 | template <typename T> |
68 | QFuture<T> runWithPromise(QThreadPool *pool, Function function, ...); |
69 | |
70 | } // namespace QtConcurrent |
71 | |
72 | #else |
73 | |
74 | namespace QtConcurrent { |
75 | |
76 | template <class Function, class ...Args> |
77 | [[nodiscard]] |
78 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
79 | { |
80 | DecayedTuple<Function, Args...> tuple { std::forward<Function>(f), |
81 | std::forward<Args>(args)... }; |
82 | return TaskResolver<std::decay_t<Function>, std::decay_t<Args>...>::run( |
83 | std::move(tuple), TaskStartParameters { pool }); |
84 | } |
85 | |
86 | template <class Function, class ...Args> |
87 | [[nodiscard]] |
88 | auto run(QThreadPool *pool, std::reference_wrapper<const Function> &&functionWrapper, |
89 | Args &&...args) |
90 | { |
91 | return run(pool, std::forward<const Function>(functionWrapper.get()), |
92 | std::forward<Args>(args)...); |
93 | } |
94 | |
95 | template <class Function, class ...Args> |
96 | [[nodiscard]] |
97 | auto run(Function &&f, Args &&...args) |
98 | { |
99 | return run(QThreadPool::globalInstance(), std::forward<Function>(f), |
100 | std::forward<Args>(args)...); |
101 | } |
102 | |
103 | // overload with a Promise Type hint, takes thread pool |
104 | template <class PromiseType, class Function, class ...Args> |
105 | [[nodiscard]] |
106 | auto run(QThreadPool *pool, Function &&f, Args &&...args) |
107 | { |
108 | return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>( |
109 | std::forward<Function>(f), std::forward<Args>(args)...))->start(pool); |
110 | } |
111 | |
112 | // overload with a Promise Type hint, uses global thread pool |
113 | template <class PromiseType, class Function, class ...Args> |
114 | [[nodiscard]] |
115 | auto run(Function &&f, Args &&...args) |
116 | { |
117 | return run<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f), |
118 | std::forward<Args>(args)...); |
119 | } |
120 | |
121 | } //namespace QtConcurrent |
122 | |
123 | #endif // Q_CLANG_QDOC |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | #endif // QT_NO_CONCURRENT |
128 | |
129 | #endif |
130 | |