From cc87c83b01b19feeee3f3f9e446d1771110b1dd2 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Mon, 21 Jul 2008 02:54:39 +0000 Subject: [PATCH] Switched the Iterator implementation to a dedicated helper class (to hide the specific implementation, since it's pretty messy and may change). --- include/iterator.h | 29 ++++++++++++++ include/node.h | 28 +------------- include/yaml.h | 1 + src/iterator.cpp | 80 +++++++++++++++++++++++---------------- src/iterpriv.cpp | 17 +++++++++ src/iterpriv.h | 23 +++++++++++ {include => src}/ltnode.h | 0 src/node.cpp | 13 ++++--- yaml-reader/main.cpp | 6 ++- yamlcpp.vcproj | 14 ++++++- 10 files changed, 143 insertions(+), 68 deletions(-) create mode 100644 include/iterator.h create mode 100644 src/iterpriv.cpp create mode 100644 src/iterpriv.h rename {include => src}/ltnode.h (100%) diff --git a/include/iterator.h b/include/iterator.h new file mode 100644 index 0000000..b0b7e34 --- /dev/null +++ b/include/iterator.h @@ -0,0 +1,29 @@ +#pragma once + +namespace YAML +{ + class Node; + struct IterPriv; + + class Iterator + { + public: + Iterator(); + Iterator(IterPriv *pData); + Iterator(const Iterator& rhs); + ~Iterator(); + + friend bool operator == (const Iterator& it, const Iterator& jt); + friend bool operator != (const Iterator& it, const Iterator& jt); + Iterator& operator = (const Iterator& rhs); + Iterator& operator ++ (); + Iterator operator ++ (int); + const Node& operator * (); + const Node *operator -> (); + const Node& first(); + const Node& second(); + + private: + IterPriv *m_pData; + }; +} diff --git a/include/node.h b/include/node.h index 9bc88ca..f6a2ef6 100644 --- a/include/node.h +++ b/include/node.h @@ -6,7 +6,7 @@ #include #include "parserstate.h" #include "exceptions.h" -#include "ltnode.h" +#include "iterator.h" namespace YAML { @@ -15,32 +15,6 @@ namespace YAML class Node { - public: - class Iterator - { - public: - Iterator(); - Iterator(std::vector ::const_iterator it); - Iterator(std::map ::const_iterator it); - ~Iterator(); - - friend bool operator == (const Iterator& it, const Iterator& jt); - friend bool operator != (const Iterator& it, const Iterator& jt); - Iterator& operator ++ (); - Iterator operator ++ (int); - const Node& operator * (); - const Node *operator -> (); - const Node& first(); - const Node& second(); - - private: - enum ITER_TYPE { IT_NONE, IT_SEQ, IT_MAP }; - ITER_TYPE type; - - std::vector ::const_iterator seqIter; - std::map ::const_iterator mapIter; - }; - public: Node(); ~Node(); diff --git a/include/yaml.h b/include/yaml.h index 08f79ca..de7f222 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -3,4 +3,5 @@ #include "crt.h" #include "parser.h" #include "node.h" +#include "iterator.h" #include "exceptions.h" \ No newline at end of file diff --git a/src/iterator.cpp b/src/iterator.cpp index b39cec3..bc3233a 100644 --- a/src/iterator.cpp +++ b/src/iterator.cpp @@ -1,93 +1,107 @@ #include "crt.h" #include "node.h" #include "exceptions.h" +#include "iterpriv.h" namespace YAML { - Node::Iterator::Iterator(): type(IT_NONE) + Iterator::Iterator(): m_pData(0) + { + m_pData = new IterPriv; + } + + Iterator::Iterator(IterPriv *pData): m_pData(pData) { } - Node::Iterator::Iterator(std::vector ::const_iterator it): seqIter(it), type(IT_SEQ) + Iterator::Iterator(const Iterator& rhs): m_pData(0) { + m_pData = new IterPriv(*rhs.m_pData); } - Node::Iterator::Iterator(std::map ::const_iterator it): mapIter(it), type(IT_MAP) + Iterator& Iterator::operator = (const Iterator& rhs) { + if(this == &rhs) + return *this; + + delete m_pData; + m_pData = new IterPriv(*rhs.m_pData); + return *this; } - Node::Iterator::~Iterator() + Iterator::~Iterator() { + delete m_pData; } - Node::Iterator& Node::Iterator::operator ++ () + Iterator& Iterator::operator ++ () { - if(type == IT_SEQ) - ++seqIter; - else if(type == IT_MAP) - ++mapIter; + if(m_pData->type == IterPriv::IT_SEQ) + ++m_pData->seqIter; + else if(m_pData->type == IterPriv::IT_MAP) + ++m_pData->mapIter; return *this; } - Node::Iterator Node::Iterator::operator ++ (int) + Iterator Iterator::operator ++ (int) { Iterator temp = *this; - if(type == IT_SEQ) - ++seqIter; - else if(type == IT_MAP) - ++mapIter; + if(m_pData->type == IterPriv::IT_SEQ) + ++m_pData->seqIter; + else if(m_pData->type == IterPriv::IT_MAP) + ++m_pData->mapIter; return temp; } - const Node& Node::Iterator::operator * () + const Node& Iterator::operator * () { - if(type == IT_SEQ) - return **seqIter; + if(m_pData->type == IterPriv::IT_SEQ) + return **m_pData->seqIter; throw BadDereference(); } - const Node *Node::Iterator::operator -> () + const Node *Iterator::operator -> () { - if(type == IT_SEQ) - return &**seqIter; + if(m_pData->type == IterPriv::IT_SEQ) + return &**m_pData->seqIter; throw BadDereference(); } - const Node& Node::Iterator::first() + const Node& Iterator::first() { - if(type == IT_MAP) - return *mapIter->first; + if(m_pData->type == IterPriv::IT_MAP) + return *m_pData->mapIter->first; throw BadDereference(); } - const Node& Node::Iterator::second() + const Node& Iterator::second() { - if(type == IT_MAP) - return *mapIter->second; + if(m_pData->type == IterPriv::IT_MAP) + return *m_pData->mapIter->second; throw BadDereference(); } - bool operator == (const Node::Iterator& it, const Node::Iterator& jt) + bool operator == (const Iterator& it, const Iterator& jt) { - if(it.type != jt.type) + if(it.m_pData->type != jt.m_pData->type) return false; - if(it.type == Node::Iterator::IT_SEQ) - return it.seqIter == jt.seqIter; - else if(it.type == Node::Iterator::IT_MAP) - return it.mapIter == jt.mapIter; + if(it.m_pData->type == IterPriv::IT_SEQ) + return it.m_pData->seqIter == jt.m_pData->seqIter; + else if(it.m_pData->type == IterPriv::IT_MAP) + return it.m_pData->mapIter == jt.m_pData->mapIter; return true; } - bool operator != (const Node::Iterator& it, const Node::Iterator& jt) + bool operator != (const Iterator& it, const Iterator& jt) { return !(it == jt); } diff --git a/src/iterpriv.cpp b/src/iterpriv.cpp new file mode 100644 index 0000000..e2ff8f2 --- /dev/null +++ b/src/iterpriv.cpp @@ -0,0 +1,17 @@ +#include "crt.h" +#include "iterpriv.h" + +namespace YAML +{ + IterPriv::IterPriv(): type(IT_NONE) + { + } + + IterPriv::IterPriv(std::vector ::const_iterator it): seqIter(it), type(IT_SEQ) + { + } + + IterPriv::IterPriv(std::map ::const_iterator it): mapIter(it), type(IT_MAP) + { + } +} diff --git a/src/iterpriv.h b/src/iterpriv.h new file mode 100644 index 0000000..5946ee9 --- /dev/null +++ b/src/iterpriv.h @@ -0,0 +1,23 @@ +#pragma once + +#include "ltnode.h" +#include +#include + +namespace YAML +{ + class Node; + + struct IterPriv + { + IterPriv(); + IterPriv(std::vector ::const_iterator it); + IterPriv(std::map ::const_iterator it); + + enum ITER_TYPE { IT_NONE, IT_SEQ, IT_MAP }; + ITER_TYPE type; + + std::vector ::const_iterator seqIter; + std::map ::const_iterator mapIter; + }; +} diff --git a/include/ltnode.h b/src/ltnode.h similarity index 100% rename from include/ltnode.h rename to src/ltnode.h diff --git a/src/node.cpp b/src/node.cpp index 1250aa0..c66d001 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -7,6 +7,7 @@ #include "scalar.h" #include "sequence.h" #include "map.h" +#include "iterpriv.h" namespace YAML { @@ -145,36 +146,36 @@ namespace YAML // begin // Returns an iterator to the beginning of this (sequence or map). - Node::Iterator Node::begin() const + Iterator Node::begin() const { if(!m_pContent) return Iterator(); std::vector ::const_iterator seqIter; if(m_pContent->GetBegin(seqIter)) - return Iterator(seqIter); + return Iterator(new IterPriv(seqIter)); std::map ::const_iterator mapIter; if(m_pContent->GetBegin(mapIter)) - return Iterator(mapIter); + return Iterator(new IterPriv(mapIter)); return Iterator(); } // end // . Returns an iterator to the end of this (sequence or map). - Node::Iterator Node::end() const + Iterator Node::end() const { if(!m_pContent) return Iterator(); std::vector ::const_iterator seqIter; if(m_pContent->GetEnd(seqIter)) - return Iterator(seqIter); + return Iterator(new IterPriv(seqIter)); std::map ::const_iterator mapIter; if(m_pContent->GetEnd(mapIter)) - return Iterator(mapIter); + return Iterator(new IterPriv(mapIter)); return Iterator(); } diff --git a/yaml-reader/main.cpp b/yaml-reader/main.cpp index 3f47840..c7ef6ac 100644 --- a/yaml-reader/main.cpp +++ b/yaml-reader/main.cpp @@ -20,7 +20,11 @@ void run() YAML::Node doc; parser.GetNextDocument(doc); - std::cout << doc; + for(YAML::Iterator it=doc.begin();it!=doc.end();++it) { + std::string item; + *it >> item; + std::cout << item << "\n"; + } } catch(YAML::Exception&) { std::cout << "Error parsing the yaml!\n"; } diff --git a/yamlcpp.vcproj b/yamlcpp.vcproj index 0bd0e81..7d82294 100644 --- a/yamlcpp.vcproj +++ b/yamlcpp.vcproj @@ -175,6 +175,10 @@ RelativePath=".\src\iterator.cpp" > + + @@ -262,7 +266,15 @@ > + + + +