Browse Source

Made the NamedValueSet begin/end iterators const, and tidied up a few other things in that class, including a fix to its equality operator

tags/2021-05-28
jules 8 years ago
parent
commit
6b45923426
2 changed files with 112 additions and 90 deletions
  1. +90
    -51
      modules/juce_core/containers/juce_NamedValueSet.cpp
  2. +22
    -39
      modules/juce_core/containers/juce_NamedValueSet.h

+ 90
- 51
modules/juce_core/containers/juce_NamedValueSet.cpp View File

@@ -23,15 +23,44 @@
namespace juce 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) NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
{ {
clear(); clear();
@@ -39,46 +68,56 @@ NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
return *this; return *this;
} }
NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
: values (static_cast<Array<NamedValue>&&> (other.values))
{
}
NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
{ {
other.values.swapWith (values); other.values.swapWith (values);
return *this; return *this;
} }
NamedValueSet::~NamedValueSet() noexcept
{
}
void NamedValueSet::clear() void NamedValueSet::clear()
{ {
values.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();
bool NamedValueSet::operator!= (const NamedValueSet& other) const
{
return ! operator== (other);
}
if (num != other.values.size())
return false;
int NamedValueSet::size() const noexcept
{
return values.size();
}
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;
bool NamedValueSet::isEmpty() const noexcept
{
return values.isEmpty();
return false;
}
return true;
}
}
return true;
} }
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(); }
static const var& getNullVarRef() noexcept static const var& getNullVarRef() noexcept
{ {
#if JUCE_ALLOW_STATIC_NULL_VARIABLES #if JUCE_ALLOW_STATIC_NULL_VARIABLES
@@ -91,7 +130,7 @@ static const var& getNullVarRef() noexcept
const var& NamedValueSet::operator[] (const Identifier& name) const noexcept const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
{ {
if (const var* v = getVarPointer (name))
if (auto* v = getVarPointer (name))
return *v; return *v;
return getNullVarRef(); 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 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 *v;
return defaultReturnValue; return defaultReturnValue;
@@ -107,16 +146,16 @@ var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultRet
var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept 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) bool NamedValueSet::set (const Identifier& name, var&& newValue)
{ {
if (var* const v = getVarPointer (name))
if (auto* v = getVarPointer (name))
{ {
if (v->equalsWithSameType (newValue)) if (v->equalsWithSameType (newValue))
return false; return false;
@@ -125,13 +164,13 @@ bool NamedValueSet::set (const Identifier& name, var&& newValue)
return true; return true;
} }
values.add (NamedValue (name, static_cast<var&&> (newValue)));
values.add ({ name, static_cast<var&&> (newValue) });
return true; return true;
} }
bool NamedValueSet::set (const Identifier& name, const var& newValue) bool NamedValueSet::set (const Identifier& name, const var& newValue)
{ {
if (var* const v = getVarPointer (name))
if (auto* v = getVarPointer (name))
{ {
if (v->equalsWithSameType (newValue)) if (v->equalsWithSameType (newValue))
return false; return false;
@@ -140,7 +179,7 @@ bool NamedValueSet::set (const Identifier& name, const var& newValue)
return true; return true;
} }
values.add (NamedValue (name, newValue));
values.add ({ name, newValue });
return true; return true;
} }
@@ -151,7 +190,7 @@ bool NamedValueSet::contains (const Identifier& name) const noexcept
int NamedValueSet::indexOf (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) for (int i = 0; i < numValues; ++i)
if (values.getReference(i).name == name) if (values.getReference(i).name == name)
@@ -162,7 +201,7 @@ int NamedValueSet::indexOf (const Identifier& name) const noexcept
bool NamedValueSet::remove (const Identifier& name) bool NamedValueSet::remove (const Identifier& name)
{ {
const int numValues = values.size();
auto numValues = values.size();
for (int i = 0; i < numValues; ++i) for (int i = 0; i < numValues; ++i)
{ {
@@ -182,7 +221,7 @@ Identifier NamedValueSet::getName (const int index) const noexcept
return values.getReference (index).name; return values.getReference (index).name;
jassertfalse; jassertfalse;
return Identifier();
return {};
} }
const var& NamedValueSet::getValueAt (const int index) const noexcept 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())) if (isPositiveAndBelow (index, values.size()))
return &(values.getReference (index).value); return &(values.getReference (index).value);
return nullptr;
return {};
} }
void NamedValueSet::setFromXmlAttributes (const XmlElement& xml) void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
{ {
values.clearQuick(); 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:")) if (att->name.toString().startsWith ("base64:"))
{ {
@@ -214,32 +253,32 @@ void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
if (mb.fromBase64Encoding (att->value)) 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; continue;
} }
} }
values.add (NamedValue (att->name, var (att->value)));
values.add ({ att->name, var (att->value) });
} }
} }
void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const 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 else
{ {
// These types can't be stored as XML! // 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());
} }
} }
} }


+ 22
- 39
modules/juce_core/containers/juce_NamedValueSet.h View File

@@ -35,62 +35,45 @@ public:
/** Creates an empty set. */ /** Creates an empty set. */
NamedValueSet() noexcept; NamedValueSet() noexcept;
/** Creates a copy of another set. */
NamedValueSet (const NamedValueSet&); NamedValueSet (const NamedValueSet&);
/** Replaces this set with a copy of another set. */
NamedValueSet& operator= (const NamedValueSet&);
/** Move constructor */
NamedValueSet (NamedValueSet&&) noexcept; NamedValueSet (NamedValueSet&&) noexcept;
/** Move assignment operator */
NamedValueSet& operator= (const NamedValueSet&);
NamedValueSet& operator= (NamedValueSet&&) noexcept; NamedValueSet& operator= (NamedValueSet&&) noexcept;
/** Destructor. */ /** Destructor. */
~NamedValueSet() noexcept; ~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 (NamedValue&& other) noexcept
: name (static_cast<Identifier&&> (other.name)),
value (static_cast<var&&> (other.value))
{
}
NamedValue (Identifier&& n, var&& v) noexcept
: name (static_cast<Identifier&&> (n)),
value (static_cast<var&&> (v))
{
}
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); }
NamedValue() noexcept;
~NamedValue() noexcept;
NamedValue (const Identifier& name, const var& value);
NamedValue (const Identifier& name, var&& value) noexcept;
NamedValue (Identifier&& name, var&& value) noexcept;
NamedValue (const NamedValue&);
NamedValue (NamedValue&&) noexcept;
NamedValue& operator= (NamedValue&&) noexcept;
bool operator== (const NamedValue&) const noexcept;
bool operator!= (const NamedValue&) const noexcept;
Identifier name; Identifier name;
var value; 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. */ /** Returns the total number of values that the set contains. */
int size() const noexcept; int size() const noexcept;


Loading…
Cancel
Save