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 <utility>
26
27QT_BEGIN_NAMESPACE
28
30
31#ifdef __cpp_lib_ranges
32using std::ranges::all_of;
33using std::ranges::any_of;
34using std::ranges::copy;
35using std::ranges::equal;
36using std::ranges::fill;
37using std::ranges::find;
38using std::ranges::find_if;
39using std::ranges::max;
40using std::ranges::max_element;
41using std::ranges::min;
42using std::ranges::min_element;
43using std::ranges::sort;
44using std::ranges::stable_sort;
45
46#else
47
48// Caveat: best effort, not a 1-to-1 mapping to c++20 style ranges
49
50inline constexpr auto all_of = [](const auto &range, auto predicate) {
51 return std::all_of(std::begin(range), std::end(range), std::move(predicate));
52};
53
54inline constexpr auto any_of = [](const auto &range, auto predicate) {
55 return std::any_of(std::begin(range), std::end(range), std::move(predicate));
56};
57
58inline constexpr auto copy = [](const auto &in, const auto &out) {
59 return std::copy(in.begin(), in.end(), out);
60};
61
62inline constexpr auto equal = [](const auto &lhs, const auto &rhs, auto predicate) {
63 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::move(predicate));
64};
65
66inline constexpr auto fill = [](auto &range, auto value) {
67 return std::fill(std::begin(range), std::end(range), std::move(value));
68};
69
70inline constexpr auto find = [](const auto &range, const auto &value) {
71 return std::find(std::begin(range), std::end(range), value);
72};
73
74inline constexpr auto find_if = [](const auto &range, auto predicate) {
75 return std::find_if(std::begin(range), std::end(range), std::move(predicate));
76};
77
78namespace impl {
79
80struct max_fn
81{
82 template <typename Range, typename Comp = std::less<>>
83 auto operator()(const Range &range, Comp comp) const
84 {
85 auto it = std::max_element(std::begin(range), std::end(range), std::move(comp));
86 return *it;
87 }
88
89 template <typename Range>
90 auto operator()(const Range &range) const
91 {
92 auto it = std::max_element(std::begin(range), std::end(range));
93 return *it;
94 }
95};
96
98{
99 template <typename Range, typename Comp = std::less<>>
100 auto operator()(const Range &range, Comp comp) const
101 {
102 return std::max_element(std::begin(range), std::end(range), std::move(comp));
103 }
104
105 template <typename Range>
106 auto operator()(const Range &range) const
107 {
108 return std::max_element(std::begin(range), std::end(range));
109 }
110};
111
112struct min_fn
113{
114 template <typename Range, typename Comp = std::less<>>
115 auto operator()(const Range &range, Comp comp) const
116 {
117 auto it = std::min_element(std::begin(range), std::end(range), std::move(comp));
118 return *it;
119 }
120
121 template <typename Range>
122 auto operator()(const Range &range) const
123 {
124 auto it = std::min_element(std::begin(range), std::end(range));
125 return *it;
126 }
127};
128
130{
131 template <typename Range, typename Comp = std::less<>>
132 auto operator()(const Range &range, Comp comp) const
133 {
134 return std::min_element(std::begin(range), std::end(range), std::move(comp));
135 }
136
137 template <typename Range>
138 auto operator()(const Range &range) const
139 {
140 return std::min_element(std::begin(range), std::end(range));
141 }
142};
143
145{
146 template <typename Range, typename Comp = std::less<>>
147 void operator()(Range &range, Comp comp) const
148 {
149 std::sort(std::begin(range), std::end(range), std::move(comp));
150 }
151
152 template <typename Range>
153 void operator()(Range &range) const
154 {
155 std::sort(std::begin(range), std::end(range));
156 }
157};
158
160{
161 template <typename Range, typename Comp = std::less<>>
162 void operator()(Range &range, Comp comp) const
163 {
164 std::stable_sort(std::begin(range), std::end(range), std::move(comp));
165 }
166
167 template <typename Range>
168 void operator()(Range &range) const
169 {
170 std::stable_sort(std::begin(range), std::end(range));
171 }
172};
173
174} // namespace impl
175
176inline constexpr auto sort = impl::sort_fn{};
177inline constexpr auto stable_sort = impl::stable_sort_fn{};
178inline constexpr auto max = impl::max_fn{};
179inline constexpr auto max_element = impl::max_element_fn{};
180inline constexpr auto min = impl::min_fn{};
181inline constexpr auto min_element = impl::min_element_fn{};
182
183#endif
184
185#if __cpp_lib_ranges_contains >= 202207L
186using std::ranges::contains;
187#else
188
189inline constexpr auto contains = [](const auto &range, const auto &value) {
190 return std::find(std::begin(range), std::end(range), value) != std::end(range);
191};
192
193#endif
194
195} // namespace QtMultimediaPrivate::ranges
196
197QT_END_NAMESPACE
198
199#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