11namespace tracktion {
inline namespace engine
18template <
typename Type>
66 operator Type() const noexcept {
return cachedValue; }
71 Type
get() const noexcept {
return cachedValue; }
84 template <
typename OtherType>
85 bool operator== (
const OtherType& other)
const {
return cachedValue == other; }
90 template <
typename OtherType>
91 bool operator!= (
const OtherType& other)
const {
return cachedValue != other; }
123 void setDefault (
const Type& value) { defaultValue = constrainer (value); }
154 Type defaultValue = Type();
155 Type cachedValue = Type();
160 Type getTypedValue()
const;
165 void valueTreeChildOrderChanged (
juce::ValueTree&,
int,
int)
override {}
173template <
typename Type>
176template <
typename Type>
178 : targetTree (v), targetProperty (i), undoManager (um),
179 defaultValue(), cachedValue (getTypedValue())
184template <
typename Type>
186 : targetTree (v), targetProperty (i), undoManager (um),
187 defaultValue (defaultToUse), cachedValue (getTypedValue())
192template <
typename Type>
195 constrainer = std::move (constrainerToUse);
198 if (targetTree.isValid())
200 setValue (constrainer (cachedValue), undoManager);
201 jassert (defaultValue == constrainer (defaultValue));
205template <
typename Type>
208 return targetTree.getPropertyAsValue (targetProperty, undoManager);
211template <
typename Type>
214 return ! targetTree.hasProperty (targetProperty);
217template <
typename Type>
220 setValue (newValue, undoManager);
224template <
typename Type>
228 auto constrainedValue = constrainer (newValue);
230 if (cachedValue != constrainedValue || isUsingDefault())
232 cachedValue = constrainedValue;
237template <
typename Type>
240 resetToDefault (undoManager);
243template <
typename Type>
246 targetTree.removeProperty (targetProperty, undoManagerToUse);
247 forceUpdateOfCachedValue();
250template <
typename Type>
253 referToWithDefault (v, i, um, Type());
256template <
typename Type>
259 referToWithDefault (v, i, um, defaultVal);
262template <
typename Type>
265 cachedValue = getTypedValue();
268template <
typename Type>
272 targetTree.removeListener (
this);
276 defaultValue = constrainer (defaultVal);
277 cachedValue = getTypedValue();
278 targetTree.addListener (
this);
281template <
typename Type>
286 if (
const juce::var* property = targetTree.getPropertyPointer (targetProperty))
292template <
typename Type>
295 if (changedProperty == targetProperty && targetTree == changedTree)
296 forceUpdateOfCachedValue();
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...
ConstrainedCachedValue()
Default constructor.
Type getDefault() const
Returns the current fallback default value.
const juce::Identifier & getPropertyID() const noexcept
Returns the property ID of the referenced property.
Type & operator*() noexcept
Dereference operator.
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.