From 1942e3d0c12495dbdda5c27de350e14f4ebc2798 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 23 Aug 2016 09:39:12 +0100 Subject: [PATCH] Added begin/end iterator methods for ValueTree, for handy range-based-for loops over its children --- .../values/juce_ValueTree.cpp | 23 +++++++++++++++++++ .../values/juce_ValueTree.h | 19 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index a76a0c48c9..1d854799b3 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -858,6 +858,29 @@ ValueTree ValueTree::getChild (int index) const : static_cast (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 (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 (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(); diff --git a/modules/juce_data_structures/values/juce_ValueTree.h b/modules/juce_data_structures/values/juce_ValueTree.h index edb611ddf7..eb666123a3 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.h +++ b/modules/juce_data_structures/values/juce_ValueTree.h @@ -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.