tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_ConstrainedCachedValue.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14//==============================================================================
18template <typename Type>
20{
21public:
22 //==============================================================================
28
40 juce::UndoManager* undoManager);
41
53 juce::UndoManager* undoManager, const Type& defaultToUse);
54
58 void setConstrainer (std::function<Type (Type)> constrainerToUse);
59
60 //==============================================================================
66 operator Type() const noexcept { return cachedValue; }
67
71 Type get() const noexcept { return cachedValue; }
72
74 Type& operator*() noexcept { return cachedValue; }
75
79 Type* operator->() noexcept { return &cachedValue; }
80
84 template <typename OtherType>
85 bool operator== (const OtherType& other) const { return cachedValue == other; }
86
90 template <typename OtherType>
91 bool operator!= (const OtherType& other) const { return cachedValue != other; }
92
93 //==============================================================================
96
100 bool isUsingDefault() const;
101
103 Type getDefault() const { return defaultValue; }
104
105 //==============================================================================
107 ConstrainedCachedValue& operator= (const Type& newValue);
108
110 void setValue (const Type& newValue, juce::UndoManager* undoManagerToUse);
111
116
120 void resetToDefault (juce::UndoManager* undoManagerToUse);
121
123 void setDefault (const Type& value) { defaultValue = constrainer (value); }
124
125 //==============================================================================
128
132 void referTo (juce::ValueTree& tree, const juce::Identifier& property, juce::UndoManager*, const Type& defaultVal);
133
141
142 //==============================================================================
144 juce::ValueTree& getValueTree() noexcept { return targetTree; }
145
147 const juce::Identifier& getPropertyID() const noexcept { return targetProperty; }
148
149private:
150 //==============================================================================
151 juce::ValueTree targetTree;
152 juce::Identifier targetProperty;
153 juce::UndoManager* undoManager;
154 Type defaultValue = Type();
155 Type cachedValue = Type();
156 std::function<Type (Type)> constrainer;
157
158 //==============================================================================
159 void referToWithDefault (juce::ValueTree&, const juce::Identifier&, juce::UndoManager*, const Type&);
160 Type getTypedValue() const;
161
162 void valueTreePropertyChanged (juce::ValueTree& changedTree, const juce::Identifier& changedProperty) override;
163 void valueTreeChildAdded (juce::ValueTree&, juce::ValueTree&) override {}
164 void valueTreeChildRemoved (juce::ValueTree&, juce::ValueTree&, int) override {}
165 void valueTreeChildOrderChanged (juce::ValueTree&, int, int) override {}
166 void valueTreeParentChanged (juce::ValueTree&) override {}
167
169};
170
171
172//==============================================================================
173template <typename Type>
175
176template <typename Type>
178 : targetTree (v), targetProperty (i), undoManager (um),
179 defaultValue(), cachedValue (getTypedValue())
180{
181 targetTree.addListener (this);
182}
183
184template <typename Type>
186 : targetTree (v), targetProperty (i), undoManager (um),
187 defaultValue (defaultToUse), cachedValue (getTypedValue())
188{
189 targetTree.addListener (this);
190}
191
192template <typename Type>
193inline void ConstrainedCachedValue<Type>::setConstrainer (std::function<Type (Type)> constrainerToUse)
194{
195 constrainer = std::move (constrainerToUse);
196 jassert (constrainer);
197
198 if (targetTree.isValid())
199 {
200 setValue (constrainer (cachedValue), undoManager);
201 jassert (defaultValue == constrainer (defaultValue));
202 }
203}
204
205template <typename Type>
207{
208 return targetTree.getPropertyAsValue (targetProperty, undoManager);
209}
210
211template <typename Type>
213{
214 return ! targetTree.hasProperty (targetProperty);
215}
216
217template <typename Type>
219{
220 setValue (newValue, undoManager);
221 return *this;
222}
223
224template <typename Type>
225inline void ConstrainedCachedValue<Type>::setValue (const Type& newValue, juce::UndoManager* undoManagerToUse)
226{
227 jassert (constrainer);
228 auto constrainedValue = constrainer (newValue);
229
230 if (cachedValue != constrainedValue || isUsingDefault())
231 {
232 cachedValue = constrainedValue;
233 targetTree.setProperty (targetProperty, juce::VariantConverter<Type>::toVar (constrainedValue), undoManagerToUse);
234 }
235}
236
237template <typename Type>
239{
240 resetToDefault (undoManager);
241}
242
243template <typename Type>
245{
246 targetTree.removeProperty (targetProperty, undoManagerToUse);
247 forceUpdateOfCachedValue();
248}
249
250template <typename Type>
252{
253 referToWithDefault (v, i, um, Type());
254}
255
256template <typename Type>
258{
259 referToWithDefault (v, i, um, defaultVal);
260}
261
262template <typename Type>
264{
265 cachedValue = getTypedValue();
266}
267
268template <typename Type>
270{
271 jassert (constrainer);
272 targetTree.removeListener (this);
273 targetTree = v;
274 targetProperty = i;
275 undoManager = um;
276 defaultValue = constrainer (defaultVal);
277 cachedValue = getTypedValue();
278 targetTree.addListener (this);
279}
280
281template <typename Type>
283{
284 jassert (constrainer);
285
286 if (const juce::var* property = targetTree.getPropertyPointer (targetProperty))
287 return constrainer (juce::VariantConverter<Type>::fromVar (*property));
288
289 return defaultValue;
290}
291
292template <typename Type>
293inline void ConstrainedCachedValue<Type>::valueTreePropertyChanged (juce::ValueTree& changedTree, const juce::Identifier& changedProperty)
294{
295 if (changedProperty == targetProperty && targetTree == changedTree)
296 forceUpdateOfCachedValue();
297}
298
299}} // namespace tracktion { inline namespace engine
void addListener(Listener *listener)
A CachedValue that can take a std::function to constrain its value.
bool isUsingDefault() const
Returns true if the current property does not exist and the CachedValue is using the fallback default...
ConstrainedCachedValue & operator=(const Type &newValue)
Sets the property.
void referTo(juce::ValueTree &tree, const juce::Identifier &property, juce::UndoManager *, const Type &defaultVal)
Makes the CachedValue refer to the specified property inside the given ValueTree, and specifies a fal...
Type getDefault() const
Returns the current fallback default value.
const juce::Identifier & getPropertyID() const noexcept
Returns the property ID of the referenced property.
bool operator==(const OtherType &other) const
Returns true if the current value of the property (or the fallback value) is equal to other.
bool operator!=(const OtherType &other) const
Returns true if the current value of the property (or the fallback value) is not equal to other.
void setValue(const Type &newValue, juce::UndoManager *undoManagerToUse)
Sets the property.
juce::Value getPropertyAsValue()
Returns the current property as a Value object.
Type * operator->() noexcept
Dereference operator.
void resetToDefault(juce::UndoManager *undoManagerToUse)
Removes the property from the referenced ValueTree and makes the CachedValue return the fallback defa...
void referTo(juce::ValueTree &tree, const juce::Identifier &property, juce::UndoManager *)
Makes the CachedValue refer to the specified property inside the given ValueTree.
void resetToDefault()
Removes the property from the referenced ValueTree and makes the CachedValue return the fallback defa...
ConstrainedCachedValue(juce::ValueTree &tree, const juce::Identifier &propertyID, juce::UndoManager *undoManager, const Type &defaultToUse)
Constructor.
void setDefault(const Type &value)
Resets the fallback default value.
Type get() const noexcept
Returns the current value of the property.
ConstrainedCachedValue(juce::ValueTree &tree, const juce::Identifier &propertyID, juce::UndoManager *undoManager)
Constructor.
void setConstrainer(std::function< Type(Type)> constrainerToUse)
Sets a std::function to use to constain the value.
juce::ValueTree & getValueTree() noexcept
Returns a reference to the ValueTree containing the referenced property.
void forceUpdateOfCachedValue()
Force an update in case the referenced property has been changed from elsewhere.
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)