24#include "cello_update_source.h"
37 juce::Identifier
getId ()
const {
return id; }
54 const juce::Identifier
id;
85 Value (
Object& data,
const juce::Identifier& id_, T initVal = {})
93 if (!
object.hasattr (
id))
120 if (
onSet !=
nullptr)
122 const auto validated {
onSet (val) };
123 if (validated.has_value ())
124 doSet (validated.value ());
135 operator T ()
const {
return get (); }
144 if (
onGet !=
nullptr)
145 return onGet (doGet ());
169 , cachedValue {
static_cast<T
> (value) }
173 value.onPropertyChange ([
this] (
const juce::Identifier& ) { cachedValue =
static_cast<T
> (value); });
176 ~Cached () { value.onPropertyChange (
nullptr); }
183 T
get ()
const {
return cachedValue; }
190 operator T ()
const {
return get (); }
236 void excludeListener (juce::ValueTree::Listener* listener) { excludedListener = listener; }
247 void doSet (
const T& val)
249 juce::ValueTree tree {
object };
252 if (notEqualTo (val))
256 auto* excluded = (excludedListener !=
nullptr) ? excludedListener : object.getExcludedListener ();
257 const auto asVar { juce::VariantConverter<T>::toVar (val) };
259 tree.setPropertyExcludingListener (excluded,
id, asVar,
object.getUndoManager ());
261 tree.setProperty (
id, asVar,
object.getUndoManager ());
268 tree.sendPropertyChangeMessage (
id);
274 juce::ValueTree tree {
object };
275 return juce::VariantConverter<T>::fromVar (tree.getProperty (
id));
285 bool notEqualTo (
const T& newValue)
287 if constexpr (std::is_floating_point_v<T>)
288 return std::fabs (newValue - doGet ()) >
epsilon;
290 return (newValue != doGet ());
303 juce::ValueTree::Listener* excludedListener {
nullptr };
307 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
310 val = val.
get () + rhs;
315 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
318 val = val.
get () - rhs;
323 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
326 val = val.
get () * rhs;
331 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
335 val = val.get () / rhs;
348 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
351 const auto newVal { val.get () +
static_cast<int> (1) };
369 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
372 const auto original { val.get () };
373 val.set (original +
static_cast<T
> (1));
386 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
389 const auto newVal { val.get () -
static_cast<T
> (1) };
407 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
410 const auto original { val.get () };
411 val.set (original -
static_cast<T
> (1));
423#define MAKE_VALUE_MEMBER(type, name, init) \
424 static const inline juce::Identifier name##Id { #name }; \
425 cello::Value<type> name { *this, name##Id, init }
436#define CACHED_VALUE(name, value) decltype(value.getCached()) name { value }
Definition cello_object.h:34
Object & setattr(const juce::Identifier &attr, const T &attrVal)
Set a new value for the specified attribute/property. We return a reference to this object so that se...
Definition cello_object.h:528
bool shouldForceUpdate() const
Definition cello_update_source.h:44
A utility class to maintain the last known value of a cello::Value object – each call that fetches fr...
Definition cello_value.h:165
T get() const
retrieve the current value of this cached object.
Definition cello_value.h:183
Definition cello_value.h:32
const juce::Identifier id
identifier of this value/property.
Definition cello_value.h:54
juce::Identifier getId() const
Definition cello_value.h:37
ValueBase(const juce::Identifier &id_)
ctor is protected – you can't create an object of type ValueBase directly, this only exists so we hav...
Definition cello_value.h:47
A class to abstract away the issues around storing and retrieving a value from a ValueTree....
Definition cello_value.h:76
std::function< T(const T &)> ValidateGetFn
An optional validator function that can be used to modify the value when it's retrieved.
Definition cello_value.h:207
void onPropertyChange(PropertyUpdateFn callback)
Register (or clear) a callback function to execute when this value changes.
Definition cello_value.h:244
static float epsilon
Definition cello_value.h:296
T get() const
Get the current value of this property from the tree.
Definition cello_value.h:142
void excludeListener(juce::ValueTree::Listener *listener)
A listener to exclude from property change updates.
Definition cello_value.h:236
Value(Object &data, const juce::Identifier &id_, T initVal={})
Construct a new Value object.
Definition cello_value.h:85
Value & operator=(const T &val)
Assign a new value, setting it in the underlying tree and perhaps notifying listeners.
Definition cello_value.h:105
void set(const T &val)
Set property value in the tree. If the onSet validator function has been configured,...
Definition cello_value.h:118
ValidateGetFn onGet
validator function called when retrieving this Value. This function is called with the current stored...
Definition cello_value.h:229
std::function< std::optional< T >(const T &)> ValidateSetFn
an optional validator function that can be used to modify the value when it's set....
Definition cello_value.h:217
Cached getCached()
Definition cello_value.h:201
ValidateSetFn onSet
validator function called before setting this Value.
Definition cello_value.h:222