mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Made the NamedValueSet begin/end iterators const, and tidied up a few other things in that class, including a fix to its equality operator
This commit is contained in:
parent
71bd8f6bed
commit
6b45923426
2 changed files with 110 additions and 88 deletions
|
|
@ -23,15 +23,44 @@
|
|||
namespace juce
|
||||
{
|
||||
|
||||
NamedValueSet::NamedValueSet() noexcept
|
||||
NamedValueSet::NamedValue::NamedValue() noexcept {}
|
||||
NamedValueSet::NamedValue::~NamedValue() noexcept {}
|
||||
|
||||
NamedValueSet::NamedValue::NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
|
||||
NamedValueSet::NamedValue::NamedValue (const NamedValue& other) : NamedValue (other.name, other.value) {}
|
||||
|
||||
NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
|
||||
: NamedValue (static_cast<Identifier&&> (other.name),
|
||||
static_cast<var&&> (other.value))
|
||||
{}
|
||||
|
||||
NamedValueSet::NamedValue::NamedValue (const Identifier& n, var&& v) noexcept
|
||||
: name (n), value (static_cast<var&&> (v))
|
||||
{
|
||||
}
|
||||
|
||||
NamedValueSet::NamedValueSet (const NamedValueSet& other)
|
||||
: values (other.values)
|
||||
NamedValueSet::NamedValue::NamedValue (Identifier&& n, var&& v) noexcept
|
||||
: name (static_cast<Identifier&&> (n)),
|
||||
value (static_cast<var&&> (v))
|
||||
{}
|
||||
|
||||
NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
|
||||
{
|
||||
name = static_cast<Identifier&&> (other.name);
|
||||
value = static_cast<var&&> (other.value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool NamedValueSet::NamedValue::operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
|
||||
bool NamedValueSet::NamedValue::operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
|
||||
|
||||
//==============================================================================
|
||||
NamedValueSet::NamedValueSet() noexcept {}
|
||||
NamedValueSet::~NamedValueSet() noexcept {}
|
||||
|
||||
NamedValueSet::NamedValueSet (const NamedValueSet& other) : values (other.values) {}
|
||||
NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept : values (static_cast<Array<NamedValue>&&> (other.values)) {}
|
||||
|
||||
NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
|
||||
{
|
||||
clear();
|
||||
|
|
@ -39,45 +68,55 @@ NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
|
|||
return *this;
|
||||
}
|
||||
|
||||
NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
|
||||
: values (static_cast<Array<NamedValue>&&> (other.values))
|
||||
{
|
||||
}
|
||||
|
||||
NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
|
||||
{
|
||||
other.values.swapWith (values);
|
||||
return *this;
|
||||
}
|
||||
|
||||
NamedValueSet::~NamedValueSet() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
void NamedValueSet::clear()
|
||||
{
|
||||
values.clear();
|
||||
}
|
||||
|
||||
bool NamedValueSet::operator== (const NamedValueSet& other) const
|
||||
bool NamedValueSet::operator== (const NamedValueSet& other) const noexcept
|
||||
{
|
||||
return values == other.values;
|
||||
auto num = values.size();
|
||||
|
||||
if (num != other.values.size())
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
// optimise for the case where the keys are in the same order
|
||||
if (values.getReference(i).name == other.values.getReference(i).name)
|
||||
{
|
||||
if (values.getReference(i).value != other.values.getReference(i).value)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if we encounter keys that are in a different order, search remaining items by brute force..
|
||||
for (int j = i; j < num; ++j)
|
||||
{
|
||||
if (auto* otherVal = other.getVarPointer (values.getReference(j).name))
|
||||
if (values.getReference(j).value == *otherVal)
|
||||
continue;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NamedValueSet::operator!= (const NamedValueSet& other) const
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
bool NamedValueSet::operator!= (const NamedValueSet& other) const noexcept { return ! operator== (other); }
|
||||
|
||||
int NamedValueSet::size() const noexcept
|
||||
{
|
||||
return values.size();
|
||||
}
|
||||
|
||||
bool NamedValueSet::isEmpty() const noexcept
|
||||
{
|
||||
return values.isEmpty();
|
||||
}
|
||||
int NamedValueSet::size() const noexcept { return values.size(); }
|
||||
bool NamedValueSet::isEmpty() const noexcept { return values.isEmpty(); }
|
||||
|
||||
static const var& getNullVarRef() noexcept
|
||||
{
|
||||
|
|
@ -91,7 +130,7 @@ static const var& getNullVarRef() noexcept
|
|||
|
||||
const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
|
||||
{
|
||||
if (const var* v = getVarPointer (name))
|
||||
if (auto* v = getVarPointer (name))
|
||||
return *v;
|
||||
|
||||
return getNullVarRef();
|
||||
|
|
@ -99,7 +138,7 @@ const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
|
|||
|
||||
var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
|
||||
{
|
||||
if (const var* const v = getVarPointer (name))
|
||||
if (auto* v = getVarPointer (name))
|
||||
return *v;
|
||||
|
||||
return defaultReturnValue;
|
||||
|
|
@ -107,16 +146,16 @@ var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultRet
|
|||
|
||||
var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
|
||||
{
|
||||
for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
|
||||
if (i->name == name)
|
||||
return &(i->value);
|
||||
for (auto& i : values)
|
||||
if (i.name == name)
|
||||
return &(i.value);
|
||||
|
||||
return nullptr;
|
||||
return {};
|
||||
}
|
||||
|
||||
bool NamedValueSet::set (const Identifier& name, var&& newValue)
|
||||
{
|
||||
if (var* const v = getVarPointer (name))
|
||||
if (auto* v = getVarPointer (name))
|
||||
{
|
||||
if (v->equalsWithSameType (newValue))
|
||||
return false;
|
||||
|
|
@ -125,13 +164,13 @@ bool NamedValueSet::set (const Identifier& name, var&& newValue)
|
|||
return true;
|
||||
}
|
||||
|
||||
values.add (NamedValue (name, static_cast<var&&> (newValue)));
|
||||
values.add ({ name, static_cast<var&&> (newValue) });
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NamedValueSet::set (const Identifier& name, const var& newValue)
|
||||
{
|
||||
if (var* const v = getVarPointer (name))
|
||||
if (auto* v = getVarPointer (name))
|
||||
{
|
||||
if (v->equalsWithSameType (newValue))
|
||||
return false;
|
||||
|
|
@ -140,7 +179,7 @@ bool NamedValueSet::set (const Identifier& name, const var& newValue)
|
|||
return true;
|
||||
}
|
||||
|
||||
values.add (NamedValue (name, newValue));
|
||||
values.add ({ name, newValue });
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -151,7 +190,7 @@ bool NamedValueSet::contains (const Identifier& name) const noexcept
|
|||
|
||||
int NamedValueSet::indexOf (const Identifier& name) const noexcept
|
||||
{
|
||||
const int numValues = values.size();
|
||||
auto numValues = values.size();
|
||||
|
||||
for (int i = 0; i < numValues; ++i)
|
||||
if (values.getReference(i).name == name)
|
||||
|
|
@ -162,7 +201,7 @@ int NamedValueSet::indexOf (const Identifier& name) const noexcept
|
|||
|
||||
bool NamedValueSet::remove (const Identifier& name)
|
||||
{
|
||||
const int numValues = values.size();
|
||||
auto numValues = values.size();
|
||||
|
||||
for (int i = 0; i < numValues; ++i)
|
||||
{
|
||||
|
|
@ -182,7 +221,7 @@ Identifier NamedValueSet::getName (const int index) const noexcept
|
|||
return values.getReference (index).name;
|
||||
|
||||
jassertfalse;
|
||||
return Identifier();
|
||||
return {};
|
||||
}
|
||||
|
||||
const var& NamedValueSet::getValueAt (const int index) const noexcept
|
||||
|
|
@ -199,14 +238,14 @@ var* NamedValueSet::getVarPointerAt (int index) const noexcept
|
|||
if (isPositiveAndBelow (index, values.size()))
|
||||
return &(values.getReference (index).value);
|
||||
|
||||
return nullptr;
|
||||
return {};
|
||||
}
|
||||
|
||||
void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
|
||||
{
|
||||
values.clearQuick();
|
||||
|
||||
for (const XmlElement::XmlAttributeNode* att = xml.attributes; att != nullptr; att = att->nextListItem)
|
||||
for (auto* att = xml.attributes.get(); att != nullptr; att = att->nextListItem)
|
||||
{
|
||||
if (att->name.toString().startsWith ("base64:"))
|
||||
{
|
||||
|
|
@ -214,32 +253,32 @@ void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
|
|||
|
||||
if (mb.fromBase64Encoding (att->value))
|
||||
{
|
||||
values.add (NamedValue (att->name.toString().substring (7), var (mb)));
|
||||
values.add ({ att->name.toString().substring (7), var (mb) });
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
values.add (NamedValue (att->name, var (att->value)));
|
||||
values.add ({ att->name, var (att->value) });
|
||||
}
|
||||
}
|
||||
|
||||
void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
|
||||
{
|
||||
for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
|
||||
for (auto& i : values)
|
||||
{
|
||||
if (const MemoryBlock* mb = i->value.getBinaryData())
|
||||
if (auto* mb = i.value.getBinaryData())
|
||||
{
|
||||
xml.setAttribute ("base64:" + i->name.toString(), mb->toBase64Encoding());
|
||||
xml.setAttribute ("base64:" + i.name.toString(), mb->toBase64Encoding());
|
||||
}
|
||||
else
|
||||
{
|
||||
// These types can't be stored as XML!
|
||||
jassert (! i->value.isObject());
|
||||
jassert (! i->value.isMethod());
|
||||
jassert (! i->value.isArray());
|
||||
jassert (! i.value.isObject());
|
||||
jassert (! i.value.isMethod());
|
||||
jassert (! i.value.isArray());
|
||||
|
||||
xml.setAttribute (i->name.toString(),
|
||||
i->value.toString());
|
||||
xml.setAttribute (i.name.toString(),
|
||||
i.value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,62 +35,45 @@ public:
|
|||
/** Creates an empty set. */
|
||||
NamedValueSet() noexcept;
|
||||
|
||||
/** Creates a copy of another set. */
|
||||
NamedValueSet (const NamedValueSet&);
|
||||
|
||||
/** Replaces this set with a copy of another set. */
|
||||
NamedValueSet& operator= (const NamedValueSet&);
|
||||
|
||||
/** Move constructor */
|
||||
NamedValueSet (NamedValueSet&&) noexcept;
|
||||
|
||||
/** Move assignment operator */
|
||||
NamedValueSet& operator= (const NamedValueSet&);
|
||||
NamedValueSet& operator= (NamedValueSet&&) noexcept;
|
||||
|
||||
/** Destructor. */
|
||||
~NamedValueSet() noexcept;
|
||||
|
||||
bool operator== (const NamedValueSet&) const;
|
||||
bool operator!= (const NamedValueSet&) const;
|
||||
/** Two NamedValueSets are considered equal if they contain all the same key/value
|
||||
pairs, regardless of the order.
|
||||
*/
|
||||
bool operator== (const NamedValueSet&) const noexcept;
|
||||
bool operator!= (const NamedValueSet&) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
struct NamedValue
|
||||
struct JUCE_API NamedValue
|
||||
{
|
||||
NamedValue() noexcept {}
|
||||
NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
|
||||
NamedValue (const NamedValue& other) : name (other.name), value (other.value) {}
|
||||
NamedValue() noexcept;
|
||||
~NamedValue() noexcept;
|
||||
|
||||
NamedValue (NamedValue&& other) noexcept
|
||||
: name (static_cast<Identifier&&> (other.name)),
|
||||
value (static_cast<var&&> (other.value))
|
||||
{
|
||||
}
|
||||
NamedValue (const Identifier& name, const var& value);
|
||||
NamedValue (const Identifier& name, var&& value) noexcept;
|
||||
NamedValue (Identifier&& name, var&& value) noexcept;
|
||||
|
||||
NamedValue (Identifier&& n, var&& v) noexcept
|
||||
: name (static_cast<Identifier&&> (n)),
|
||||
value (static_cast<var&&> (v))
|
||||
{
|
||||
}
|
||||
NamedValue (const NamedValue&);
|
||||
NamedValue (NamedValue&&) noexcept;
|
||||
NamedValue& operator= (NamedValue&&) noexcept;
|
||||
|
||||
NamedValue& operator= (NamedValue&& other) noexcept
|
||||
{
|
||||
name = static_cast<Identifier&&> (other.name);
|
||||
value = static_cast<var&&> (other.value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
|
||||
bool operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
|
||||
bool operator== (const NamedValue&) const noexcept;
|
||||
bool operator!= (const NamedValue&) const noexcept;
|
||||
|
||||
Identifier name;
|
||||
var value;
|
||||
};
|
||||
|
||||
NamedValueSet::NamedValue* begin() { return values.begin(); }
|
||||
NamedValueSet::NamedValue* end() { return values.end(); }
|
||||
const NamedValueSet::NamedValue* begin() const noexcept { return values.begin(); }
|
||||
const NamedValueSet::NamedValue* end() const noexcept { return values.end(); }
|
||||
|
||||
//==============================================================================
|
||||
|
||||
/** Returns the total number of values that the set contains. */
|
||||
int size() const noexcept;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue