LMMS
Loading...
Searching...
No Matches
juce_ValueTreeSynchroniser.cpp
Go to the documentation of this file.
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
30{
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
56 {
57 stream.writeByte ((char) type);
58 }
59
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
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
96{
97 valueTree.addListener (this);
98}
99
101{
102 valueTree.removeListener (this);
103}
104
106{
109 valueTree.writeToStream (m);
110 stateChanged (m.getData(), m.getDataSize());
111}
112
114{
116
117 if (auto* value = vt.getPropertyPointer (property))
118 {
120 m.writeString (property.toString());
121 value->writeToStream (m);
122 }
123 else
124 {
126 m.writeString (property.toString());
127 }
128
129 stateChanged (m.getData(), m.getDataSize());
130}
131
133{
134 const int index = parentTree.indexOf (childTree);
135 jassert (index >= 0);
136
139 m.writeCompressedInt (index);
140 childTree.writeToStream (m);
141 stateChanged (m.getData(), m.getDataSize());
142}
143
145{
148 m.writeCompressedInt (oldIndex);
149 stateChanged (m.getData(), m.getDataSize());
150}
151
153{
156 m.writeCompressedInt (oldIndex);
157 m.writeCompressedInt (newIndex);
158 stateChanged (m.getData(), m.getDataSize());
159}
160
161bool ValueTreeSynchroniser::applyChange (ValueTree& root, const void* data, size_t dataSize, UndoManager* undoManager)
162{
163 MemoryInputStream input (data, dataSize, false);
164
166
168 {
169 root = ValueTree::readFromStream (input);
170 return true;
171 }
172
174
175 if (! v.isValid())
176 return false;
177
178 switch (type)
179 {
181 {
182 Identifier property (input.readString());
183 v.setProperty (property, var::readFromStream (input), undoManager);
184 return true;
185 }
186
188 {
189 Identifier property (input.readString());
190 v.removeProperty (property, undoManager);
191 return true;
192 }
193
195 {
196 const int index = input.readCompressedInt();
197 v.addChild (ValueTree::readFromStream (input), index, undoManager);
198 return true;
199 }
200
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
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
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
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_Array.h:56
ElementType getUnchecked(int index) const
Definition juce_Array.h:252
int size() const noexcept
Definition juce_Array.h:215
void add(const ElementType &newElement)
Definition juce_Array.h:418
Definition juce_Identifier.h:39
const String & toString() const noexcept
Definition juce_Identifier.h:102
virtual int readCompressedInt()
Definition juce_InputStream.cpp:108
virtual String readString()
Definition juce_InputStream.cpp:182
virtual char readByte()
Definition juce_InputStream.cpp:56
Definition juce_MemoryInputStream.h:36
Definition juce_MemoryOutputStream.h:36
virtual bool writeCompressedInt(int value)
Definition juce_OutputStream.cpp:121
virtual bool writeByte(char byte)
Definition juce_OutputStream.cpp:83
Definition juce_UndoManager.h:52
Definition juce_ValueTree.h:72
static ValueTree readFromStream(InputStream &input)
Definition juce_ValueTree.cpp:1034
const var * getPropertyPointer(const Identifier &name) const noexcept
Definition juce_ValueTree.cpp:749
int indexOf(const ValueTree &child) const noexcept
Definition juce_ValueTree.cpp:905
void writeToStream(OutputStream &output) const
Definition juce_ValueTree.cpp:1029
Definition juce_ValueTreeSynchroniser.h:46
~ValueTreeSynchroniser() override
Definition juce_ValueTreeSynchroniser.cpp:100
static bool applyChange(ValueTree &target, const void *encodedChangeData, size_t encodedChangeDataSize, UndoManager *undoManager)
Definition juce_ValueTreeSynchroniser.cpp:161
const ValueTree & getRoot() noexcept
Definition juce_ValueTreeSynchroniser.h:84
virtual void stateChanged(const void *encodedChange, size_t encodedChangeSize)=0
void valueTreeChildAdded(ValueTree &, ValueTree &) override
Definition juce_ValueTreeSynchroniser.cpp:132
ValueTreeSynchroniser(const ValueTree &tree)
Definition juce_ValueTreeSynchroniser.cpp:95
void valueTreePropertyChanged(ValueTree &, const Identifier &) override
Definition juce_ValueTreeSynchroniser.cpp:113
void valueTreeChildOrderChanged(ValueTree &, int, int) override
Definition juce_ValueTreeSynchroniser.cpp:152
void valueTreeChildRemoved(ValueTree &, ValueTree &, int) override
Definition juce_ValueTreeSynchroniser.cpp:144
void sendFullSyncCallback()
Definition juce_ValueTreeSynchroniser.cpp:105
ValueTree valueTree
Definition juce_ValueTreeSynchroniser.h:87
static var readFromStream(InputStream &input)
Definition juce_Variant.cpp:838
unsigned * m
Definition inflate.c:1559
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
static uintptr_t parent
Definition pugl.h:1644
JSAMPIMAGE data
Definition jpeglib.h:945
#define jassert(expression)
#define jassertfalse
Definition juce_ValueTreeSynchroniser.cpp:30
static void writeHeader(MemoryOutputStream &stream, ChangeType type)
Definition juce_ValueTreeSynchroniser.cpp:55
static void getValueTreePath(ValueTree v, const ValueTree &topLevelTree, Array< int > &path)
Definition juce_ValueTreeSynchroniser.cpp:41
static ValueTree readSubTreeLocation(MemoryInputStream &input, ValueTree v)
Definition juce_ValueTreeSynchroniser.cpp:74
ChangeType
Definition juce_ValueTreeSynchroniser.cpp:32
@ childRemoved
Definition juce_ValueTreeSynchroniser.cpp:36
@ fullSync
Definition juce_ValueTreeSynchroniser.cpp:34
@ childAdded
Definition juce_ValueTreeSynchroniser.cpp:35
@ childMoved
Definition juce_ValueTreeSynchroniser.cpp:37
@ propertyChanged
Definition juce_ValueTreeSynchroniser.cpp:33
@ propertyRemoved
Definition juce_ValueTreeSynchroniser.cpp:38
Definition carla_juce.cpp:31
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:279
@ tree
Definition juce_AccessibilityRole.h:58