![]() |
cello
JUCE ValueTrees for Humans
|
A Value-like object that lets us use parts of the API we have for Value objects (the get and set parts), generating a computed value based on whatever is needed for your application. More...
#include <cello_computed_value.h>
Public Types | |
| using | SetImplFn = std::function<void (const T&)> |
| The type of the function that will be called to set the value of the computed value. | |
| using | GetImplFn = std::function<T ()> |
Public Member Functions | |
| ComputedValue (Object &object, const juce::Identifier &id, GetImplFn getImpl=nullptr, SetImplFn setImpl=nullptr) | |
| ComputedValue & | operator= (const T &val) |
| Assignment operator for the computed value. | |
| void | set (const T &val) |
| Set the value of the computed value. | |
| operator T () const | |
| Conversion operator to the type of the computed value. | |
| T | get () const |
| Get the current computed value. | |
Public Member Functions inherited from cello::ValueBase | |
| juce::Identifier | getId () const |
Public Member Functions inherited from cello::UpdateSource | |
| void | forceUpdate (bool shouldForceUpdate) |
| If passed true, any call that sets any Value property on this Object will result in a property change update callback being executed. Default (false) behavior only performs this callback when the underlying value is changed. | |
| bool | shouldForceUpdate () const |
Public Attributes | |
| GetImplFn | getImpl |
| SetImplFn | setImpl |
Additional Inherited Members | |
Protected Member Functions inherited from cello::ValueBase | |
| ValueBase (const juce::Identifier &id_) | |
ctor is protected – you can't create an object of type ValueBase directly, this only exists so we have a common base type that's shared by all the templated Value<T> classes. | |
Protected Attributes inherited from cello::ValueBase | |
| const juce::Identifier | id |
| identifier of this value/property. | |
A Value-like object that lets us use parts of the API we have for Value objects (the get and set parts), generating a computed value based on whatever is needed for your application.
There's a (required) lambda for computing the value that's called when we perform a get() operation.
You can provide a separate lambda to be called when setting the value; this should perform the reverse operation of the get lambda. As a simple example, consider a case where we store data in metric units, but want to display it in imperial units. We can create a ComputedValue<double> with a get lambda that converts the data from metric to imperial, and a set lambda that converts the data from imperial to metric.
This class is (intentionally) simpler than a Value object, in that it doesn't permit listening to changes in the computed value. If you need to know when the computed value changes, add a listener to the Value used as the source(s) of the computed value.
This also doesn't support the onSet and onGet validation functions that are in Value objects; any validation that you need to perform should be done in the get and set lambdas. Obviously, if your setImpl() lambda updates another Value and your computation would make that Value invalid according to its onSet() validator, the underlying Value will remain in its valid state as determined by its onSet() validator.
| T | The type of the computed value. |
|
inline |
Get the current computed value.
|
inline |
Conversion operator to the type of the computed value.
|
inline |
Assignment operator for the computed value.
| val | The new value to assign to the computed value. |
this.x = this.y = 42;setImpl lambda if it is set, otherwise it will assert.
|
inline |
Set the value of the computed value.
| val | The new value to set the computed value to. |
setImpl lambda if it is set, otherwise it will assert. Note that the assertion does not necessarily mean "hey, you forgot to set the setImpl lambda!", it means "hey, this
is a read-only computed value, and you're trying to set it."