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_FlagCache.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 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26#if ! DOXYGEN
27
28namespace juce
29{
30
31template <size_t requiredFlagBitsPerItem>
32class FlagCache
33{
34 using FlagType = uint32_t;
35
36public:
37 FlagCache() = default;
38
39 explicit FlagCache (size_t items)
40 : flags (divCeil (items, groupsPerWord))
41 {
42 std::fill (flags.begin(), flags.end(), 0);
43 }
44
45 void set (size_t index, FlagType bits)
46 {
47 const auto flagIndex = index / groupsPerWord;
48 jassert (flagIndex < flags.size());
49 const auto groupIndex = index - (flagIndex * groupsPerWord);
50 flags[flagIndex].fetch_or (moveToGroupPosition (bits, groupIndex), std::memory_order_acq_rel);
51 }
52
53 /* Calls the supplied callback for any entries with non-zero flags, and
54 sets all flags to zero.
55 */
56 template <typename Callback>
57 void ifSet (Callback&& callback)
58 {
59 for (size_t flagIndex = 0; flagIndex < flags.size(); ++flagIndex)
60 {
61 const auto prevFlags = flags[flagIndex].exchange (0, std::memory_order_acq_rel);
62
63 for (size_t group = 0; group < groupsPerWord; ++group)
64 {
65 const auto masked = moveFromGroupPosition (prevFlags, group);
66
67 if (masked != 0)
68 callback ((flagIndex * groupsPerWord) + group, masked);
69 }
70 }
71 }
72
73 void clear()
74 {
75 std::fill (flags.begin(), flags.end(), 0);
76 }
77
78private:
79 /* Given the flags for a single item, and a group index, shifts the flags
80 so that they are positioned at the appropriate location for that group
81 index.
82
83 e.g. If the flag type is a uint32_t, and there are 2 flags per item,
84 then each uint32_t will hold flags for 16 items. The flags for item 0
85 are the least significant two bits; the flags for item 15 are the most
86 significant two bits.
87 */
89 {
91 }
92
93 /* Given a set of grouped flags for multiple items, and a group index,
94 extracts the flags set for an item at that group index.
95
96 e.g. If the flag type is a uint32_t, and there are 2 flags per item,
97 then each uint32_t will hold flags for 16 items. Asking for groupIndex
98 0 will return the least significant two bits; asking for groupIndex 15
99 will return the most significant two bits.
100 */
101 static constexpr FlagType moveFromGroupPosition (FlagType grouped, size_t groupIndex)
102 {
104 }
105
106 static constexpr size_t findNextPowerOfTwoImpl (size_t n, size_t shift)
107 {
108 return shift == 32 ? n : findNextPowerOfTwoImpl (n | (n >> shift), shift * 2);
109 }
110
111 static constexpr size_t findNextPowerOfTwo (size_t value)
112 {
113 return findNextPowerOfTwoImpl (value - 1, 1) + 1;
114 }
115
116 static constexpr size_t divCeil (size_t a, size_t b)
117 {
118 return (a / b) + ((a % b) != 0);
119 }
120
122 static constexpr size_t groupsPerWord = (8 * sizeof (FlagType)) / bitsPerFlagGroup;
123 static constexpr FlagType groupMask = ((FlagType) 1 << requiredFlagBitsPerItem) - 1;
124
126};
127
128template <size_t requiredFlagBitsPerItem>
130{
131public:
132 FlaggedFloatCache() = default;
133
134 explicit FlaggedFloatCache (size_t sizeIn)
135 : values (sizeIn),
136 flags (sizeIn)
137 {
138 std::fill (values.begin(), values.end(), 0.0f);
139 }
140
141 size_t size() const noexcept { return values.size(); }
142
143 float exchangeValue (size_t index, float value)
144 {
145 jassert (index < size());
146 return values[index].exchange (value, std::memory_order_relaxed);
147 }
148
149 void setBits (size_t index, uint32_t bits) { flags.set (index, bits); }
150
151 void setValueAndBits (size_t index, float value, uint32_t bits)
152 {
153 exchangeValue (index, value);
154 setBits (index, bits);
155 }
156
157 float get (size_t index) const noexcept
158 {
159 jassert (index < size());
160 return values[index].load (std::memory_order_relaxed);
161 }
162
163 /* Calls the supplied callback for any entries which have been modified
164 since the last call to this function.
165 */
166 template <typename Callback>
167 void ifSet (Callback&& callback)
168 {
169 flags.ifSet ([this, &callback] (size_t groupIndex, uint32_t bits)
170 {
171 callback (groupIndex, values[groupIndex].load (std::memory_order_relaxed), bits);
172 });
173 }
174
175private:
178};
179
180} // namespace juce
181
182#endif
T fill(T... args)
#define jassert(expression)
Platform-independent assertion macro.
auto & get(ProcessorChain< Processors... > &chain) noexcept
Non-member equivalent of ProcessorChain::get which avoids awkward member template syntax.
JUCE Namespace.
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
T size(T... args)
typedef uint32_t