Loading...
Searching...
No Matches
juce_ValueTreeSynchroniser.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
namespace ValueTreeSynchroniserHelpers
{
enum ChangeType
{
propertyChanged = 1,
fullSync = 2,
childAdded = 3,
childRemoved = 4,
childMoved = 5,
propertyRemoved = 6
};
static void getValueTreePath (ValueTree v, const ValueTree& topLevelTree, Array<int>& path)
{
while (v != topLevelTree)
{
ValueTree parent (v.getParent());
if (! parent.isValid())
break;
path.add (parent.indexOf (v));
v = parent;
}
}
static void writeHeader (MemoryOutputStream& stream, ChangeType type)
{
stream.writeByte ((char) type);
}
static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
ChangeType type, ValueTree v)
{
writeHeader (stream, type);
Array<int> path;
getValueTreePath (v, target.getRoot(), path);
stream.writeCompressedInt (path.size());
for (int i = path.size(); --i >= 0;)
stream.writeCompressedInt (path.getUnchecked (i));
}
static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
{
const int numLevels = input.readCompressedInt();
if (! isPositiveAndBelow (numLevels, 65536)) // sanity-check
return {};
for (int i = numLevels; --i >= 0;)
{
const int index = input.readCompressedInt();
if (! isPositiveAndBelow (index, v.getNumChildren()))
return {};
v = v.getChild (index);
}
return v;
}
}
ValueTreeSynchroniser::ValueTreeSynchroniser (const ValueTree& tree) : valueTree (tree)
{
valueTree.addListener (this);
}
ValueTreeSynchroniser::~ValueTreeSynchroniser()
{
valueTree.removeListener (this);
}
void ValueTreeSynchroniser::sendFullSyncCallback()
{
MemoryOutputStream m;
writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
valueTree.writeToStream (m);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreePropertyChanged (ValueTree& vt, const Identifier& property)
{
MemoryOutputStream m;
if (auto* value = vt.getPropertyPointer (property))
{
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
m.writeString (property.toString());
value->writeToStream (m);
}
else
{
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyRemoved, vt);
m.writeString (property.toString());
}
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
{
const int index = parentTree.indexOf (childTree);
jassert (index >= 0);
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
m.writeCompressedInt (index);
childTree.writeToStream (m);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int oldIndex)
{
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
m.writeCompressedInt (oldIndex);
stateChanged (m.getData(), m.getDataSize());
}
void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent, int oldIndex, int newIndex)
{
MemoryOutputStream m;
ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
m.writeCompressedInt (oldIndex);
m.writeCompressedInt (newIndex);
stateChanged (m.getData(), m.getDataSize());
}
bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
{
MemoryInputStream input (data, dataSize, false);
const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
if (type == ValueTreeSynchroniserHelpers::fullSync)
{
root = ValueTree::readFromStream (input);
return true;
}
ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
if (! v.isValid())
return false;
switch (type)
{
case ValueTreeSynchroniserHelpers::propertyChanged:
{
Identifier property (input.readString());
v.setProperty (property, var::readFromStream (input), undoManager);
return true;
}
case ValueTreeSynchroniserHelpers::propertyRemoved:
{
Identifier property (input.readString());
v.removeProperty (property, undoManager);
return true;
}
case ValueTreeSynchroniserHelpers::childAdded:
{
const int index = input.readCompressedInt();
v.addChild (ValueTree::readFromStream (input), index, undoManager);
return true;
}
case ValueTreeSynchroniserHelpers::childRemoved:
{
const int index = input.readCompressedInt();
if (isPositiveAndBelow (index, v.getNumChildren()))
{
v.removeChild (index, undoManager);
return true;
}
jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
break;
}
case ValueTreeSynchroniserHelpers::childMoved:
{
const int oldIndex = input.readCompressedInt();
const int newIndex = input.readCompressedInt();
if (isPositiveAndBelow (oldIndex, v.getNumChildren())
&& isPositiveAndBelow (newIndex, v.getNumChildren()))
{
v.moveChild (oldIndex, newIndex, undoManager);
return true;
}
jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
break;
}
case ValueTreeSynchroniserHelpers::fullSync:
break;
default:
jassertfalse; // Seem to have received some corrupt data?
break;
}
return false;
}
} // namespace juce
juce::Identifier
Definition juce_Identifier.h:39
juce::Identifier::toString
const String & toString() const noexcept
Definition juce_Identifier.h:102
juce::InputStream::readCompressedInt
virtual int readCompressedInt()
Definition juce_InputStream.cpp:108
juce::InputStream::readString
virtual String readString()
Definition juce_InputStream.cpp:182
juce::InputStream::readByte
virtual char readByte()
Definition juce_InputStream.cpp:56
juce::MemoryInputStream
Definition juce_MemoryInputStream.h:36
juce::MemoryOutputStream
Definition juce_MemoryOutputStream.h:36
juce::MemoryOutputStream::getData
const void * getData() const noexcept
Definition juce_MemoryOutputStream.cpp:148
juce::MemoryOutputStream::getDataSize
size_t getDataSize() const noexcept
Definition juce_MemoryOutputStream.h:80
juce::OutputStream::writeString
virtual bool writeString(const String &text)
Definition juce_OutputStream.cpp:183
juce::UndoManager
Definition juce_UndoManager.h:52
juce::ValueTreeSynchroniser::~ValueTreeSynchroniser
~ValueTreeSynchroniser() override
Definition juce_ValueTreeSynchroniser.cpp:100
juce::ValueTreeSynchroniser::applyChange
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Definition juce_ValueTreeSynchroniser.cpp:161
juce::ValueTreeSynchroniser::stateChanged
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
juce::ValueTreeSynchroniser::ValueTreeSynchroniser
ValueTreeSynchroniser(const ValueTree &tree)
Definition juce_ValueTreeSynchroniser.cpp:95
juce::ValueTreeSynchroniser::sendFullSyncCallback
void sendFullSyncCallback()
Definition juce_ValueTreeSynchroniser.cpp:105
juce::ValueTree
Definition juce_ValueTree.h:72
juce::ValueTree::readFromStream
static ValueTree readFromStream(InputStream &input)
Definition juce_ValueTree.cpp:1040
juce::ValueTree::removeChild
void removeChild(const ValueTree &child, UndoManager *undoManager)
Definition juce_ValueTree.cpp:935
juce::ValueTree::getNumChildren
int getNumChildren() const noexcept
Definition juce_ValueTree.cpp:855
juce::ValueTree::isValid
bool isValid() const noexcept
Definition juce_ValueTree.h:176
juce::ValueTree::getPropertyPointer
const var * getPropertyPointer(const Identifier &name) const noexcept
Definition juce_ValueTree.cpp:755
juce::ValueTree::setProperty
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Definition juce_ValueTree.cpp:761
juce::ValueTree::addChild
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Definition juce_ValueTree.cpp:916
juce::ValueTree::moveChild
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Definition juce_ValueTree.cpp:947
juce::ValueTree::removeProperty
void removeProperty(const Identifier &name, UndoManager *undoManager)
Definition juce_ValueTree.cpp:783
juce::var::readFromStream
static var readFromStream(InputStream &input)
Definition juce_Variant.cpp:826