From 4d375d0bc2cb2c3fedab989e2a618a2588a99e56 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 31 Oct 2017 12:17:00 +0000 Subject: [PATCH] Added a parameter to ValueTree::getPropertyAsValue() to allow synchronous updates --- .../values/juce_ValueTree.cpp | 39 +++++++++---------- .../values/juce_ValueTree.h | 5 ++- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index 05fc5a3b18..edc1fc93a5 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -32,21 +32,21 @@ class ValueTree::SharedObject : public ReferenceCountedObject public: typedef ReferenceCountedObjectPtr Ptr; - explicit SharedObject (const Identifier& t) noexcept : type (t) - { - } + explicit SharedObject (const Identifier& t) noexcept : type (t) {} SharedObject (const SharedObject& other) : ReferenceCountedObject(), type (other.type), properties (other.properties) { - for (int i = 0; i < other.children.size(); ++i) + for (auto* c : other.children) { - auto child = new SharedObject (*other.children.getObjectPointerUnchecked(i)); + auto child = new SharedObject (*c); child->parent = this; children.add (child); } } + SharedObject& operator= (const SharedObject&) = delete; + ~SharedObject() { jassert (parent == nullptr); // this should never happen unless something isn't obeying the ref-counting! @@ -98,7 +98,6 @@ public: void sendPropertyChangeMessage (const Identifier& property, ValueTree::Listener* listenerToExclude = nullptr) { ValueTree tree (this); - callListenersForAllParents ([&] (ListenerList& list) { list.callExcluding (listenerToExclude, &ValueTree::Listener::valueTreePropertyChanged, tree, property); }); } @@ -144,11 +143,13 @@ public: if (auto* existingValue = properties.getVarPointer (name)) { if (*existingValue != newValue) - undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue, false, false, listenerToExclude)); + undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue, + false, false, listenerToExclude)); } else { - undoManager->perform (new SetPropertyAction (this, name, newValue, {}, true, false, listenerToExclude)); + undoManager->perform (new SetPropertyAction (this, name, newValue, {}, + true, false, listenerToExclude)); } } } @@ -402,7 +403,7 @@ public: } else { - output.writeString (String()); + output.writeString ({}); output.writeCompressedInt (0); output.writeCompressedInt (0); } @@ -451,7 +452,7 @@ public: { if (! (isAddingNewProperty || isDeletingProperty)) { - if (SetPropertyAction* const next = dynamic_cast (nextAction)) + if (auto* next = dynamic_cast (nextAction)) if (next->target == target && next->name == name && ! (next->isAddingNewProperty || next->isDeletingProperty)) return new SetPropertyAction (target, name, next->newValue, oldValue, false, false); @@ -571,8 +572,6 @@ public: SortedSet valueTreesWithListeners; SharedObject* parent = nullptr; -private: - SharedObject& operator= (const SharedObject&); JUCE_LEAK_DETECTOR (SharedObject) }; @@ -779,12 +778,11 @@ int ValueTree::getReferenceCount() const noexcept } //============================================================================== -class ValueTreePropertyValueSource : public Value::ValueSource, - private ValueTree::Listener +struct ValueTreePropertyValueSource : public Value::ValueSource, + private ValueTree::Listener { -public: - ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um) - : tree (vt), property (prop), undoManager (um) + ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um, bool sync) + : tree (vt), property (prop), undoManager (um), updateSynchronously (sync) { tree.addListener (this); } @@ -801,11 +799,12 @@ private: ValueTree tree; const Identifier property; UndoManager* const undoManager; + const bool updateSynchronously; void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override { if (tree == changedTree && property == changedProperty) - sendChangeMessage (false); + sendChangeMessage (updateSynchronously); } void valueTreeChildAdded (ValueTree&, ValueTree&) override {} @@ -816,9 +815,9 @@ private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource) }; -Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager) +Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager, bool updateSynchronously) { - return Value (new ValueTreePropertyValueSource (*this, name, undoManager)); + return Value (new ValueTreePropertyValueSource (*this, name, undoManager, updateSynchronously)); } //============================================================================== diff --git a/modules/juce_data_structures/values/juce_ValueTree.h b/modules/juce_data_structures/values/juce_ValueTree.h index 4f73aee75f..594c4eece2 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.h +++ b/modules/juce_data_structures/values/juce_ValueTree.h @@ -209,8 +209,11 @@ public: The Value object will maintain a reference to this tree, and will use the undo manager when it needs to change the value. Attaching a Value::Listener to the value object will provide callbacks whenever the property changes. + If shouldUpdateSynchronously is true the Value::Listener will be updated synchronously. + @see ValueSource::sendChangeMessage (bool) */ - Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager); + Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager, + bool shouldUpdateSynchronously = false); /** Overwrites all the properties in this tree with the properties of the source tree. Any properties that already exist will be updated; and new ones will be added, and