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 
23 namespace juce
24 {
25 
26 NamedValueSet::NamedValue::NamedValue() noexcept {}
27 NamedValueSet::NamedValue::~NamedValue() noexcept {}
28 
29 NamedValueSet::NamedValue::NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
30 NamedValueSet::NamedValue::NamedValue (const NamedValue& other) : NamedValue (other.name, other.value) {}
31 
32 NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
33  : NamedValue (std::move (other.name),
34  std::move (other.value))
35 {}
36 
37 NamedValueSet::NamedValue::NamedValue (const Identifier& n, var&& v) noexcept
38  : name (n), value (std::move (v))
39 {
40 }
41 
42 NamedValueSet::NamedValue::NamedValue (Identifier&& n, var&& v) noexcept
43  : name (std::move (n)),
44  value (std::move (v))
45 {}
46 
47 NamedValueSet::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 
54 bool NamedValueSet::NamedValue::operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
55 bool NamedValueSet::NamedValue::operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
56 
57 //==============================================================================
60 
61 NamedValueSet::NamedValueSet (const NamedValueSet& other) : values (other.values) {}
62 
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 
78 NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
79 {
80  other.values.swapWith (values);
81  return *this;
82 }
83 
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 
123 bool 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 
128 static 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) const noexcept
151 {
152  for (auto& i : values)
153  if (i.name == name)
154  return &(i.value);
155 
156  return {};
157 }
158 
159 bool 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 
174 bool 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 
189 bool NamedValueSet::contains (const Identifier& name) const noexcept
190 {
191  return getVarPointer (name) != nullptr;
192 }
193 
194 int 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 
221 Identifier 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 
230 const 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 
239 var* 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  {
255  MemoryBlock mb;
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
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...
void copyToXmlAttributes(XmlElement &xml) const
Sets attributes in an XML element corresponding to each of this object&#39;s properties.
int size() const noexcept
Returns the total number of values that the set contains.
Represents a string identifier, designed for accessing properties by name.
int indexOf(const Identifier &name) const noexcept
Returns the index of the given name, or -1 if it&#39;s not found.
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:45
Used to build a tree of elements representing an XML document.
STL namespace.
void clear()
Removes all values.
var * getVarPointerAt(int index) const noexcept
Returns the value of the item at a given index.
NamedValueSet() noexcept
Creates an empty set.
Holds a set of named var objects.
ObjectType * get() const noexcept
Returns the item which this pointer points to.
bool isEmpty() const noexcept
Returns true if the set is empty.
void setAttribute(const Identifier &attributeName, const String &newValue)
Adds a named attribute to the element.
void setFromXmlAttributes(const XmlElement &xml)
Sets properties to the values of all of an XML element&#39;s attributes.
bool operator==(const NamedValueSet &) const noexcept
Two NamedValueSets are considered equal if they contain all the same key/value pairs, regardless of the order.
bool contains(const Identifier &name) const noexcept
Returns true if the set contains an item with the specified name.
~NamedValueSet() noexcept
Destructor.
const var & getValueAt(int index) const noexcept
Returns the value of the item at a given index.
bool fromBase64Encoding(StringRef encodedString)
Takes a string created by MemoryBlock::toBase64Encoding() and extracts the original data...
Identifier getName(int index) const noexcept
Returns the name of the value at a given index.
bool set(const Identifier &name, const var &newValue)
Changes or adds a named value.
const var & operator[](const Identifier &name) const noexcept
Returns the value of a named item.
A class to hold a resizable block of raw data.
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...
bool remove(const Identifier &name)
Removes a value from the set.