From d06e4354dd3daaa3c1535fd72e63c7f9e5eb90b2 Mon Sep 17 00:00:00 2001 From: arturo castro Date: Tue, 7 Aug 2018 11:48:46 +0200 Subject: [PATCH] ofXml: fix attribute iterator --- libs/openFrameworks/utils/ofXml.cpp | 4 +- libs/openFrameworks/utils/ofXml.h | 110 +++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 11 deletions(-) diff --git a/libs/openFrameworks/utils/ofXml.cpp b/libs/openFrameworks/utils/ofXml.cpp index b729a859e1f..6a90160070b 100644 --- a/libs/openFrameworks/utils/ofXml.cpp +++ b/libs/openFrameworks/utils/ofXml.cpp @@ -150,8 +150,8 @@ ofXml::Attribute ofXml::getAttribute(const std::string & name) const{ return this->xml.attribute(name.c_str()); } -ofXml::Range> ofXml::getAttributes() const{ - return ofXml::Range>(doc, this->xml.attributes()); +ofXml::Range ofXml::getAttributes() const{ + return ofXml::Range(doc, this->xml.attributes()); } ofXml::Attribute ofXml::getFirstAttribute() const{ diff --git a/libs/openFrameworks/utils/ofXml.h b/libs/openFrameworks/utils/ofXml.h index 957e1303557..164baa61bda 100644 --- a/libs/openFrameworks/utils/ofXml.h +++ b/libs/openFrameworks/utils/ofXml.h @@ -4,8 +4,9 @@ #include "pugixml.hpp" #include "ofParameter.h" -template +template class ofXmlIterator; +class ofXmlAttributeIterator; class ofXmlSearchIterator; class ofXml{ @@ -80,8 +81,14 @@ class ofXml{ template class Range{ public: - It begin() const { return It(ofXml(doc, *range.begin())); } - It end() const { return It(ofXml(doc, pugi::xml_node())); } + It begin() const { + if(range.begin() != range.end()){ + return It(doc, *range.begin()); + }else{ + return It(doc, typename It::Node()); + } + } + It end() const { return It(doc, typename It::Node()); } private: Range(std::shared_ptr doc, pugi::xml_object_range range) @@ -130,7 +137,7 @@ class ofXml{ Attribute getAttribute(const std::string & name) const; - Range> getAttributes() const; + Range getAttributes() const; Attribute getFirstAttribute() const; Attribute getLastAttribute() const; Attribute appendAttribute(const std::string & name); @@ -203,12 +210,13 @@ class ofXml{ std::shared_ptr doc; pugi::xml_node xml; - template + template friend class ofXmlIterator; + friend class ofXmlAttributeIterator; friend class ofXmlSearchIterator; }; -template +template class ofXmlIterator{ public: ofXmlIterator(){} @@ -222,11 +230,19 @@ class ofXmlIterator{ return this->xml.xml != rhs.xml.xml; } - ofXml& operator*() const{ + const ofXml& operator*() const{ return this->xml; } - ofXml* operator->() const{ + const ofXml* operator->() const{ + return &this->xml; + } + + ofXml& operator*(){ + return this->xml; + } + + ofXml* operator->(){ return &this->xml; } @@ -252,10 +268,17 @@ class ofXmlIterator{ return now; } typedef It Base; + typedef pugi::xml_node Node; private: // Construct an iterator which points to the specified node - ofXmlIterator(ofXml xml) + ofXmlIterator(std::shared_ptr doc, const pugi::xml_node & xml) + :xml(doc, xml){ + + } + + // Construct an iterator which points to the specified node + ofXmlIterator(ofXml && xml) :xml(xml){ } @@ -263,6 +286,75 @@ class ofXmlIterator{ friend class ofXml; }; +class ofXmlAttributeIterator{ +public: + ofXmlAttributeIterator(){} + + // Iterator operators + bool operator==(const ofXmlAttributeIterator& rhs) const{ + return this->attr == rhs.attr; + } + + bool operator!=(const ofXmlAttributeIterator& rhs) const{ + return this->attr != rhs.attr; + } + + const ofXml::Attribute & operator*() const{ + return this->attr; + } + + const ofXml::Attribute* operator->() const{ + return &this->attr; + } + + ofXml::Attribute & operator*(){ + return this->attr; + } + + ofXml::Attribute* operator->(){ + return &this->attr; + } + + const ofXmlAttributeIterator& operator++(){ + this->attr = attr.getNextAttribute(); + return *this; + } + + ofXmlAttributeIterator operator++(int){ + auto now = attr; + this->attr = attr.getNextAttribute(); + return now; + } + + const ofXmlAttributeIterator& operator--(){ + this->attr = attr.getPreviousAttribute(); + return *this; + } + + ofXmlAttributeIterator operator--(int){ + auto now = attr; + this->attr = attr.getPreviousAttribute(); + return now; + } + + typedef pugi::xml_attribute_iterator Base; + typedef pugi::xml_attribute Node; +private: + + // Construct an iterator which points to the specified node + ofXmlAttributeIterator(std::shared_ptr, const ofXml::Attribute & attr) + :attr(attr){ + + } + + ofXmlAttributeIterator(const ofXml::Attribute & attr) + :attr(attr){ + + } + ofXml::Attribute attr; + friend class ofXml; +}; + class ofXmlSearchIterator{ public: