JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::Value Class Referencefinal

Represents a shared variant value. More...

#include <juce_Value.h>

Collaboration diagram for juce::Value:

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

Private Member Functions

 Value (void *)=delete
 
void callListeners ()
 
Valueoperator= (const Value &)=delete
 
void removeFromListenerList ()
 

Private Attributes

ListenerList< Listenerlisteners
 
ReferenceCountedObjectPtr< ValueSourcevalue
 

Friends

class ValueSource
 

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.

Important note! The Value class is not thread-safe! If you're accessing one from multiple threads, then you'll need to use your own synchronisation around any code that accesses it.

@tags{DataStructures}

Constructor & Destructor Documentation

◆ Value() [1/6]

juce::Value::Value ( )

Creates an empty Value, containing a void var.

◆ Value() [2/6]

juce::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() [3/6]

juce::Value::Value ( const var initialValue)
explicit

Creates a Value that is set to the specified value.

◆ Value() [4/6]

juce::Value::Value ( Value &&  )
noexcept

Move constructor.

◆ ~Value()

juce::Value::~Value ( )

Destructor.

◆ Value() [5/6]

juce::Value::Value ( ValueSource valueSource)
explicit

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

◆ Value() [6/6]

juce::Value::Value ( void *  )
explicitprivatedelete

Member Function Documentation

◆ addListener()

void juce::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 Value 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 Value, but more likely to one that's a member variable.

See also
removeListener

Referenced by juce::StandaloneFilterWindow::MainContentComponent::MainContentComponent(), and juce::StandalonePluginHolder::StandalonePluginHolder().

◆ callListeners()

void juce::Value::callListeners ( )
private

◆ getValue()

◆ getValueSource()

ValueSource& juce::Value::getValueSource ( )
inlinenoexcept

Returns the ValueSource that this value is referring to.

References juce::gl::value.

◆ operator var()

juce::Value::operator var ( ) const

Returns the current value.

◆ operator!=()

bool juce::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()).

◆ operator=() [1/3]

Value& juce::Value::operator= ( const Value )
privatedelete

◆ operator=() [2/3]

Value& juce::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.

◆ operator=() [3/3]

Value& juce::Value::operator= ( Value &&  )
noexcept

Move assignment operator.

◆ operator==()

bool juce::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()).

◆ refersToSameSourceAs()

bool juce::Value::refersToSameSourceAs ( const Value other) const

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

◆ referTo()

void juce::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.

Referenced by juce::StandaloneFilterWindow::MainContentComponent::MainContentComponent(), and juce::StandalonePluginHolder::SettingsComponent::SettingsComponent().

◆ removeFromListenerList()

void juce::Value::removeFromListenerList ( )
private

◆ removeListener()

void juce::Value::removeListener ( Listener listener)

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

◆ setValue()

void juce::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.

Referenced by juce::StandalonePluginHolder::reloadAudioDeviceState().

◆ toString()

String juce::Value::toString ( ) const

Returns the value as a string.

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

Friends And Related Function Documentation

◆ ValueSource

friend class ValueSource
friend

Member Data Documentation

◆ listeners

ListenerList<Listener> juce::Value::listeners
private

◆ value

ReferenceCountedObjectPtr<ValueSource> juce::Value::value
private

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