22#include "cello_update_source.h"
35 juce::Identifier
getId ()
const {
return id; }
52 const juce::Identifier
id;
83 Value (
Object& data,
const juce::Identifier& id_, T initVal = {})
91 if (!
object.hasattr (
id))
118 if (
onSet !=
nullptr)
129 operator T ()
const {
return get (); }
138 if (
onGet !=
nullptr)
139 return onGet (doGet ());
163 , cachedValue {
static_cast<T
> (value) }
167 value.onPropertyChange ([
this] (juce::Identifier ) { cachedValue =
static_cast<T
> (value); });
170 ~Cached () { value.onPropertyChange (
nullptr); }
172 operator T ()
const {
return cachedValue; }
211 void excludeListener (juce::ValueTree::Listener* listener) { excludedListener = listener; }
222 void doSet (
const T& val)
224 juce::ValueTree tree {
object };
227 if (notEqualTo (val))
231 auto* excluded = (excludedListener !=
nullptr) ? excludedListener : object.getExcludedListener ();
232 const auto asVar { juce::VariantConverter<T>::toVar (val) };
234 tree.setPropertyExcludingListener (excluded,
id, asVar,
object.getUndoManager ());
236 tree.setProperty (
id, asVar,
object.getUndoManager ());
243 tree.sendPropertyChangeMessage (
id);
249 juce::ValueTree tree {
object };
250 return juce::VariantConverter<T>::fromVar (tree.getProperty (
id));
260 bool notEqualTo (
const T& newValue)
262 if constexpr (std::is_floating_point_v<T>)
263 return std::fabs (newValue - doGet ()) >
epsilon;
265 return (newValue != doGet ());
278 juce::ValueTree::Listener* excludedListener {
nullptr };
282 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
285 const auto current {
static_cast<T
> (val) };
291 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
294 const auto current {
static_cast<T
> (val) };
300 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
303 const auto current {
static_cast<T
> (val) };
309 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
313 const auto current {
static_cast<T
> (val) };
327 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
330 const auto newVal {
static_cast<T
> (val) +
static_cast<int> (1) };
348 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
351 const auto original {
static_cast<T
> (val) };
352 val.set (original +
static_cast<T
> (1));
365 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
368 const auto newVal {
static_cast<T
> (val) -
static_cast<T
> (1) };
386 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
389 const auto original {
static_cast<T
> (val) };
390 val.set (original -
static_cast<T
> (1));
401#define MAKE_VALUE_MEMBER(type, name, init) \
402 cello::Value<type> name \
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:159
Definition cello_value.h:30
const juce::Identifier id
identifier of this value/property.
Definition cello_value.h:52
juce::Identifier getId() const
Definition cello_value.h:35
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:45
A class to abstract away the issues around storing and retrieving a value from a ValueTree....
Definition cello_value.h:74
void onPropertyChange(PropertyUpdateFn callback)
Register (or clear) a callback function to execute when this value changes.
Definition cello_value.h:219
static float epsilon
Definition cello_value.h:271
T get() const
Get the current value of this property from the tree.
Definition cello_value.h:136
void excludeListener(juce::ValueTree::Listener *listener)
A listener to exclude from property change updates.
Definition cello_value.h:211
Value(Object &data, const juce::Identifier &id_, T initVal={})
Construct a new Value object.
Definition cello_value.h:83
Value & operator=(const T &val)
Assign a new value, setting it in the underlying tree and perhaps notifying listeners.
Definition cello_value.h:103
void set(const T &val)
Set property value in the tree. If the onSet validator function has been configured,...
Definition cello_value.h:116
std::function< T(const T &)> ValidatePropertyFn
We define the signature of a 'validator' function that can validate/modify/replace values as your app...
Definition cello_value.h:192
ValidatePropertyFn onSet
validator function called before setting this Value.
Definition cello_value.h:197
ValidatePropertyFn onGet
validator function called when retrieving this Value. This function is called with the current stored...
Definition cello_value.h:204
Cached getCached()
Definition cello_value.h:183