OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
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
96 ValueTreeSynchroniser::ValueTreeSynchroniser (const ValueTree& tree) : valueTree (tree)
97{
98 valueTree.addListener (this);
99}
100
101 ValueTreeSynchroniser::~ValueTreeSynchroniser()
102{
103 valueTree.removeListener (this);
104}
105
106 void ValueTreeSynchroniser::sendFullSyncCallback()
107{
108 MemoryOutputStream m;
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{
116 MemoryOutputStream m;
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 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
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:53
juce::ValueTreeSynchroniser::~ValueTreeSynchroniser
~ValueTreeSynchroniser() override
Definition: juce_ValueTreeSynchroniser.cpp:101
juce::ValueTreeSynchroniser::applyChange
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Definition: juce_ValueTreeSynchroniser.cpp:162
juce::ValueTreeSynchroniser::stateChanged
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
juce::ValueTreeSynchroniser::ValueTreeSynchroniser
ValueTreeSynchroniser(const ValueTree &tree)
Definition: juce_ValueTreeSynchroniser.cpp:96
juce::ValueTreeSynchroniser::sendFullSyncCallback
void sendFullSyncCallback()
Definition: juce_ValueTreeSynchroniser.cpp:106
juce::ValueTree
Definition: juce_ValueTree.h:73
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:932
juce::ValueTree::getChild
ValueTree getChild(int index) const
Definition: juce_ValueTree.cpp:857
juce::ValueTree::getNumChildren
int getNumChildren() const noexcept
Definition: juce_ValueTree.cpp:852
juce::ValueTree::isValid
bool isValid() const noexcept
Definition: juce_ValueTree.h:175
juce::ValueTree::getPropertyPointer
const var * getPropertyPointer(const Identifier &name) const noexcept
Definition: juce_ValueTree.cpp:752
juce::ValueTree::setProperty
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:758
juce::ValueTree::addListener
void addListener(Listener *listener)
Definition: juce_ValueTree.cpp:969
juce::ValueTree::addChild
void addChild(const ValueTree &child, int index, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:913
juce::ValueTree::removeListener
void removeListener(Listener *listener)
Definition: juce_ValueTree.cpp:980
juce::ValueTree::writeToStream
void writeToStream(OutputStream &output) const
Definition: juce_ValueTree.cpp:1035
juce::ValueTree::moveChild
void moveChild(int currentIndex, int newIndex, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:944
juce::ValueTree::removeProperty
void removeProperty(const Identifier &name, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:780
juce::var::readFromStream
static var readFromStream(InputStream &input)
Definition: juce_Variant.cpp:772