OpenShot Audio Library | OpenShotAudio: juce_NamedValueSet.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.1

Loading...
Searching...
No Matches
juce_NamedValueSet.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 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26NamedValueSet::NamedValue::NamedValue() noexcept {}
27NamedValueSet::NamedValue::~NamedValue() noexcept {}
28
29NamedValueSet::NamedValue::NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
30NamedValueSet::NamedValue::NamedValue (const NamedValue& other) : NamedValue (other.name, other.value) {}
31
32NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
33 : NamedValue (std::move (other.name),
34 std::move (other.value))
35{}
36
37NamedValueSet::NamedValue::NamedValue (const Identifier& n, var&& v) noexcept
38 : name (n), value (std::move (v))
39{
40}
41
42NamedValueSet::NamedValue::NamedValue (Identifier&& n, var&& v) noexcept
43 : name (std::move (n)),
44 value (std::move (v))
45{}
46
47NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
48{
49 name = std::move (other.name);
50 value = std::move (other.value);
51 return *this;
52}
53
54bool NamedValueSet::NamedValue::operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
55bool NamedValueSet::NamedValue::operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
56
57//==============================================================================
58NamedValueSet::NamedValueSet() noexcept {}
59NamedValueSet::~NamedValueSet() noexcept {}
60
61NamedValueSet::NamedValueSet (const NamedValueSet& other) : values (other.values) {}
62
63 NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
64 : values (std::move (other.values)) {}
65
66 NamedValueSet::NamedValueSet (std::initializer_list<NamedValue> list)
67 : values (std::move (list))
68{
69}
70
71 NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
72{
73 clear();
74 values = other.values;
75 return *this;
76}
77
78NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
79{
80 other.values.swapWith (values);
81 return *this;
82}
83
84 void NamedValueSet::clear()
85{
86 values.clear();
87}
88
89 bool NamedValueSet::operator== (const NamedValueSet& other) const noexcept
90{
91 auto num = values.size();
92
93 if (num != other.values.size())
94 return false;
95
96 for (int i = 0; i < num; ++i)
97 {
98 // optimise for the case where the keys are in the same order
99 if (values.getReference(i).name == other.values.getReference(i).name)
100 {
101 if (values.getReference(i).value != other.values.getReference(i).value)
102 return false;
103 }
104 else
105 {
106 // if we encounter keys that are in a different order, search remaining items by brute force..
107 for (int j = i; j < num; ++j)
108 {
109 if (auto* otherVal = other.getVarPointer (values.getReference(j).name))
110 if (values.getReference(j).value == *otherVal)
111 continue;
112
113 return false;
114 }
115
116 return true;
117 }
118 }
119
120 return true;
121}
122
123bool NamedValueSet::operator!= (const NamedValueSet& other) const noexcept { return ! operator== (other); }
124
125 int NamedValueSet::size() const noexcept { return values.size(); }
126 bool NamedValueSet::isEmpty() const noexcept { return values.isEmpty(); }
127
128static const var& getNullVarRef() noexcept
129{
130 static var nullVar;
131 return nullVar;
132}
133
134 const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
135{
136 if (auto* v = getVarPointer (name))
137 return *v;
138
139 return getNullVarRef();
140}
141
142 var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
143{
144 if (auto* v = getVarPointer (name))
145 return *v;
146
147 return defaultReturnValue;
148}
149
150 var* NamedValueSet::getVarPointer (const Identifier& name) noexcept
151{
152 for (auto& i : values)
153 if (i.name == name)
154 return &(i.value);
155
156 return {};
157}
158
159 const var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
160{
161 for (auto& i : values)
162 if (i.name == name)
163 return &(i.value);
164
165 return {};
166}
167
168 bool NamedValueSet::set (const Identifier& name, var&& newValue)
169{
170 if (auto* v = getVarPointer (name))
171 {
172 if (v->equalsWithSameType (newValue))
173 return false;
174
175 *v = std::move (newValue);
176 return true;
177 }
178
179 values.add ({ name, std::move (newValue) });
180 return true;
181}
182
183 bool NamedValueSet::set (const Identifier& name, const var& newValue)
184{
185 if (auto* v = getVarPointer (name))
186 {
187 if (v->equalsWithSameType (newValue))
188 return false;
189
190 *v = newValue;
191 return true;
192 }
193
194 values.add ({ name, newValue });
195 return true;
196}
197
198 bool NamedValueSet::contains (const Identifier& name) const noexcept
199{
200 return getVarPointer (name) != nullptr;
201}
202
203 int NamedValueSet::indexOf (const Identifier& name) const noexcept
204{
205 auto numValues = values.size();
206
207 for (int i = 0; i < numValues; ++i)
208 if (values.getReference(i).name == name)
209 return i;
210
211 return -1;
212}
213
214 bool NamedValueSet::remove (const Identifier& name)
215{
216 auto numValues = values.size();
217
218 for (int i = 0; i < numValues; ++i)
219 {
220 if (values.getReference(i).name == name)
221 {
222 values.remove (i);
223 return true;
224 }
225 }
226
227 return false;
228}
229
230 Identifier NamedValueSet::getName (const int index) const noexcept
231{
232 if (isPositiveAndBelow (index, values.size()))
233 return values.getReference (index).name;
234
235 jassertfalse;
236 return {};
237}
238
239 const var& NamedValueSet::getValueAt (const int index) const noexcept
240{
241 if (isPositiveAndBelow (index, values.size()))
242 return values.getReference (index).value;
243
244 jassertfalse;
245 return getNullVarRef();
246}
247
248 var* NamedValueSet::getVarPointerAt (int index) noexcept
249{
250 if (isPositiveAndBelow (index, values.size()))
251 return &(values.getReference (index).value);
252
253 return {};
254}
255
256 const var* NamedValueSet::getVarPointerAt (int index) const noexcept
257{
258 if (isPositiveAndBelow (index, values.size()))
259 return &(values.getReference (index).value);
260
261 return {};
262}
263
264 void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
265{
266 values.clearQuick();
267
268 for (auto* att = xml.attributes.get(); att != nullptr; att = att->nextListItem)
269 {
270 if (att->name.toString().startsWith ("base64:"))
271 {
272 MemoryBlock mb;
273
274 if (mb.fromBase64Encoding (att->value))
275 {
276 values.add ({ att->name.toString().substring (7), var (mb) });
277 continue;
278 }
279 }
280
281 values.add ({ att->name, var (att->value) });
282 }
283}
284
285 void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
286{
287 for (auto& i : values)
288 {
289 if (auto* mb = i.value.getBinaryData())
290 {
291 xml.setAttribute ("base64:" + i.name.toString(), mb->toBase64Encoding());
292 }
293 else
294 {
295 // These types can't be stored as XML!
296 jassert (! i.value.isObject());
297 jassert (! i.value.isMethod());
298 jassert (! i.value.isArray());
299
300 xml.setAttribute (i.name.toString(),
301 i.value.toString());
302 }
303 }
304}
305
306} // namespace juce
juce::Identifier
Definition: juce_Identifier.h:39
juce::LinkedListPointer::get
ObjectType * get() const noexcept
Definition: juce_LinkedListPointer.h:102
juce::MemoryBlock
Definition: juce_MemoryBlock.h:33
juce::MemoryBlock::fromBase64Encoding
bool fromBase64Encoding(StringRef encodedString)
Definition: juce_MemoryBlock.cpp:379
juce::NamedValueSet
Definition: juce_NamedValueSet.h:35
juce::NamedValueSet::getVarPointerAt
var * getVarPointerAt(int index) noexcept
Definition: juce_NamedValueSet.cpp:248
juce::NamedValueSet::set
bool set(const Identifier &name, const var &newValue)
Definition: juce_NamedValueSet.cpp:183
juce::NamedValueSet::contains
bool contains(const Identifier &name) const noexcept
Definition: juce_NamedValueSet.cpp:198
juce::NamedValueSet::remove
bool remove(const Identifier &name)
Definition: juce_NamedValueSet.cpp:214
juce::NamedValueSet::indexOf
int indexOf(const Identifier &name) const noexcept
Definition: juce_NamedValueSet.cpp:203
juce::NamedValueSet::getValueAt
const var & getValueAt(int index) const noexcept
Definition: juce_NamedValueSet.cpp:239
juce::NamedValueSet::clear
void clear()
Definition: juce_NamedValueSet.cpp:84
juce::NamedValueSet::isEmpty
bool isEmpty() const noexcept
Definition: juce_NamedValueSet.cpp:126
juce::NamedValueSet::operator==
bool operator==(const NamedValueSet &) const noexcept
Definition: juce_NamedValueSet.cpp:89
juce::NamedValueSet::NamedValueSet
NamedValueSet() noexcept
Definition: juce_NamedValueSet.cpp:58
juce::NamedValueSet::getName
Identifier getName(int index) const noexcept
Definition: juce_NamedValueSet.cpp:230
juce::NamedValueSet::getWithDefault
var getWithDefault(const Identifier &name, const var &defaultReturnValue) const
Definition: juce_NamedValueSet.cpp:142
juce::NamedValueSet::size
int size() const noexcept
Definition: juce_NamedValueSet.cpp:125
juce::NamedValueSet::operator[]
const var & operator[](const Identifier &name) const noexcept
Definition: juce_NamedValueSet.cpp:134
juce::NamedValueSet::getVarPointer
var * getVarPointer(const Identifier &name) noexcept
Definition: juce_NamedValueSet.cpp:150
juce::NamedValueSet::copyToXmlAttributes
void copyToXmlAttributes(XmlElement &xml) const
Definition: juce_NamedValueSet.cpp:285
juce::NamedValueSet::setFromXmlAttributes
void setFromXmlAttributes(const XmlElement &xml)
Definition: juce_NamedValueSet.cpp:264
juce::XmlElement
Definition: juce_XmlElement.h:138
juce::XmlElement::setAttribute
void setAttribute(const Identifier &attributeName, const String &newValue)
Definition: juce_XmlElement.cpp:615
juce::var
Definition: juce_Variant.h:42