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 
138  MemoryOutputStream m;
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 {
147  MemoryOutputStream m;
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 {
155  MemoryOutputStream m;
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
juce::ValueTree
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
Definition: juce_ValueTree.h:76
juce::ValueTree::addListener
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
Definition: juce_ValueTree.cpp:969
juce::ValueTree::getChild
ValueTree getChild(int index) const
Returns one of this tree's sub-trees.
Definition: juce_ValueTree.cpp:857
juce::ValueTree::removeProperty
void removeProperty(const Identifier &name, UndoManager *undoManager)
Removes a property from the tree.
Definition: juce_ValueTree.cpp:780
juce::MemoryOutputStream
Writes data to an internal memory buffer, which grows as required.
Definition: juce_MemoryOutputStream.h:39
juce::ValueTree::writeToStream
void writeToStream(OutputStream &output) const
Stores this tree (and all its children) in a binary format.
Definition: juce_ValueTree.cpp:1029
juce::var::readFromStream
static var readFromStream(InputStream &input)
Reads back a stored binary representation of a value.
Definition: juce_Variant.cpp:772
juce::UndoManager
Manages a list of undo/redo commands.
Definition: juce_UndoManager.h:56
juce::MemoryOutputStream::getData
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
Definition: juce_MemoryOutputStream.cpp:148
juce::ValueTree::getPropertyPointer
const var * getPropertyPointer(const Identifier &name) const noexcept
Returns a pointer to the value of a named property, or nullptr if the property doesn't exist.
Definition: juce_ValueTree.cpp:752
juce::ValueTree::removeListener
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
Definition: juce_ValueTree.cpp:980
juce::ValueTree::addChild
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Adds a child to this tree.
Definition: juce_ValueTree.cpp:913
juce::InputStream::readCompressedInt
virtual int readCompressedInt()
Reads an encoded 32-bit number from the stream using a space-saving compressed format.
Definition: juce_InputStream.cpp:88
juce::ValueTreeSynchroniser::stateChanged
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...
juce::OutputStream::writeString
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
Definition: juce_OutputStream.cpp:172
juce::Identifier::toString
const String & toString() const noexcept
Returns this identifier as a string.
Definition: juce_Identifier.h:106
juce::ValueTreeSynchroniser::~ValueTreeSynchroniser
~ValueTreeSynchroniser() override
Destructor.
Definition: juce_ValueTreeSynchroniser.cpp:101
juce::ValueTree::setProperty
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Changes a named property of the tree.
Definition: juce_ValueTree.cpp:758
juce::InputStream::readByte
virtual char readByte()
Reads a byte from the stream.
Definition: juce_InputStream.cpp:36
juce::ValueTreeSynchroniser::sendFullSyncCallback
void sendFullSyncCallback()
Forces the sending of a full state message, which may be large, as it encodes the entire ValueTree.
Definition: juce_ValueTreeSynchroniser.cpp:106
juce::ValueTree::isValid
bool isValid() const noexcept
Returns true if this tree refers to some valid data.
Definition: juce_ValueTree.h:179
juce::ValueTree::readFromStream
static ValueTree readFromStream(InputStream &input)
Reloads a tree from a stream that was written with writeToStream().
Definition: juce_ValueTree.cpp:1034
juce::MemoryInputStream
Allows a block of data to be accessed as a stream.
Definition: juce_MemoryInputStream.h:39
juce::Identifier
Represents a string identifier, designed for accessing properties by name.
Definition: juce_Identifier.h:42
juce::InputStream::readString
virtual String readString()
Reads a zero-terminated UTF-8 string from the stream.
Definition: juce_InputStream.cpp:162
juce::ValueTreeSynchroniser::ValueTreeSynchroniser
ValueTreeSynchroniser(const ValueTree &tree)
Creates a ValueTreeSynchroniser that watches the given tree.
Definition: juce_ValueTreeSynchroniser.cpp:96
juce::ValueTree::moveChild
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Moves one of the sub-trees to a different index.
Definition: juce_ValueTree.cpp:944
juce::ValueTreeSynchroniser::applyChange
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Applies an encoded change to the given destination tree.
Definition: juce_ValueTreeSynchroniser.cpp:164
juce::ValueTree::getNumChildren
int getNumChildren() const noexcept
Returns the number of child trees inside this one.
Definition: juce_ValueTree.cpp:852
juce::MemoryOutputStream::getDataSize
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
Definition: juce_MemoryOutputStream.h:84
juce::ValueTree::removeChild
void removeChild(const ValueTree &child, UndoManager *undoManager)
Removes the specified child from this tree's child-list.
Definition: juce_ValueTree.cpp:932