Skip to content

Commit

Permalink
ofXml: fix attribute iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoc committed Aug 7, 2018
1 parent 333ce0d commit d06e435
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
4 changes: 2 additions & 2 deletions libs/openFrameworks/utils/ofXml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ ofXml::Attribute ofXml::getAttribute(const std::string & name) const{
return this->xml.attribute(name.c_str());
}

ofXml::Range<ofXmlIterator<pugi::xml_attribute_iterator>> ofXml::getAttributes() const{
return ofXml::Range<ofXmlIterator<pugi::xml_attribute_iterator>>(doc, this->xml.attributes());
ofXml::Range<ofXmlAttributeIterator> ofXml::getAttributes() const{
return ofXml::Range<ofXmlAttributeIterator>(doc, this->xml.attributes());
}

ofXml::Attribute ofXml::getFirstAttribute() const{
Expand Down
110 changes: 101 additions & 9 deletions libs/openFrameworks/utils/ofXml.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "pugixml.hpp"
#include "ofParameter.h"

template<class It>
template<typename It>
class ofXmlIterator;
class ofXmlAttributeIterator;
class ofXmlSearchIterator;

class ofXml{
Expand Down Expand Up @@ -80,8 +81,14 @@ class ofXml{
template<class It>
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<pugi::xml_document> doc, pugi::xml_object_range<typename It::Base> range)
Expand Down Expand Up @@ -130,7 +137,7 @@ class ofXml{


Attribute getAttribute(const std::string & name) const;
Range<ofXmlIterator<pugi::xml_attribute_iterator>> getAttributes() const;
Range<ofXmlAttributeIterator> getAttributes() const;
Attribute getFirstAttribute() const;
Attribute getLastAttribute() const;
Attribute appendAttribute(const std::string & name);
Expand Down Expand Up @@ -203,12 +210,13 @@ class ofXml{
std::shared_ptr<pugi::xml_document> doc;
pugi::xml_node xml;

template<class It>
template<typename It>
friend class ofXmlIterator;
friend class ofXmlAttributeIterator;
friend class ofXmlSearchIterator;
};

template<class It>
template<typename It>
class ofXmlIterator{
public:
ofXmlIterator(){}
Expand All @@ -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;
}

Expand All @@ -252,17 +268,93 @@ 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<pugi::xml_document> doc, const pugi::xml_node & xml)
:xml(doc, xml){

}

// Construct an iterator which points to the specified node
ofXmlIterator(ofXml && xml)
:xml(xml){

}
mutable ofXml xml;
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<pugi::xml_document>, const ofXml::Attribute & attr)
:attr(attr){

}

ofXmlAttributeIterator(const ofXml::Attribute & attr)
:attr(attr){

}
ofXml::Attribute attr;
friend class ofXml;
};


class ofXmlSearchIterator{
public:
Expand Down

0 comments on commit d06e435

Please sign in to comment.