OpenShot Audio Library | OpenShotAudio: juce_ValueTreeSynchroniser.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_ValueTreeSynchroniser.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29namespace ValueTreeSynchroniserHelpers
30{
31 enum ChangeType
32 {
33 propertyChanged = 1,
34 fullSync = 2,
35 childAdded = 3,
36 childRemoved = 4,
37 childMoved = 5,
38 propertyRemoved = 6
39 };
40
41 static void getValueTreePath (ValueTree v, const ValueTree& topLevelTree, Array<int>& path)
42 {
43 while (v != topLevelTree)
44 {
45 ValueTree parent (v.getParent());
46
47 if (! parent.isValid())
48 break;
49
50 path.add (parent.indexOf (v));
51 v = parent;
52 }
53 }
54
55 static void writeHeader (MemoryOutputStream& stream, ChangeType type)
56 {
57 stream.writeByte ((char) type);
58 }
59
60 static void writeHeader (ValueTreeSynchroniser& target, MemoryOutputStream& stream,
61 ChangeType type, ValueTree v)
62 {
63 writeHeader (stream, type);
64
65 Array<int> path;
66 getValueTreePath (v, target.getRoot(), path);
67
68 stream.writeCompressedInt (path.size());
69
70 for (int i = path.size(); --i >= 0;)
71 stream.writeCompressedInt (path.getUnchecked (i));
72 }
73
74 static ValueTree readSubTreeLocation (MemoryInputStream& input, ValueTree v)
75 {
76 const int numLevels = input.readCompressedInt();
77
78 if (! isPositiveAndBelow (numLevels, 65536)) // sanity-check
79 return {};
80
81 for (int i = numLevels; --i >= 0;)
82 {
83 const int index = input.readCompressedInt();
84
85 if (! isPositiveAndBelow (index, v.getNumChildren()))
86 return {};
87
88 v = v.getChild (index);
89 }
90
91 return v;
92 }
93}
94
95ValueTreeSynchroniser::ValueTreeSynchroniser (const ValueTree& tree) : valueTree (tree)
96{
97 valueTree.addListener (this);
98}
99
100ValueTreeSynchroniser::~ValueTreeSynchroniser()
101{
102 valueTree.removeListener (this);
103}
104
105 void ValueTreeSynchroniser::sendFullSyncCallback()
106{
107 MemoryOutputStream m;
108 writeHeader (m, ValueTreeSynchroniserHelpers::fullSync);
109 valueTree.writeToStream (m);
110 stateChanged (m.getData(), m.getDataSize());
111}
112
113void ValueTreeSynchroniser::valueTreePropertyChanged (ValueTree& vt, const Identifier& property)
114{
115 MemoryOutputStream m;
116
117 if (auto* value = vt.getPropertyPointer (property))
118 {
119 ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyChanged, vt);
120 m.writeString (property.toString());
121 value->writeToStream (m);
122 }
123 else
124 {
125 ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::propertyRemoved, vt);
126 m.writeString (property.toString());
127 }
128
129 stateChanged (m.getData(), m.getDataSize());
130}
131
132void ValueTreeSynchroniser::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childTree)
133{
134 const int index = parentTree.indexOf (childTree);
135 jassert (index >= 0);
136
137 MemoryOutputStream m;
138 ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childAdded, parentTree);
139 m.writeCompressedInt (index);
140 childTree.writeToStream (m);
141 stateChanged (m.getData(), m.getDataSize());
142}
143
144void ValueTreeSynchroniser::valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int oldIndex)
145{
146 MemoryOutputStream m;
147 ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childRemoved, parentTree);
148 m.writeCompressedInt (oldIndex);
149 stateChanged (m.getData(), m.getDataSize());
150}
151
152void ValueTreeSynchroniser::valueTreeChildOrderChanged (ValueTree& parent, int oldIndex, int newIndex)
153{
154 MemoryOutputStream m;
155 ValueTreeSynchroniserHelpers::writeHeader (*this, m, ValueTreeSynchroniserHelpers::childMoved, parent);
156 m.writeCompressedInt (oldIndex);
157 m.writeCompressedInt (newIndex);
158 stateChanged (m.getData(), m.getDataSize());
159}
160
161 bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
162{
163 MemoryInputStream input (data, dataSize, false);
164
165 const ValueTreeSynchroniserHelpers::ChangeType type = (ValueTreeSynchroniserHelpers::ChangeType) input.readByte();
166
167 if (type == ValueTreeSynchroniserHelpers::fullSync)
168 {
169 root = ValueTree::readFromStream (input);
170 return true;
171 }
172
173 ValueTree v (ValueTreeSynchroniserHelpers::readSubTreeLocation (input, root));
174
175 if (! v.isValid())
176 return false;
177
178 switch (type)
179 {
180 case ValueTreeSynchroniserHelpers::propertyChanged:
181 {
182 Identifier property (input.readString());
183 v.setProperty (property, var::readFromStream (input), undoManager);
184 return true;
185 }
186
187 case ValueTreeSynchroniserHelpers::propertyRemoved:
188 {
189 Identifier property (input.readString());
190 v.removeProperty (property, undoManager);
191 return true;
192 }
193
194 case ValueTreeSynchroniserHelpers::childAdded:
195 {
196 const int index = input.readCompressedInt();
197 v.addChild (ValueTree::readFromStream (input), index, undoManager);
198 return true;
199 }
200
201 case ValueTreeSynchroniserHelpers::childRemoved:
202 {
203 const int index = input.readCompressedInt();
204
205 if (isPositiveAndBelow (index, v.getNumChildren()))
206 {
207 v.removeChild (index, undoManager);
208 return true;
209 }
210
211 jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
212 break;
213 }
214
215 case ValueTreeSynchroniserHelpers::childMoved:
216 {
217 const int oldIndex = input.readCompressedInt();
218 const int newIndex = input.readCompressedInt();
219
220 if (isPositiveAndBelow (oldIndex, v.getNumChildren())
221 && isPositiveAndBelow (newIndex, v.getNumChildren()))
222 {
223 v.moveChild (oldIndex, newIndex, undoManager);
224 return true;
225 }
226
227 jassertfalse; // Either received some corrupt data, or the trees have drifted out of sync
228 break;
229 }
230
231 case ValueTreeSynchroniserHelpers::fullSync:
232 break;
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::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:52
juce::ValueTreeSynchroniser::~ValueTreeSynchroniser
~ValueTreeSynchroniser() override
Definition juce_ValueTreeSynchroniser.cpp:100
juce::ValueTreeSynchroniser::applyChange
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Definition juce_ValueTreeSynchroniser.cpp:161
juce::ValueTreeSynchroniser::stateChanged
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
juce::ValueTreeSynchroniser::ValueTreeSynchroniser
ValueTreeSynchroniser(const ValueTree &tree)
Definition juce_ValueTreeSynchroniser.cpp:95
juce::ValueTreeSynchroniser::sendFullSyncCallback
void sendFullSyncCallback()
Definition juce_ValueTreeSynchroniser.cpp:105
juce::ValueTree
Definition juce_ValueTree.h:72
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:935
juce::ValueTree::getNumChildren
int getNumChildren() const noexcept
Definition juce_ValueTree.cpp:855
juce::ValueTree::isValid
bool isValid() const noexcept
Definition juce_ValueTree.h:176
juce::ValueTree::getPropertyPointer
const var * getPropertyPointer(const Identifier &name) const noexcept
Definition juce_ValueTree.cpp:755
juce::ValueTree::setProperty
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Definition juce_ValueTree.cpp:761
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:916
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:947
juce::ValueTree::removeProperty
void removeProperty(const Identifier &name, UndoManager *undoManager)
Definition juce_ValueTree.cpp:783
juce::var::readFromStream
static var readFromStream(InputStream &input)
Definition juce_Variant.cpp:826