Browse Source

Added begin/end iterator methods for ValueTree, for handy range-based-for loops over its children

tags/2021-05-28
jules 9 years ago
parent
commit
1942e3d0c1
2 changed files with 42 additions and 0 deletions
  1. +23
    -0
      modules/juce_data_structures/values/juce_ValueTree.cpp
  2. +19
    -0
      modules/juce_data_structures/values/juce_ValueTree.h

+ 23
- 0
modules/juce_data_structures/values/juce_ValueTree.cpp View File

@@ -858,6 +858,29 @@ ValueTree ValueTree::getChild (int index) const
: static_cast<SharedObject*> (nullptr));
}
ValueTree::Iterator::Iterator (const ValueTree& v, bool isEnd) noexcept
: internal (v.object != nullptr ? (isEnd ? v.object->children.end() : v.object->children.begin()) : nullptr)
{}
ValueTree::Iterator& ValueTree::Iterator::operator++() noexcept
{
internal = static_cast<SharedObject**> (internal) + 1;
return *this;
}
bool ValueTree::Iterator::operator!= (const Iterator& other) const noexcept
{
return internal != other.internal;
}
ValueTree ValueTree::Iterator::operator*() const
{
return ValueTree (*static_cast<SharedObject**> (internal));
}
ValueTree::Iterator ValueTree::begin() const noexcept { return Iterator (*this, false); }
ValueTree::Iterator ValueTree::end() const noexcept { return Iterator (*this, true); }
ValueTree ValueTree::getChildWithName (const Identifier& type) const
{
return object != nullptr ? object->getChildWithName (type) : ValueTree();


+ 19
- 0
modules/juce_data_structures/values/juce_ValueTree.h View File

@@ -328,6 +328,25 @@ public:
*/
ValueTree getSibling (int delta) const noexcept;
//==============================================================================
struct Iterator
{
Iterator (const ValueTree&, bool isEnd) noexcept;
Iterator& operator++() noexcept;
bool operator!= (const Iterator&) const noexcept;
ValueTree operator*() const;
private:
void* internal;
};
/** Returns a start iterator for the children in this tree. */
Iterator begin() const noexcept;
/** Returns an end iterator for the children in this tree. */
Iterator end() const noexcept;
//==============================================================================
/** Creates an XmlElement that holds a complete image of this node and all its children.


Loading…
Cancel
Save