Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmultimedia_ranges_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 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 QMULTIMEDIA_RANGES_P_H
5#define QMULTIMEDIA_RANGES_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qtconfigmacros.h>
19
20#ifdef __cpp_lib_ranges
21# include <ranges> // IWYU pragma: export
22#endif
23
24#include <algorithm>
25#include <functional>
26#include <utility>
27
28QT_BEGIN_NAMESPACE
29
31
32#ifdef __cpp_lib_ranges
33using std::ranges::all_of;
34using std::ranges::any_of;
35using std::ranges::copy;
36using std::ranges::equal;
37using std::ranges::fill;
38using std::ranges::find;
39using std::ranges::find_if;
40using std::ranges::max;
41using std::ranges::max_element;
42using std::ranges::min;
43using std::ranges::min_element;
44using std::ranges::sort;
45using std::ranges::stable_sort;
46
47#else
48
49// Caveat: best effort, not a 1-to-1 mapping to c++20 style ranges
50
51inline constexpr auto all_of = [](const auto &range, auto predicate) {
52 return std::all_of(std::begin(range), std::end(range), std::move(predicate));
53};
54
55inline constexpr auto any_of = [](const auto &range, auto predicate) {
56 return std::any_of(std::begin(range), std::end(range), std::move(predicate));
57};
58
59inline constexpr auto copy = [](const auto &in, const auto &out) {
60 return std::copy(in.begin(), in.end(), out);
61};
62
63inline constexpr auto equal = [](const auto &lhs, const auto &rhs, auto predicate) {
64 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::move(predicate));
65};
66
67inline constexpr auto fill = [](auto &range, auto value) {
68 return std::fill(std::begin(range), std::end(range), std::move(value));
69};
70
71inline constexpr auto find = [](const auto &range, const auto &value) {
72 return std::find(std::begin(range), std::end(range), value);
73};
74
75inline constexpr auto find_if = [](const auto &range, auto predicate) {
76 return std::find_if(std::begin(range), std::end(range), std::move(predicate));
77};
78
79namespace impl {
80
81struct max_fn
82{
83 template <typename Range, typename Comp = std::less<>>
84 auto operator()(const Range &range, Comp comp) const
85 {
86 auto it = std::max_element(std::begin(range), std::end(range), std::move(comp));
87 return *it;
88 }
89
90 template <typename Range>
91 auto operator()(const Range &range) const
92 {
93 auto it = std::max_element(std::begin(range), std::end(range));
94 return *it;
95 }
96};
97
99{
100 template <typename Range, typename Comp = std::less<>>
101 auto operator()(const Range &range, Comp comp) const
102 {
103 return std::max_element(std::begin(range), std::end(range), std::move(comp));
104 }
105
106 template <typename Range>
107 auto operator()(const Range &range) const
108 {
109 return std::max_element(std::begin(range), std::end(range));
110 }
111};
112
113struct min_fn
114{
115 template <typename Range, typename Comp = std::less<>>
116 auto operator()(const Range &range, Comp comp) const
117 {
118 auto it = std::min_element(std::begin(range), std::end(range), std::move(comp));
119 return *it;
120 }
121
122 template <typename Range>
123 auto operator()(const Range &range) const
124 {
125 auto it = std::min_element(std::begin(range), std::end(range));
126 return *it;
127 }
128};
129
131{
132 template <typename Range, typename Comp = std::less<>>
133 auto operator()(const Range &range, Comp comp) const
134 {
135 return std::min_element(std::begin(range), std::end(range), std::move(comp));
136 }
137
138 template <typename Range>
139 auto operator()(const Range &range) const
140 {
141 return std::min_element(std::begin(range), std::end(range));
142 }
143};
144
146{
147 template <typename Range, typename Comp = std::less<>>
148 void operator()(Range &range, Comp comp) const
149 {
150 std::sort(std::begin(range), std::end(range), std::move(comp));
151 }
152
153 template <typename Range>
154 void operator()(Range &range) const
155 {
156 std::sort(std::begin(range), std::end(range));
157 }
158};
159
161{
162 template <typename Range, typename Comp = std::less<>>
163 void operator()(Range &range, Comp comp) const
164 {
165 std::stable_sort(std::begin(range), std::end(range), std::move(comp));
166 }
167
168 template <typename Range>
169 void operator()(Range &range) const
170 {
171 std::stable_sort(std::begin(range), std::end(range));
172 }
173};
174
175} // namespace impl
176
177inline constexpr auto sort = impl::sort_fn{};
178inline constexpr auto stable_sort = impl::stable_sort_fn{};
179inline constexpr auto max = impl::max_fn{};
180inline constexpr auto max_element = impl::max_element_fn{};
181inline constexpr auto min = impl::min_fn{};
182inline constexpr auto min_element = impl::min_element_fn{};
183
184#endif
185
186#if __cpp_lib_ranges_contains >= 202207L
187using std::ranges::contains;
188#else
189
190inline constexpr auto contains = [](const auto &range, const auto &value) {
191 return std::find(std::begin(range), std::end(range), value) != std::end(range);
192};
193
194#endif
195
196} // namespace QtMultimediaPrivate::ranges
197
198QT_END_NAMESPACE
199
200#endif // QMULTIMEDIA_RANGES_P_H
auto operator()(const Range &range, Comp comp) const
auto operator()(const Range &range) const
auto operator()(const Range &range, Comp comp) const
auto operator()(const Range &range, Comp comp) const
auto operator()(const Range &range, Comp comp) const
void operator()(Range &range, Comp comp) const