JUCE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Classes | Public Member Functions | List of all members
Value Class Reference

Represents a shared variant value. More...

Classes

class  Listener
 Receives callbacks when a Value object changes. More...
 
class  ValueSource
 Used internally by the Value class as the base class for its shared value objects. More...
 

Public Member Functions

 Value ()
 Creates an empty Value, containing a void var.
 
 Value (const Value &other)
 Creates a Value that refers to the same value as another one.
 
 Value (const var &initialValue)
 Creates a Value that is set to the specified value.
 
 ~Value ()
 Destructor.
 
var getValue () const
 Returns the current value.
 
 operator var () const
 Returns the current value.
 
String toString () const
 Returns the value as a string.
 
void setValue (const var &newValue)
 Sets the current value.
 
Valueoperator= (const var &newValue)
 Sets the current value.
 
void referTo (const Value &valueToReferTo)
 Makes this object refer to the same underlying ValueSource as another one.
 
bool refersToSameSourceAs (const Value &other) const
 Returns true if this value and the other one are references to the same value.
 
bool operator== (const Value &other) const
 Compares two values.
 
bool operator!= (const Value &other) const
 Compares two values.
 
void addListener (Listener *listener)
 Adds a listener to receive callbacks when the value changes.
 
void removeListener (Listener *listener)
 Removes a listener that was previously added with addListener().
 
 Value (ValueSource *valueSource)
 Creates a Value object that uses this valueSource object as its underlying data.
 
ValueSourcegetValueSource () noexcept
 Returns the ValueSource that this value is referring to.
 

Detailed Description

Represents a shared variant value.

A Value object contains a reference to a var object, and can get and set its value. Listeners can be attached to be told when the value is changed.

The Value class is a wrapper around a shared, reference-counted underlying data object - this means that multiple Value objects can all refer to the same piece of data, allowing all of them to be notified when any of them changes it.

When you create a Value with its default constructor, it acts as a wrapper around a simple var object, but by creating a Value that refers to a custom subclass of ValueSource, you can map the Value onto any kind of underlying data.

Constructor & Destructor Documentation

Value::Value ( )

Creates an empty Value, containing a void var.

Value::Value ( const Value other)

Creates a Value that refers to the same value as another one.

Note that this doesn't make a copy of the other value - both this and the other Value will share the same underlying value, so that when either one alters it, both will see it change.

Value::Value ( const var initialValue)
explicit

Creates a Value that is set to the specified value.

Value::~Value ( )

Destructor.

Value::Value ( ValueSource valueSource)
explicit

Creates a Value object that uses this valueSource object as its underlying data.

Member Function Documentation

var Value::getValue ( ) const

Returns the current value.

Value::operator var ( ) const

Returns the current value.

String Value::toString ( ) const

Returns the value as a string.

This is a shortcut for "myValue.getValue().toString()".

void Value::setValue ( const var newValue)

Sets the current value.

You can also use operator= to set the value.

If there are any listeners registered, they will be notified of the change asynchronously.

Value& Value::operator= ( const var newValue)

Sets the current value.

This is the same as calling setValue().

If there are any listeners registered, they will be notified of the change asynchronously.

void Value::referTo ( const Value valueToReferTo)

Makes this object refer to the same underlying ValueSource as another one.

Once this object has been connected to another one, changing either one will update the other.

Existing listeners will still be registered after you call this method, and they'll continue to receive messages when the new value changes.

bool Value::refersToSameSourceAs ( const Value other) const

Returns true if this value and the other one are references to the same value.

bool Value::operator== ( const Value other) const

Compares two values.

This is a compare-by-value comparison, so is effectively the same as saying (this->getValue() == other.getValue()).

bool Value::operator!= ( const Value other) const

Compares two values.

This is a compare-by-value comparison, so is effectively the same as saying (this->getValue() != other.getValue()).

void Value::addListener ( Listener listener)

Adds a listener to receive callbacks when the value changes.

The listener is added to this specific Value object, and not to the shared object that it refers to. When this object is deleted, all the listeners will be lost, even if other references to the same Value still exist. So when you're adding a listener, make sure that you add it to a ValueTree instance that will last for as long as you need the listener. In general, you'd never want to add a listener to a local stack-based ValueTree, but more likely to one that's a member variable.

See Also
removeListener
void Value::removeListener ( Listener listener)

Removes a listener that was previously added with addListener().

ValueSource& Value::getValueSource ( )
noexcept

Returns the ValueSource that this value is referring to.


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