JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Enumerate.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26namespace detail
27{
28
29template <typename T, typename = void>
30constexpr auto canPreDecrement = false;
31
32template <typename T>
33constexpr auto canPreDecrement<T, std::void_t<decltype (--std::declval<T>())>> = true;
34
35template <typename T, typename I, typename = void>
36constexpr auto canAddAssign = false;
37
38template <typename T, typename I>
39constexpr auto canAddAssign<T, I, std::void_t<decltype (std::declval<T>() += std::declval<I>())>> = true;
40
41template <typename T, typename I, typename = void>
42constexpr auto canSubAssign = false;
43
44template <typename T, typename I>
45constexpr auto canSubAssign<T, I, std::void_t<decltype (std::declval<T>() -= std::declval<I>())>> = true;
46
47template <typename T, typename I, typename = void>
48constexpr auto canAdd = false;
49
50template <typename T, typename I>
51constexpr auto canAdd<T, I, std::void_t<decltype (std::declval<T>() + std::declval<I>())>> = true;
52
53template <typename T, typename I, typename = void>
54constexpr auto canSub = false;
55
56template <typename T, typename I>
57constexpr auto canSub<T, I, std::void_t<decltype (std::declval<T>() - std::declval<I>())>> = true;
58
59template <typename T, typename I, typename = void>
60constexpr auto canLessThan = false;
61
62template <typename T, typename I>
63constexpr auto canLessThan<T, I, std::void_t<decltype (std::declval<T>() < std::declval<I>())>> = true;
64
65template <typename T, typename I, typename = void>
66constexpr auto canLessThanEqual = false;
67
68template <typename T, typename I>
69constexpr auto canLessThanEqual<T, I, std::void_t<decltype (std::declval<T>() <= std::declval<I>())>> = true;
70
71template <typename T, typename I, typename = void>
72constexpr auto canGreaterThan = false;
73
74template <typename T, typename I>
75constexpr auto canGreaterThan<T, I, std::void_t<decltype (std::declval<T>() > std::declval<I>())>> = true;
76
77template <typename T, typename I, typename = void>
78constexpr auto canGreaterThanEqual = false;
79
80template <typename T, typename I>
81constexpr auto canGreaterThanEqual<T, I, std::void_t<decltype (std::declval<T>() >= std::declval<I>())>> = true;
82
83namespace withAdlSize
84{
85 using std::size;
86
87 template <typename Range>
88 using AdlSize = decltype (size (std::declval<Range>()));
89
90 template <typename Range>
92}
93
94} // namespace detail
95
108template <typename Index, typename Value>
110{
111 Index index;
112 Value value;
113};
114
126template <typename Iter, typename Index = ptrdiff_t>
128{
129public:
131 constexpr EnumerateIterator() = default;
132
134 constexpr explicit EnumerateIterator (Iter iter)
135 : EnumerateIterator (std::move (iter), Index{}) {}
136
138 constexpr EnumerateIterator (Iter iter, Index ind)
139 : iterator (std::move (iter)), index (ind) {}
140
142 template <typename OtherIter, typename OtherInd>
144 {
145 return iterator == other.iterator;
146 }
147
149 template <typename OtherIter, typename OtherInd>
151 {
152 return ! operator== (other);
153 }
154
160 {
161 return { index, *iterator };
162 }
163
166 {
167 ++iterator;
168 ++index;
169 return *this;
170 }
171
174 {
175 auto copy = *this;
176 operator++();
177 return copy;
178 }
179
183 template <typename T = Iter, std::enable_if_t<detail::canPreDecrement<T>, int> = 0>
185 {
186 --iterator;
187 --index;
188 return *this;
189 }
190
194 template <typename T = Iter, std::enable_if_t<detail::canPreDecrement<T>, int> = 0>
196 {
197 auto copy = *this;
198 operator--();
199 return copy;
200 }
201
205 template <typename I, std::enable_if_t<detail::canAddAssign<Iter&, I>, int> = 0>
207 {
208 iterator += diff;
209 index += static_cast<Index> (diff);
210 return *this;
211 }
212
216 template <typename I, std::enable_if_t<detail::canSubAssign<Iter&, I>, int> = 0>
218 {
219 iterator -= diff;
220 index -= static_cast<Index> (diff);
221 return *this;
222 }
223
229 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canSub<Iter, OtherIter>, int> = 0>
231 {
232 return iterator - other.iterator;
233 }
234
240 template <typename I, std::enable_if_t<detail::canAdd<EnumerateIterator, I>, int> = 0>
241 [[nodiscard]] constexpr auto operator[] (I diff) const
242 {
243 return *(*this + diff);
244 }
245
249 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canLessThan<Iter, OtherIter>, int> = 0>
251 {
252 return iterator < other.iterator;
253 }
254
258 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canLessThanEqual<Iter, OtherIter>, int> = 0>
260 {
261 return iterator <= other.iterator;
262 }
263
267 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canGreaterThan<Iter, OtherIter>, int> = 0>
269 {
270 return iterator > other.iterator;
271 }
272
276 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canGreaterThanEqual<Iter, OtherIter>, int> = 0>
278 {
279 return iterator >= other.iterator;
280 }
281
285 template <typename I, std::enable_if_t<detail::canAddAssign<EnumerateIterator&, I>, int> = 0>
286 constexpr friend auto operator+ (EnumerateIterator iter, I ind)
287 {
288 return iter += ind;
289 }
290
294 template <typename I, std::enable_if_t<detail::canAddAssign<EnumerateIterator&, I>, int> = 0>
295 constexpr friend auto operator+ (I ind, EnumerateIterator iter)
296 {
297 return iter += ind;
298 }
299
303 template <typename I, std::enable_if_t<detail::canSubAssign<EnumerateIterator&, I>, int> = 0>
304 constexpr friend auto operator- (EnumerateIterator iter, I ind)
305 {
306 return iter -= ind;
307 }
308
309private:
310 Iter iterator{};
311 Index index = 0;
312};
313
314//==============================================================================
324template <typename Begin, typename End>
326{
327public:
331 constexpr IteratorPair (Begin bIn, End eIn)
332 : b (std::move (bIn)), e (std::move (eIn)) {}
333
335 constexpr auto begin() const { return b; }
336
338 constexpr auto end() const { return e; }
339
340private:
341 Begin b;
342 End e;
343};
344
352template <typename Begin, typename End = Begin>
353[[nodiscard]] constexpr auto makeRange (Begin begin, End end)
354{
355 return IteratorPair<Begin, End> { std::move (begin), std::move (end) };
356}
357
358//==============================================================================
418template <typename Range, typename Index = detail::withAdlSize::AdlSignedSize<Range>>
419[[nodiscard]] constexpr auto enumerate (Range&& range, Index startingValue = {})
420{
421 // This ensures argument-dependent lookup works properly for user-defined non-member begin/end
422 using std::begin, std::end;
423 return makeRange (EnumerateIterator { begin (range), startingValue },
424 EnumerateIterator { end (range), startingValue });
425}
426
427} // namespace juce
T begin(T... args)
An iterator that wraps some other iterator, keeping track of the relative position of that iterator b...
constexpr EnumerateIterator(Iter iter, Index ind)
Wraps the provided iterator, and sets the internal count to the provided value.
constexpr EnumerateIterator & operator-=(I diff)
Subtracts an integral value from both the iterator and the index.
constexpr EnumerateIterator & operator++()
Increments the iterator and the index.
constexpr bool operator<(const EnumerateIterator< OtherIter, OtherInd > &other) const
Returns the result of comparing the two wrapped iterators.
constexpr EnumerateIterator()=default
Default constructor.
constexpr EnumerateIterator(Iter iter)
Wraps the provided iterator, and sets the internal count to 0.
constexpr bool operator!=(const EnumerateIterator< OtherIter, OtherInd > &other) const
constexpr friend auto operator-(EnumerateIterator iter, I ind)
Returns the result of subtracting an integral value from this iterator.
constexpr bool operator>(const EnumerateIterator< OtherIter, OtherInd > &other) const
Returns the result of comparing the two wrapped iterators.
constexpr EnumerateIterator & operator--()
Decrements the iterator and the index.
constexpr friend auto operator+(EnumerateIterator iter, I ind)
Returns the result of adding an integral value to this iterator.
constexpr auto operator[](I diff) const
Indexes into this iterator, equivalent to adding an integral value to this iterator and then derefere...
constexpr bool operator<=(const EnumerateIterator< OtherIter, OtherInd > &other) const
Returns the result of comparing the two wrapped iterators.
constexpr EnumerateIterator & operator+=(I diff)
Adds an integral value to both the iterator and the index.
constexpr bool operator==(const EnumerateIterator< OtherIter, OtherInd > &other) const
Two EnumerateIterators are considered equal if the wrapped iterators are equal.
constexpr bool operator>=(const EnumerateIterator< OtherIter, OtherInd > &other) const
Returns the result of comparing the two wrapped iterators.
Wraps a pair of iterators, providing member begin() and end() functions that return those iterators.
constexpr IteratorPair(Begin bIn, End eIn)
Constructs a pair from a begin and end iterator.
constexpr auto end() const
Returns the end iterator.
constexpr auto begin() const
Returns the begin iterator.
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
Represents a shared variant value.
Definition juce_Value.h:51
T end(T... args)
JUCE Namespace.
constexpr auto enumerate(Range &&range, Index startingValue={})
Given a range and an optional starting offset, returns an IteratorPair that holds EnumerateIterators ...
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Returns a default-constructed sentinel value.
constexpr auto makeRange(Begin begin, End end)
Given two iterators "begin" and "end", returns an IteratorPair with a member begin() and end() functi...
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
RangedDirectoryIterator begin(const RangedDirectoryIterator &it)
Returns the iterator that was passed in.
Returned when dereferencing an EnumerateIterator.
T size(T... args)