Loading...
Searching...
No Matches
juce_CachedValue.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
template <typename Type>
class CachedValue : private ValueTree::Listener
{
public:
//==============================================================================
CachedValue();
CachedValue (ValueTree& tree, const Identifier& propertyID,
UndoManager* undoManager);
CachedValue (ValueTree& tree, const Identifier& propertyID,
UndoManager* undoManager, const Type& defaultToUse);
//==============================================================================
operator Type() const noexcept { return cachedValue; }
Type get() const noexcept { return cachedValue; }
const Type& operator*() const noexcept { return cachedValue; }
const Type* operator->() const noexcept { return &cachedValue; }
template <typename OtherType>
bool operator== (const OtherType& other) const
{
return cachedValue == other;
}
template <typename OtherType>
bool operator!= (const OtherType& other) const { return ! operator== (other); }
//==============================================================================
Value getPropertyAsValue();
bool isUsingDefault() const;
Type getDefault() const { return defaultValue; }
//==============================================================================
CachedValue& operator= (const Type& newValue);
void setValue (const Type& newValue, UndoManager* undoManagerToUse);
void resetToDefault();
void resetToDefault (UndoManager* undoManagerToUse);
void setDefault (const Type& value) { defaultValue = value; }
//==============================================================================
void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
void forceUpdateOfCachedValue();
//==============================================================================
ValueTree& getValueTree() noexcept { return targetTree; }
const Identifier& getPropertyID() const noexcept { return targetProperty; }
UndoManager* getUndoManager() noexcept { return undoManager; }
private:
//==============================================================================
ValueTree targetTree;
Identifier targetProperty;
UndoManager* undoManager = nullptr;
Type defaultValue;
Type cachedValue;
//==============================================================================
void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
Type getTypedValue() const;
void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
//==============================================================================
JUCE_DECLARE_WEAK_REFERENCEABLE (CachedValue)
JUCE_DECLARE_NON_COPYABLE (CachedValue)
};
//==============================================================================
template <typename Type>
inline CachedValue<Type>::CachedValue() = default;
template <typename Type>
inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um)
: targetTree (v), targetProperty (i), undoManager (um),
defaultValue(), cachedValue (getTypedValue())
{
targetTree.addListener (this);
}
template <typename Type>
inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultToUse)
: targetTree (v), targetProperty (i), undoManager (um),
defaultValue (defaultToUse), cachedValue (getTypedValue())
{
targetTree.addListener (this);
}
template <typename Type>
inline Value CachedValue<Type>::getPropertyAsValue()
{
return targetTree.getPropertyAsValue (targetProperty, undoManager);
}
template <typename Type>
inline bool CachedValue<Type>::isUsingDefault() const
{
return ! targetTree.hasProperty (targetProperty);
}
template <typename Type>
inline CachedValue<Type>& CachedValue<Type>::operator= (const Type& newValue)
{
setValue (newValue, undoManager);
return *this;
}
template <typename Type>
inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
{
if (! exactlyEqual (cachedValue, newValue) || isUsingDefault())
{
cachedValue = newValue;
targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
}
}
template <typename Type>
inline void CachedValue<Type>::resetToDefault()
{
resetToDefault (undoManager);
}
template <typename Type>
inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
{
targetTree.removeProperty (targetProperty, undoManagerToUse);
forceUpdateOfCachedValue();
}
template <typename Type>
inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um)
{
referToWithDefault (v, i, um, Type());
}
template <typename Type>
inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
{
referToWithDefault (v, i, um, defaultVal);
}
template <typename Type>
inline void CachedValue<Type>::forceUpdateOfCachedValue()
{
cachedValue = getTypedValue();
}
template <typename Type>
inline void CachedValue<Type>::referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
{
targetTree.removeListener (this);
targetTree = v;
targetProperty = i;
undoManager = um;
defaultValue = defaultVal;
cachedValue = getTypedValue();
targetTree.addListener (this);
}
template <typename Type>
inline Type CachedValue<Type>::getTypedValue() const
{
if (const var* property = targetTree.getPropertyPointer (targetProperty))
return VariantConverter<Type>::fromVar (*property);
return defaultValue;
}
template <typename Type>
inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
{
if (changedProperty == targetProperty && targetTree == changedTree)
forceUpdateOfCachedValue();
}
} // namespace juce
juce::CachedValue
Definition juce_CachedValue.h:59
juce::CachedValue::isUsingDefault
bool isUsingDefault() const
Definition juce_CachedValue.h:236
juce::CachedValue::getDefault
Type getDefault() const
Definition juce_CachedValue.h:140
juce::CachedValue::CachedValue
CachedValue()
juce::CachedValue::getValueTree
ValueTree & getValueTree() noexcept
Definition juce_CachedValue.h:181
juce::CachedValue::getPropertyID
const Identifier & getPropertyID() const noexcept
Definition juce_CachedValue.h:184
juce::CachedValue::operator=
CachedValue & operator=(const Type &newValue)
Definition juce_CachedValue.h:242
juce::CachedValue::forceUpdateOfCachedValue
void forceUpdateOfCachedValue()
Definition juce_CachedValue.h:284
juce::CachedValue::operator*
const Type & operator*() const noexcept
Definition juce_CachedValue.h:108
juce::CachedValue::operator->
const Type * operator->() const noexcept
Definition juce_CachedValue.h:113
juce::CachedValue::setValue
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
Definition juce_CachedValue.h:249
juce::CachedValue::getUndoManager
UndoManager * getUndoManager() noexcept
Definition juce_CachedValue.h:187
juce::CachedValue::resetToDefault
void resetToDefault()
Definition juce_CachedValue.h:259
juce::CachedValue::referTo
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Definition juce_CachedValue.h:272
juce::CachedValue::getPropertyAsValue
Value getPropertyAsValue()
Definition juce_CachedValue.h:230
juce::CachedValue::operator==
bool operator==(const OtherType &other) const
Definition juce_CachedValue.h:119
juce::CachedValue::setDefault
void setDefault(const Type &value)
Definition juce_CachedValue.h:160
juce::CachedValue::operator!=
bool operator!=(const OtherType &other) const
Definition juce_CachedValue.h:128
juce::CachedValue::get
Type get() const noexcept
Definition juce_CachedValue.h:105
juce::Identifier
Definition juce_Identifier.h:39
juce::UndoManager
Definition juce_UndoManager.h:52
juce::ValueTree::Listener
Definition juce_ValueTree.h:481
juce::ValueTree
Definition juce_ValueTree.h:72
juce::Value
Definition juce_Value.h:51