cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello::ComputedValue< T > Class Template Reference

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>

Inheritance diagram for cello::ComputedValue< T >:
cello::ValueBase cello::UpdateSource

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)
 
ComputedValueoperator= (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.
 
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.
 

Detailed Description

template<typename T>
class cello::ComputedValue< T >

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.

Template Parameters
TThe type of the computed value.

Member Function Documentation

◆ get()

template<typename T>
T cello::ComputedValue< T >::get ( ) const
inline

Get the current computed value.

Returns
T The current computed value. If no getImpl lambda is set, this will will assert (in a debug build) and return a default T value (in release builds)

◆ operator T()

template<typename T>
cello::ComputedValue< T >::operator T ( ) const
inline

Conversion operator to the type of the computed value.

Returns
T The current computed value.

◆ operator=()

template<typename T>
ComputedValue & cello::ComputedValue< T >::operator= ( const T & val)
inline

Assignment operator for the computed value.

Parameters
valThe new value to assign to the computed value.
Returns
ComputedValue& reference to this value so you could e.g. do this.x = this.y = 42;
Note
This operator will call the setImpl lambda if it is set, otherwise it will assert.

◆ set()

template<typename T>
void cello::ComputedValue< T >::set ( const T & val)
inline

Set the value of the computed value.

Parameters
valThe new value to set the computed value to.
Note
This operator will call the 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."

The documentation for this class was generated from the following file: