OpenShot Audio Library | OpenShotAudio  0.3.1
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 bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
163 {
164  MemoryInputStream input (data, dataSize, false);
165 
166  const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
167 
168  if (type == ValueTreeSynchroniserHelpers::fullSync)
169  {
170  root = ValueTree::readFromStream (input);
171  return true;
172  }
173 
174  ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
175 
176  if (! v.isValid())
177  return false;
178 
179  switch (type)
180  {
181  case ValueTreeSynchroniserHelpers::propertyChanged:
182  {
183  Identifier property (input.readString());
184  v.setProperty (property, var::readFromStream (input), undoManager);
185  return true;
186  }
187 
188  case ValueTreeSynchroniserHelpers::propertyRemoved:
189  {
190  Identifier property (input.readString());
191  v.removeProperty (property, undoManager);
192  return true;
193  }
194 
195  case ValueTreeSynchroniserHelpers::childAdded:
196  {
197  const int index = input.readCompressedInt();
198  v.addChild (ValueTree::readFromStream (input), index, undoManager);
199  return true;
200  }
201 
202  case ValueTreeSynchroniserHelpers::childRemoved:
203  {
204  const int index = input.readCompressedInt();
205 
206  if (isPositiveAndBelow (index, v.getNumChildren()))
207  {
208  v.removeChild (index, undoManager);
209  return true;
210  }
211 
212  jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
213  break;
214  }
215 
216  case ValueTreeSynchroniserHelpers::childMoved:
217  {
218  const int oldIndex = input.readCompressedInt();
219  const int newIndex = input.readCompressedInt();
220 
221  if (isPositiveAndBelow (oldIndex, v.getNumChildren())
222  && isPositiveAndBelow (newIndex, v.getNumChildren()))
223  {
224  v.moveChild (oldIndex, newIndex, undoManager);
225  return true;
226  }
227 
228  jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
229  break;
230  }
231 
232  default:
233  jassertfalse; // Seem to have received some corrupt data?
234  break;
235  }
236 
237  return false;
238 }
239 
240 } // namespace juce
void addListener(Listener *listener)
static ValueTree readFromStream(InputStream &input)
void removeChild(const ValueTree &child, UndoManager *undoManager)
void removeListener(Listener *listener)
int getNumChildren() const noexcept
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
virtual String readString()
virtual int readCompressedInt()
ValueTree getChild(int index) const
bool isValid() const noexcept
const void * getData() const noexcept
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
const String & toString() const noexcept
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
ValueTreeSynchroniser(const ValueTree &tree)
size_t getDataSize() const noexcept
virtual char readByte()
int indexOf(const ValueTree &child) const noexcept
void writeToStream(OutputStream &output) const
const var * getPropertyPointer(const Identifier &name) const noexcept
virtual bool writeCompressedInt(int value)
virtual bool writeString(const String &text)
void removeProperty(const Identifier &name, UndoManager *undoManager)
static var readFromStream(InputStream &input)