24#include "cello_update_source.h"
37 juce::Identifier
getId ()
const {
return id; }
54 const juce::Identifier
id;
112 if (!
object.hasattr (
id))
139 if (
onSet !=
nullptr)
141 const auto validated {
onSet (val) };
142 if (validated.has_value ())
143 doSet (validated.value ());
154 operator T ()
const {
return get (); }
163 if (
onGet !=
nullptr)
164 return onGet (doGet ());
188 , cachedValue {
static_cast<T
> (value) }
192 value.onPropertyChange ([
this] (
const juce::Identifier& ) { cachedValue =
static_cast<T
> (value); });
195 ~Cached () { value.onPropertyChange (
nullptr); }
202 T
get ()
const {
return cachedValue; }
209 operator T ()
const {
return get (); }
239 void excludeListener (juce::ValueTree::Listener* listener) { excludedListener = listener; }
250 void doSet (
const T& val)
252 juce::ValueTree tree {
object };
255 if (notEqualTo (val))
259 auto* excluded = (excludedListener !=
nullptr) ? excludedListener : object.getExcludedListener ();
260 const auto asVar { juce::VariantConverter<T>::toVar (val) };
262 tree.setPropertyExcludingListener (excluded,
id, asVar,
object.getUndoManager ());
264 tree.setProperty (
id, asVar,
object.getUndoManager ());
271 tree.sendPropertyChangeMessage (
id);
277 juce::ValueTree tree {
object };
278 return juce::VariantConverter<T>::fromVar (tree.getProperty (
id));
288 bool notEqualTo (
const T& newValue)
290 if constexpr (std::is_floating_point_v<T>)
291 return std::fabs (newValue - doGet ()) >
epsilon;
293 return (newValue != doGet ());
306 juce::ValueTree::Listener* excludedListener {
nullptr };
310 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
313 val = val.
get () + rhs;
318 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
321 val = val.
get () - rhs;
326 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
329 val = val.
get () * rhs;
334 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
338 val = val.get () / rhs;
351 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
354 const auto newVal { val.get () +
static_cast<int> (1) };
372 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
375 const auto original { val.get () };
376 val.set (original +
static_cast<T
> (1));
389 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
392 const auto newVal { val.get () -
static_cast<T
> (1) };
410 typename =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
413 const auto original { val.get () };
414 val.set (original -
static_cast<T
> (1));
426#define MAKE_VALUE_MEMBER(type, name, init) \
427 static const inline juce::Identifier name##Id { #name }; \
428 cello::Value<type> name { *this, name##Id, init }
431#define MAKE_VALUE_MEMBER_GET(type, name, init, getFn) \
432 static const inline juce::Identifier name##Id { #name }; \
433 cello::Value<type> name { *this, name##Id, init, getFn }
436#define MAKE_VALUE_MEMBER_GET_SET(type, name, init, getFn, setFn) \
437 static const inline juce::Identifier name##Id { #name }; \
438 cello::Value<type> name { *this, name##Id, init, getFn, setFn }
450#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:537
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:184
T get() const
retrieve the current value of this cached object.
Definition cello_value.h:202
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
Value(Object &data, const juce::Identifier &id_, T initVal={}, ValidateGetFn getFn=nullptr, ValidateSetFn setFn=nullptr)
Construct a new Value object.
Definition cello_value.h:101
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:82
void onPropertyChange(PropertyUpdateFn callback)
Register (or clear) a callback function to execute when this value changes.
Definition cello_value.h:247
static float epsilon
Definition cello_value.h:299
T get() const
Get the current value of this property from the tree.
Definition cello_value.h:161
void excludeListener(juce::ValueTree::Listener *listener)
A listener to exclude from property change updates.
Definition cello_value.h:239
Value & operator=(const T &val)
Assign a new value, setting it in the underlying tree and perhaps notifying listeners.
Definition cello_value.h:124
void set(const T &val)
Set property value in the tree. If the onSet validator function has been configured,...
Definition cello_value.h:137
ValidateGetFn onGet
validator function called when retrieving this Value. This function is called with the current stored...
Definition cello_value.h:232
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:92
Cached getCached()
Definition cello_value.h:220
ValidateSetFn onSet
validator function called before setting this Value.
Definition cello_value.h:225