29namespace ValueTreeSynchroniserHelpers
41 static void getValueTreePath (ValueTree v,
const ValueTree&
topLevelTree, Array<int>& path)
45 ValueTree parent (v.getParent());
47 if (! parent.isValid())
50 path.add (parent.indexOf (v));
55 static void writeHeader (MemoryOutputStream& stream, ChangeType type)
57 stream.writeByte ((
char) type);
60 static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
61 ChangeType type, ValueTree v)
63 writeHeader (stream, type);
66 getValueTreePath (v, target.getRoot(), path);
68 stream.writeCompressedInt (path.size());
70 for (
int i = path.size(); --i >= 0;)
71 stream.writeCompressedInt (path.getUnchecked (i));
74 static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
76 const int numLevels = input.readCompressedInt();
83 const int index = input.readCompressedInt();
108 writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
117 if (
auto* value =
vt.getPropertyPointer (property))
119 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::propertyChanged,
vt);
121 value->writeToStream (m);
125 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::propertyRemoved,
vt);
132void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree&
parentTree, ValueTree&
childTree)
137 MemoryOutputStream m;
138 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childAdded,
parentTree);
139 m.writeCompressedInt (index);
144void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree&
parentTree, ValueTree&,
int oldIndex)
146 MemoryOutputStream m;
147 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childRemoved,
parentTree);
152void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent,
int oldIndex,
int newIndex)
154 MemoryOutputStream m;
155 ValueTreeSynchroniserHelpers::writeHeader (*
this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
165 const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.
readByte();
167 if (type == ValueTreeSynchroniserHelpers::fullSync)
173 ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
180 case ValueTreeSynchroniserHelpers::propertyChanged:
187 case ValueTreeSynchroniserHelpers::propertyRemoved:
194 case ValueTreeSynchroniserHelpers::childAdded:
201 case ValueTreeSynchroniserHelpers::childRemoved:
215 case ValueTreeSynchroniserHelpers::childMoved:
231 case ValueTreeSynchroniserHelpers::fullSync:
Represents a string identifier, designed for accessing properties by name.
const String & toString() const noexcept
Returns this identifier as a string.
Writes data to an internal memory buffer, which grows as required.
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
Manages a list of undo/redo commands.
~ValueTreeSynchroniser() override
Destructor.
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Applies an encoded change to the given destination tree.
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
This callback happens when the ValueTree changes and the given state-change message needs to be appli...
ValueTreeSynchroniser(const ValueTree &tree)
Creates a ValueTreeSynchroniser that watches the given tree.
void sendFullSyncCallback()
Forces the sending of a full state message, which may be large, as it encodes the entire ValueTree.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
static ValueTree readFromStream(InputStream &input)
Reloads a tree from a stream that was written with writeToStream().
void removeChild(const ValueTree &child, UndoManager *undoManager)
Removes the specified child from this tree's child-list.
ValueTree getChild(int index) const
Returns one of this tree's sub-trees.
int getNumChildren() const noexcept
Returns the number of child trees inside this one.
bool isValid() const noexcept
Returns true if this tree refers to some valid data.
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Changes a named property of the tree.
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Adds a child to this tree.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
void writeToStream(OutputStream &output) const
Stores this tree (and all its children) in a binary format.
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Moves one of the sub-trees to a different index.
void removeProperty(const Identifier &name, UndoManager *undoManager)
Removes a property from the tree.
static var readFromStream(InputStream &input)
Reads back a stored binary representation of a value.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.