OpenShot Library | libopenshot-audio 0.2.0
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
63NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
64 : values (std::move (other.values)) {}
65
66NamedValueSet::NamedValueSet (std::initializer_list<NamedValue> list)
67 : values (std::move (list))
68{
69}
70
71NamedValueSet& 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
85{
86 values.clear();
87}
88
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
125int NamedValueSet::size() const noexcept { return values.size(); }
126bool NamedValueSet::isEmpty() const noexcept { return values.isEmpty(); }
127
128static const var& getNullVarRef() noexcept
129{
130 static var nullVar;
131 return nullVar;
132}
133
134const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
135{
136 if (auto* v = getVarPointer (name))
137 return *v;
138
139 return getNullVarRef();
140}
141
143{
144 if (auto* v = getVarPointer (name))
145 return *v;
146
147 return defaultReturnValue;
148}
149
150var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
151{
152 for (auto& i : values)
153 if (i.name == name)
154 return &(i.value);
155
156 return {};
157}
158
159bool NamedValueSet::set (const Identifier& name, var&& newValue)
160{
161 if (auto* v = getVarPointer (name))
162 {
163 if (v->equalsWithSameType (newValue))
164 return false;
165
166 *v = std::move (newValue);
167 return true;
168 }
169
170 values.add ({ name, std::move (newValue) });
171 return true;
172}
173
174bool NamedValueSet::set (const Identifier& name, const var& newValue)
175{
176 if (auto* v = getVarPointer (name))
177 {
178 if (v->equalsWithSameType (newValue))
179 return false;
180
181 *v = newValue;
182 return true;
183 }
184
185 values.add ({ name, newValue });
186 return true;
187}
188
189bool NamedValueSet::contains (const Identifier& name) const noexcept
190{
191 return getVarPointer (name) != nullptr;
192}
193
194int NamedValueSet::indexOf (const Identifier& name) const noexcept
195{
196 auto numValues = values.size();
197
198 for (int i = 0; i < numValues; ++i)
199 if (values.getReference(i).name == name)
200 return i;
201
202 return -1;
203}
204
206{
207 auto numValues = values.size();
208
209 for (int i = 0; i < numValues; ++i)
210 {
211 if (values.getReference(i).name == name)
212 {
213 values.remove (i);
214 return true;
215 }
216 }
217
218 return false;
219}
220
221Identifier NamedValueSet::getName (const int index) const noexcept
222{
223 if (isPositiveAndBelow (index, values.size()))
224 return values.getReference (index).name;
225
226 jassertfalse;
227 return {};
228}
229
230const var& NamedValueSet::getValueAt (const int index) const noexcept
231{
232 if (isPositiveAndBelow (index, values.size()))
233 return values.getReference (index).value;
234
235 jassertfalse;
236 return getNullVarRef();
237}
238
239var* NamedValueSet::getVarPointerAt (int index) const noexcept
240{
241 if (isPositiveAndBelow (index, values.size()))
242 return &(values.getReference (index).value);
243
244 return {};
245}
246
248{
249 values.clearQuick();
250
251 for (auto* att = xml.attributes.get(); att != nullptr; att = att->nextListItem)
252 {
253 if (att->name.toString().startsWith ("base64:"))
254 {
256
257 if (mb.fromBase64Encoding (att->value))
258 {
259 values.add ({ att->name.toString().substring (7), var (mb) });
260 continue;
261 }
262 }
263
264 values.add ({ att->name, var (att->value) });
265 }
266}
267
269{
270 for (auto& i : values)
271 {
272 if (auto* mb = i.value.getBinaryData())
273 {
274 xml.setAttribute ("base64:" + i.name.toString(), mb->toBase64Encoding());
275 }
276 else
277 {
278 // These types can't be stored as XML!
279 jassert (! i.value.isObject());
280 jassert (! i.value.isMethod());
281 jassert (! i.value.isArray());
282
283 xml.setAttribute (i.name.toString(),
284 i.value.toString());
285 }
286 }
287}
288
289} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
Definition juce_Array.h:578
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Definition juce_Array.h:226
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
Definition juce_Array.h:202
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:219
void remove(int indexToRemove)
Removes an element from the array.
Definition juce_Array.h:724
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:375
ElementType & getReference(int index) const noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:271
void clear()
Removes all elements from the array.
Definition juce_Array.h:192
Represents a string identifier, designed for accessing properties by name.
A class to hold a resizable block of raw data.
Holds a set of named var objects.
bool set(const Identifier &name, const var &newValue)
Changes or adds a named value.
bool contains(const Identifier &name) const noexcept
Returns true if the set contains an item with the specified name.
bool remove(const Identifier &name)
Removes a value from the set.
int indexOf(const Identifier &name) const noexcept
Returns the index of the given name, or -1 if it's not found.
const var & getValueAt(int index) const noexcept
Returns the value of the item at a given index.
var * getVarPointerAt(int index) const noexcept
Returns the value of the item at a given index.
void clear()
Removes all values.
var * getVarPointer(const Identifier &name) const noexcept
Returns a pointer to the var that holds a named value, or null if there is no value with this name.
bool isEmpty() const noexcept
Returns true if the set is empty.
bool operator==(const NamedValueSet &) const noexcept
Two NamedValueSets are considered equal if they contain all the same key/value pairs,...
NamedValueSet() noexcept
Creates an empty set.
Identifier getName(int index) const noexcept
Returns the name of the value at a given index.
var getWithDefault(const Identifier &name, const var &defaultReturnValue) const
Tries to return the named value, but if no such value is found, this will instead return the supplied...
int size() const noexcept
Returns the total number of values that the set contains.
const var & operator[](const Identifier &name) const noexcept
Returns the value of a named item.
void copyToXmlAttributes(XmlElement &xml) const
Sets attributes in an XML element corresponding to each of this object's properties.
void setFromXmlAttributes(const XmlElement &xml)
Sets properties to the values of all of an XML element's attributes.
Used to build a tree of elements representing an XML document.
A variant class, that can be used to hold a range of primitive values.