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_CachedValue.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
26namespace juce
27{
28
29//==============================================================================
57template <typename Type>
59{
60public:
61 //==============================================================================
67
79 UndoManager* undoManager);
80
92 UndoManager* undoManager, const Type& defaultToUse);
93
94 //==============================================================================
100 operator Type() const noexcept { return cachedValue; }
101
105 Type get() const noexcept { return cachedValue; }
106
108 const Type& operator*() const noexcept { return cachedValue; }
109
113 const Type* operator->() const noexcept { return &cachedValue; }
114
118 template <typename OtherType>
119 bool operator== (const OtherType& other) const
120 {
121 return cachedValue == other;
122 }
123
127 template <typename OtherType>
128 bool operator!= (const OtherType& other) const { return ! operator== (other); }
129
130 //==============================================================================
133
137 bool isUsingDefault() const;
138
140 Type getDefault() const { return defaultValue; }
141
142 //==============================================================================
144 CachedValue& operator= (const Type& newValue);
145
147 void setValue (const Type& newValue, UndoManager* undoManagerToUse);
148
152 void resetToDefault();
153
158
160 void setDefault (const Type& value) { defaultValue = value; }
161
162 //==============================================================================
164 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
165
169 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
170
178
179 //==============================================================================
181 ValueTree& getValueTree() noexcept { return targetTree; }
182
184 const Identifier& getPropertyID() const noexcept { return targetProperty; }
185
187 UndoManager* getUndoManager() noexcept { return undoManager; }
188
189private:
190 //==============================================================================
191 ValueTree targetTree;
192 Identifier targetProperty;
193 UndoManager* undoManager = nullptr;
194 Type defaultValue;
195 Type cachedValue;
196
197 //==============================================================================
198 void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
199 Type getTypedValue() const;
200
201 void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
202
203 //==============================================================================
206};
207
208
209//==============================================================================
210template <typename Type>
211inline CachedValue<Type>::CachedValue() = default;
212
213template <typename Type>
215 : targetTree (v), targetProperty (i), undoManager (um),
216 defaultValue(), cachedValue (getTypedValue())
217{
218 targetTree.addListener (this);
219}
220
221template <typename Type>
223 : targetTree (v), targetProperty (i), undoManager (um),
224 defaultValue (defaultToUse), cachedValue (getTypedValue())
225{
226 targetTree.addListener (this);
227}
228
229template <typename Type>
231{
232 return targetTree.getPropertyAsValue (targetProperty, undoManager);
233}
234
235template <typename Type>
237{
238 return ! targetTree.hasProperty (targetProperty);
239}
240
241template <typename Type>
243{
244 setValue (newValue, undoManager);
245 return *this;
246}
247
248template <typename Type>
249inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
250{
251 if (! exactlyEqual (cachedValue, newValue) || isUsingDefault())
252 {
253 cachedValue = newValue;
254 targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
255 }
256}
257
258template <typename Type>
260{
261 resetToDefault (undoManager);
262}
263
264template <typename Type>
266{
267 targetTree.removeProperty (targetProperty, undoManagerToUse);
268 forceUpdateOfCachedValue();
269}
270
271template <typename Type>
273{
274 referToWithDefault (v, i, um, Type());
275}
276
277template <typename Type>
279{
280 referToWithDefault (v, i, um, defaultVal);
281}
282
283template <typename Type>
285{
286 cachedValue = getTypedValue();
287}
288
289template <typename Type>
291{
292 targetTree.removeListener (this);
293 targetTree = v;
294 targetProperty = i;
295 undoManager = um;
296 defaultValue = defaultVal;
297 cachedValue = getTypedValue();
298 targetTree.addListener (this);
299}
300
301template <typename Type>
302inline Type CachedValue<Type>::getTypedValue() const
303{
304 if (const var* property = targetTree.getPropertyPointer (targetProperty))
305 return VariantConverter<Type>::fromVar (*property);
306
307 return defaultValue;
308}
309
310template <typename Type>
311inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
312{
313 if (changedProperty == targetProperty && targetTree == changedTree)
314 forceUpdateOfCachedValue();
315}
316
317} // namespace juce
This class acts as a typed wrapper around a property inside a ValueTree.
bool isUsingDefault() const
Returns true if the current property does not exist and the CachedValue is using the fallback default...
Type getDefault() const
Returns the current fallback default value.
CachedValue()
Default constructor.
ValueTree & getValueTree() noexcept
Returns a reference to the ValueTree containing the referenced property.
const Identifier & getPropertyID() const noexcept
Returns the property ID of the referenced property.
CachedValue & operator=(const Type &newValue)
Sets the property.
void forceUpdateOfCachedValue()
Force an update in case the referenced property has been changed from elsewhere.
const Type & operator*() const noexcept
Dereference operator.
const Type * operator->() const noexcept
Dereference operator.
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
Sets the property.
UndoManager * getUndoManager() noexcept
Returns the UndoManager that is being used.
void resetToDefault()
Removes the property from the referenced ValueTree and makes the CachedValue return the fallback defa...
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Makes the CachedValue refer to the specified property inside the given ValueTree.
Value getPropertyAsValue()
Returns the current property as a Value object.
bool operator==(const OtherType &other) const
Returns true if the current value of the property (or the fallback value) is equal to other.
void setDefault(const Type &value)
Resets the fallback default value.
bool operator!=(const OtherType &other) const
Returns true if the current value of the property (or the fallback value) is not equal to other.
Type get() const noexcept
Returns the current value of the property.
Represents a string identifier, designed for accessing properties by name.
Manages a list of undo/redo commands.
Listener class for events that happen to a ValueTree.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
Represents a shared variant value.
Definition juce_Value.h:51
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class)
Macro to easily allow a class to be made weak-referenceable.
JUCE Namespace.
constexpr bool exactlyEqual(Type a, Type b)
Equivalent to operator==, but suppresses float-equality warnings.
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
This template-overloaded class can be used to convert between var and custom types.