From dcbde7b11820755db59b3ca1431f702d6295a012 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 10 Feb 2014 12:45:12 +0000 Subject: [PATCH] Added NamedValueSet::indexOf() --- .../containers/juce_NamedValueSet.cpp | 31 ++++++++++++++----- .../juce_core/containers/juce_NamedValueSet.h | 19 +++++++----- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/modules/juce_core/containers/juce_NamedValueSet.cpp b/modules/juce_core/containers/juce_NamedValueSet.cpp index ed30af2d44..0d866b1b8c 100644 --- a/modules/juce_core/containers/juce_NamedValueSet.cpp +++ b/modules/juce_core/containers/juce_NamedValueSet.cpp @@ -141,7 +141,7 @@ int NamedValueSet::size() const noexcept return values.size(); } -const var& NamedValueSet::operator[] (const Identifier name) const +const var& NamedValueSet::operator[] (Identifier name) const { for (NamedValue* i = values; i != nullptr; i = i->nextListItem) if (i->name == name) @@ -150,7 +150,7 @@ const var& NamedValueSet::operator[] (const Identifier name) const return var::null; } -var NamedValueSet::getWithDefault (const Identifier name, const var& defaultReturnValue) const +var NamedValueSet::getWithDefault (Identifier name, const var& defaultReturnValue) const { if (const var* const v = getVarPointer (name)) return *v; @@ -158,7 +158,7 @@ var NamedValueSet::getWithDefault (const Identifier name, const var& defaultRetu return defaultReturnValue; } -var* NamedValueSet::getVarPointer (const Identifier name) const noexcept +var* NamedValueSet::getVarPointer (Identifier name) const noexcept { for (NamedValue* i = values; i != nullptr; i = i->nextListItem) if (i->name == name) @@ -168,7 +168,7 @@ var* NamedValueSet::getVarPointer (const Identifier name) const noexcept } #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS -bool NamedValueSet::set (const Identifier name, var&& newValue) +bool NamedValueSet::set (Identifier name, var&& newValue) { LinkedListPointer* i = &values; @@ -193,7 +193,7 @@ bool NamedValueSet::set (const Identifier name, var&& newValue) } #endif -bool NamedValueSet::set (const Identifier name, const var& newValue) +bool NamedValueSet::set (Identifier name, const var& newValue) { LinkedListPointer* i = &values; @@ -217,12 +217,27 @@ bool NamedValueSet::set (const Identifier name, const var& newValue) return true; } -bool NamedValueSet::contains (const Identifier name) const +bool NamedValueSet::contains (Identifier name) const { return getVarPointer (name) != nullptr; } -bool NamedValueSet::remove (const Identifier name) +int NamedValueSet::indexOf (Identifier name) const noexcept +{ + int index = 0; + + for (NamedValue* i = values; i != nullptr; i = i->nextListItem) + { + if (i->name == name) + return index; + + ++index; + } + + return -1; +} + +bool NamedValueSet::remove (Identifier name) { LinkedListPointer* i = &values; @@ -245,7 +260,7 @@ bool NamedValueSet::remove (const Identifier name) return false; } -const Identifier NamedValueSet::getName (const int index) const +Identifier NamedValueSet::getName (const int index) const { const NamedValue* const v = values[index]; jassert (v != nullptr); diff --git a/modules/juce_core/containers/juce_NamedValueSet.h b/modules/juce_core/containers/juce_NamedValueSet.h index 2ac2b86309..6043e2ab98 100644 --- a/modules/juce_core/containers/juce_NamedValueSet.h +++ b/modules/juce_core/containers/juce_NamedValueSet.h @@ -67,46 +67,49 @@ public: If the name isn't found, this will return a void variant. @see getProperty */ - const var& operator[] (const Identifier name) const; + const var& operator[] (Identifier name) const; /** Tries to return the named value, but if no such value is found, this will instead return the supplied default value. */ - var getWithDefault (const Identifier name, const var& defaultReturnValue) const; + var getWithDefault (Identifier name, const var& defaultReturnValue) const; /** Changes or adds a named value. @returns true if a value was changed or added; false if the value was already set the the value passed-in. */ - bool set (const Identifier name, const var& newValue); + bool set (Identifier name, const var& newValue); #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS /** Changes or adds a named value. @returns true if a value was changed or added; false if the value was already set the the value passed-in. */ - bool set (const Identifier name, var&& newValue); + bool set (Identifier name, var&& newValue); #endif /** Returns true if the set contains an item with the specified name. */ - bool contains (const Identifier name) const; + bool contains (Identifier name) const; /** Removes a value from the set. @returns true if a value was removed; false if there was no value with the name that was given. */ - bool remove (const Identifier name); + bool remove (Identifier name); /** Returns the name of the value at a given index. The index must be between 0 and size() - 1. */ - const Identifier getName (int index) const; + Identifier getName (int index) const; /** Returns the value of the item at a given index. The index must be between 0 and size() - 1. */ const var& getValueAt (int index) const; + /** Returns the index of the given name, or -1 if it's not found. */ + int indexOf (Identifier name) const noexcept; + /** Removes all values. */ void clear(); @@ -117,7 +120,7 @@ public: Do not use this method unless you really need access to the internal var object for some reason - for normal reading and writing always prefer operator[]() and set(). */ - var* getVarPointer (const Identifier name) const noexcept; + var* getVarPointer (Identifier name) const noexcept; //============================================================================== /** Sets properties to the values of all of an XML element's attributes. */