Browse Source

Fix for Value move operators.

tags/2021-05-28
jules 11 years ago
parent
commit
b052208cf4
2 changed files with 22 additions and 10 deletions
  1. +21
    -10
      modules/juce_data_structures/values/juce_Value.cpp
  2. +1
    -0
      modules/juce_data_structures/values/juce_Value.h

+ 21
- 10
modules/juce_data_structures/values/juce_Value.cpp View File

@@ -94,24 +94,20 @@ private:
//==============================================================================
Value::Value()
: value (new SimpleValueSource())
Value::Value() : value (new SimpleValueSource())
{
}
Value::Value (ValueSource* const v)
: value (v)
Value::Value (ValueSource* const v) : value (v)
{
jassert (v != nullptr);
}
Value::Value (const var& initialValue)
: value (new SimpleValueSource (initialValue))
Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
{
}
Value::Value (const Value& other)
: value (other.value)
Value::Value (const Value& other) : value (other.value)
{
}
@@ -123,12 +119,22 @@ Value& Value::operator= (const Value& other)
#if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
Value::Value (Value&& other) noexcept
: value (static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value))
{
// moving a Value with listeners will lose those listeners, which
// probably isn't what you wanted to happen!
jassert (other.listeners.size() == 0);
other.removeFromListenerList();
value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
}
Value& Value::operator= (Value&& other) noexcept
{
// moving a Value with listeners will lose those listeners, which
// probably isn't what you wanted to happen!
jassert (other.listeners.size() == 0);
other.removeFromListenerList();
value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
return *this;
}
@@ -136,7 +142,12 @@ Value& Value::operator= (Value&& other) noexcept
Value::~Value()
{
if (listeners.size() > 0)
removeFromListenerList();
}
void Value::removeFromListenerList()
{
if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
value->valuesWithListeners.removeValue (this);
}


+ 1
- 0
modules/juce_data_structures/values/juce_Value.h View File

@@ -217,6 +217,7 @@ private:
ListenerList<Listener> listeners;
void callListeners();
void removeFromListenerList();
// This is disallowed to avoid confusion about whether it should
// do a by-value or by-reference copy.


Loading…
Cancel
Save