OpenShot Library | libopenshot-audio  0.2.0
juce_ValueTreeSynchroniser.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 namespace ValueTreeSynchroniserHelpers
31 {
32  enum ChangeType
33  {
34  propertyChanged = 1,
35  fullSync = 2,
36  childAdded = 3,
37  childRemoved = 4,
38  childMoved = 5,
39  propertyRemoved = 6
40  };
41 
42  static void getValueTreePath (ValueTree v, const ValueTree& topLevelTree, Array<int>& path)
43  {
44  while (v != topLevelTree)
45  {
46  ValueTree parent (v.getParent());
47 
48  if (! parent.isValid())
49  break;
50 
51  path.add (parent.indexOf (v));
52  v = parent;
53  }
54  }
55 
56  static void writeHeader (MemoryOutputStream& stream, ChangeType type)
57  {
58  stream.writeByte ((char) type);
59  }
60 
61  static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
62  ChangeType type, ValueTree v)
63  {
64  writeHeader (stream, type);
65 
66  Array<int> path;
67  getValueTreePath (v, target.getRoot(), path);
68 
69  stream.writeCompressedInt (path.size());
70 
71  for (int i = path.size(); --i >= 0;)
72  stream.writeCompressedInt (path.getUnchecked(i));
73  }
74 
75  static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
76  {
77  const int numLevels = input.readCompressedInt();
78 
79  if (! isPositiveAndBelow (numLevels, 65536)) // sanity-check
80  return {};
81 
82  for (int i = numLevels; --i >= 0;)
83  {
84  const int index = input.readCompressedInt();
85 
86  if (! isPositiveAndBelow (index, v.getNumChildren()))
87  return {};
88 
89  v = v.getChild (index);
90  }
91 
92  return v;
93  }
94 }
95 
97 {
98  valueTree.addListener (this);
99 }
100 
102 {
103  valueTree.removeListener (this);
104 }
105 
107 {
109  writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
110  valueTree.writeToStream (m);
111  stateChanged (m.getData(), m.getDataSize());
112 }
113 
114 void ValueTreeSynchroniser::valueTreePropertyChanged (ValueTree& vt, const Identifier& property)
115 {
117 
118  if (auto* value = vt.getPropertyPointer (property))
119  {
120  ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
121  m.writeString (property.toString());
122  value->writeToStream (m);
123  }
124  else
125  {
126  ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyRemoved, vt);
127  m.writeString (property.toString());
128  }
129 
130  stateChanged (m.getData(), m.getDataSize());
131 }
132 
133 void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
134 {
135  const int index = parentTree.indexOf (childTree);
136  jassert (index >= 0);
137 
139  ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
140  m.writeCompressedInt (index);
141  childTree.writeToStream (m);
142  stateChanged (m.getData(), m.getDataSize());
143 }
144 
145 void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int oldIndex)
146 {
148  ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
149  m.writeCompressedInt (oldIndex);
150  stateChanged (m.getData(), m.getDataSize());
151 }
152 
153 void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent, int oldIndex, int newIndex)
154 {
156  ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
157  m.writeCompressedInt (oldIndex);
158  m.writeCompressedInt (newIndex);
159  stateChanged (m.getData(), m.getDataSize());
160 }
161 
162 void ValueTreeSynchroniser::valueTreeParentChanged (ValueTree&) {} // (No action needed here)
163 
164 bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
165 {
166  MemoryInputStream input (data, dataSize, false);
167 
168  const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
169 
170  if (type == ValueTreeSynchroniserHelpers::fullSync)
171  {
172  root = ValueTree::readFromStream (input);
173  return true;
174  }
175 
176  ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
177 
178  if (! v.isValid())
179  return false;
180 
181  switch (type)
182  {
183  case ValueTreeSynchroniserHelpers::propertyChanged:
184  {
185  Identifier property (input.readString());
186  v.setProperty (property, var::readFromStream (input), undoManager);
187  return true;
188  }
189 
190  case ValueTreeSynchroniserHelpers::propertyRemoved:
191  {
192  Identifier property (input.readString());
193  v.removeProperty (property, undoManager);
194  return true;
195  }
196 
197  case ValueTreeSynchroniserHelpers::childAdded:
198  {
199  const int index = input.readCompressedInt();
200  v.addChild (ValueTree::readFromStream (input), index, undoManager);
201  return true;
202  }
203 
204  case ValueTreeSynchroniserHelpers::childRemoved:
205  {
206  const int index = input.readCompressedInt();
207 
208  if (isPositiveAndBelow (index, v.getNumChildren()))
209  {
210  v.removeChild (index, undoManager);
211  return true;
212  }
213 
214  jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
215  break;
216  }
217 
218  case ValueTreeSynchroniserHelpers::childMoved:
219  {
220  const int oldIndex = input.readCompressedInt();
221  const int newIndex = input.readCompressedInt();
222 
223  if (isPositiveAndBelow (oldIndex, v.getNumChildren())
224  && isPositiveAndBelow (newIndex, v.getNumChildren()))
225  {
226  v.moveChild (oldIndex, newIndex, undoManager);
227  return true;
228  }
229 
230  jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
231  break;
232  }
233 
234  default:
235  jassertfalse; // Seem to have received some corrupt data?
236  break;
237  }
238 
239  return false;
240 }
241 
242 } // namespace juce
~ValueTreeSynchroniser() override
Destructor.
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
Represents a string identifier, designed for accessing properties by name.
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&#39;s child-list.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
int getNumChildren() const noexcept
Returns the number of child trees inside this one.
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Applies an encoded change to the given destination tree.
virtual String readString()
Reads a zero-terminated UTF-8 string from the stream.
virtual int readCompressedInt()
Reads an encoded 32-bit number from the stream using a space-saving compressed format.
ValueTree getChild(int index) const
Returns one of this tree&#39;s sub-trees.
bool isValid() const noexcept
Returns true if this tree refers to some valid data.
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Changes a named property of the 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...
const String & toString() const noexcept
Returns this identifier as a string.
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Adds a child to this tree.
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Moves one of the sub-trees to a different index.
ValueTreeSynchroniser(const ValueTree &tree)
Creates a ValueTreeSynchroniser that watches the given tree.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
void sendFullSyncCallback()
Forces the sending of a full state message, which may be large, as it encodes the entire ValueTree...
virtual char readByte()
Reads a byte from the stream.
int indexOf(const ValueTree &child) const noexcept
Returns the index of a child item in this parent.
void writeToStream(OutputStream &output) const
Stores this tree (and all its children) in a binary format.
Manages a list of undo/redo commands.
const var * getPropertyPointer(const Identifier &name) const noexcept
Returns a pointer to the value of a named property, or nullptr if the property doesn&#39;t exist...
virtual bool writeCompressedInt(int value)
Writes a condensed binary encoding of a 32-bit integer.
Writes data to an internal memory buffer, which grows as required.
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
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.
Allows a block of data to be accessed as a stream.