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_FUNCTIONWRAPPERS_H |
5 | #define QTCONCURRENT_FUNCTIONWRAPPERS_H |
6 | |
7 | #include <QtConcurrent/qtconcurrentcompilertest.h> |
8 | #include <QtConcurrent/qtconcurrentreducekernel.h> |
9 | #include <QtCore/qfuture.h> |
10 | |
11 | #include <tuple> |
12 | |
13 | #if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC) |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | namespace QtPrivate { |
18 | |
19 | struct PushBackWrapper |
20 | { |
21 | template <class C, class U> |
22 | inline void operator()(C &c, const U &u) const |
23 | { |
24 | return c.push_back(u); |
25 | } |
26 | |
27 | template <class C, class U> |
28 | inline void operator()(C &c, U &&u) const |
29 | { |
30 | return c.push_back(u); |
31 | } |
32 | }; |
33 | |
34 | // -- MapResultType |
35 | |
36 | template <class T, class Enable = void> |
37 | struct Argument |
38 | { |
39 | using Type = void; |
40 | }; |
41 | |
42 | template <class Sequence> |
43 | struct Argument<Sequence, typename std::enable_if<IsIterableValue<Sequence>>::type> |
44 | { |
45 | using Type = std::decay_t<decltype(*std::declval<Sequence>().begin())>; |
46 | }; |
47 | |
48 | template <class Iterator> |
49 | struct Argument<Iterator, typename std::enable_if<IsDereferenceableValue<Iterator>>::type> |
50 | { |
51 | using Type = std::decay_t<decltype(*std::declval<Iterator>())>; |
52 | }; |
53 | |
54 | template <class T> |
55 | using ArgumentType = typename Argument<T>::Type; |
56 | |
57 | template <class T, class MapFunctor> |
58 | struct MapResult |
59 | { |
60 | static_assert(std::is_invocable_v<std::decay_t<MapFunctor>, ArgumentType<T>>, |
61 | "It's not possible to invoke the function with passed argument." ); |
62 | using Type = std::invoke_result_t<std::decay_t<MapFunctor>, ArgumentType<T>>; |
63 | }; |
64 | |
65 | template <class T, class MapFunctor> |
66 | using MapResultType = typename MapResult<T, MapFunctor>::Type; |
67 | |
68 | // -- ReduceResultType |
69 | |
70 | template <class T> |
71 | struct ReduceResultType; |
72 | |
73 | template <class U, class V> |
74 | struct ReduceResultType<void(*)(U&,V)> |
75 | { |
76 | using ResultType = U; |
77 | }; |
78 | |
79 | template <class T, class C, class U> |
80 | struct ReduceResultType<T(C::*)(U)> |
81 | { |
82 | using ResultType = C; |
83 | }; |
84 | |
85 | template <class U, class V> |
86 | struct ReduceResultType<std::function<void(U&, V)>> |
87 | { |
88 | using ResultType = U; |
89 | }; |
90 | |
91 | template <typename R, typename ...A> |
92 | struct ReduceResultType<R(*)(A...)> |
93 | { |
94 | using ResultType = typename std::tuple_element<0, std::tuple<A...>>::type; |
95 | }; |
96 | |
97 | template <class U, class V> |
98 | struct ReduceResultType<void(*)(U&,V) noexcept> |
99 | { |
100 | using ResultType = U; |
101 | }; |
102 | |
103 | template <class T, class C, class U> |
104 | struct ReduceResultType<T(C::*)(U) noexcept> |
105 | { |
106 | using ResultType = C; |
107 | }; |
108 | |
109 | template<class T, class Enable = void> |
110 | inline constexpr bool hasCallOperator_v = false; |
111 | |
112 | template<class T> |
113 | inline constexpr bool hasCallOperator_v<T, std::void_t<decltype(&T::operator())>> = true; |
114 | |
115 | template<class T, class Enable = void> |
116 | inline constexpr bool isIterator_v = false; |
117 | |
118 | template<class T> |
119 | inline constexpr bool isIterator_v<T, std::void_t<typename std::iterator_traits<T>::value_type>> = |
120 | true; |
121 | |
122 | template <class Callable, class Sequence> |
123 | using isInvocable = std::is_invocable<Callable, typename std::decay_t<Sequence>::value_type>; |
124 | |
125 | template <class InitialValueType, class ResultType> |
126 | inline constexpr bool isInitialValueCompatible_v = std::conjunction_v< |
127 | std::is_convertible<InitialValueType, ResultType>, |
128 | std::negation<std::is_same<std::decay_t<InitialValueType>, QtConcurrent::ReduceOption>>>; |
129 | |
130 | template<class Callable, class Enable = void> |
131 | struct ReduceResultTypeHelper |
132 | { |
133 | }; |
134 | |
135 | template <class Callable> |
136 | struct ReduceResultTypeHelper<Callable, |
137 | typename std::enable_if_t<std::is_function_v<std::remove_pointer_t<std::decay_t<Callable>>> |
138 | || std::is_member_function_pointer_v<std::decay_t<Callable>>>> |
139 | { |
140 | using type = typename QtPrivate::ReduceResultType<std::decay_t<Callable>>::ResultType; |
141 | }; |
142 | |
143 | template <class Callable> |
144 | struct ReduceResultTypeHelper<Callable, |
145 | typename std::enable_if_t<!std::is_function_v<std::remove_pointer_t<std::decay_t<Callable>>> |
146 | && hasCallOperator_v<std::decay_t<Callable>>>> |
147 | { |
148 | using type = std::decay_t<typename QtPrivate::ArgResolver<Callable>::First>; |
149 | }; |
150 | |
151 | // -- MapSequenceResultType |
152 | |
153 | template <class InputSequence, class MapFunctor> |
154 | struct MapSequenceResultType |
155 | { |
156 | static_assert(std::is_same_v<typename InputSequence::value_type, |
157 | QtPrivate::MapResultType<InputSequence, MapFunctor>>, |
158 | "Couldn't deduce the output sequence type, you must specify it explicitly." ); |
159 | typedef InputSequence ResultType; |
160 | }; |
161 | |
162 | #ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS |
163 | |
164 | template <template <typename...> class InputSequence, typename MapFunctor, typename ...T> |
165 | struct MapSequenceResultType<InputSequence<T...>, MapFunctor> |
166 | { |
167 | typedef InputSequence<QtPrivate::MapResultType<InputSequence<T...>, MapFunctor>> ResultType; |
168 | }; |
169 | |
170 | #endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER |
171 | |
172 | } // namespace QtPrivate. |
173 | |
174 | |
175 | QT_END_NAMESPACE |
176 | |
177 | #endif // QT_NO_CONCURRENT |
178 | |
179 | #endif |
180 | |