Switched the Iterator implementation to a dedicated helper class (to hide the specific implementation, since it's pretty messy and may change).

This commit is contained in:
beder
2008-07-21 02:54:39 +00:00
parent 557f81e622
commit 57255a9898
10 changed files with 143 additions and 68 deletions

29
include/iterator.h Normal file
View File

@@ -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;
};
}

View File

@@ -6,7 +6,7 @@
#include <map> #include <map>
#include "parserstate.h" #include "parserstate.h"
#include "exceptions.h" #include "exceptions.h"
#include "ltnode.h" #include "iterator.h"
namespace YAML namespace YAML
{ {
@@ -15,32 +15,6 @@ namespace YAML
class Node class Node
{ {
public:
class Iterator
{
public:
Iterator();
Iterator(std::vector <Node *>::const_iterator it);
Iterator(std::map <Node *, Node *, ltnode>::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 <Node *>::const_iterator seqIter;
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
};
public: public:
Node(); Node();
~Node(); ~Node();

View File

@@ -3,4 +3,5 @@
#include "crt.h" #include "crt.h"
#include "parser.h" #include "parser.h"
#include "node.h" #include "node.h"
#include "iterator.h"
#include "exceptions.h" #include "exceptions.h"

View File

@@ -1,93 +1,107 @@
#include "crt.h" #include "crt.h"
#include "node.h" #include "node.h"
#include "exceptions.h" #include "exceptions.h"
#include "iterpriv.h"
namespace YAML 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 <Node *>::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 <Node *, Node *, ltnode>::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) if(m_pData->type == IterPriv::IT_SEQ)
++seqIter; ++m_pData->seqIter;
else if(type == IT_MAP) else if(m_pData->type == IterPriv::IT_MAP)
++mapIter; ++m_pData->mapIter;
return *this; return *this;
} }
Node::Iterator Node::Iterator::operator ++ (int) Iterator Iterator::operator ++ (int)
{ {
Iterator temp = *this; Iterator temp = *this;
if(type == IT_SEQ) if(m_pData->type == IterPriv::IT_SEQ)
++seqIter; ++m_pData->seqIter;
else if(type == IT_MAP) else if(m_pData->type == IterPriv::IT_MAP)
++mapIter; ++m_pData->mapIter;
return temp; return temp;
} }
const Node& Node::Iterator::operator * () const Node& Iterator::operator * ()
{ {
if(type == IT_SEQ) if(m_pData->type == IterPriv::IT_SEQ)
return **seqIter; return **m_pData->seqIter;
throw BadDereference(); throw BadDereference();
} }
const Node *Node::Iterator::operator -> () const Node *Iterator::operator -> ()
{ {
if(type == IT_SEQ) if(m_pData->type == IterPriv::IT_SEQ)
return &**seqIter; return &**m_pData->seqIter;
throw BadDereference(); throw BadDereference();
} }
const Node& Node::Iterator::first() const Node& Iterator::first()
{ {
if(type == IT_MAP) if(m_pData->type == IterPriv::IT_MAP)
return *mapIter->first; return *m_pData->mapIter->first;
throw BadDereference(); throw BadDereference();
} }
const Node& Node::Iterator::second() const Node& Iterator::second()
{ {
if(type == IT_MAP) if(m_pData->type == IterPriv::IT_MAP)
return *mapIter->second; return *m_pData->mapIter->second;
throw BadDereference(); 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; return false;
if(it.type == Node::Iterator::IT_SEQ) if(it.m_pData->type == IterPriv::IT_SEQ)
return it.seqIter == jt.seqIter; return it.m_pData->seqIter == jt.m_pData->seqIter;
else if(it.type == Node::Iterator::IT_MAP) else if(it.m_pData->type == IterPriv::IT_MAP)
return it.mapIter == jt.mapIter; return it.m_pData->mapIter == jt.m_pData->mapIter;
return true; return true;
} }
bool operator != (const Node::Iterator& it, const Node::Iterator& jt) bool operator != (const Iterator& it, const Iterator& jt)
{ {
return !(it == jt); return !(it == jt);
} }

17
src/iterpriv.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "crt.h"
#include "iterpriv.h"
namespace YAML
{
IterPriv::IterPriv(): type(IT_NONE)
{
}
IterPriv::IterPriv(std::vector <Node *>::const_iterator it): seqIter(it), type(IT_SEQ)
{
}
IterPriv::IterPriv(std::map <Node *, Node *, ltnode>::const_iterator it): mapIter(it), type(IT_MAP)
{
}
}

23
src/iterpriv.h Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include "ltnode.h"
#include <vector>
#include <map>
namespace YAML
{
class Node;
struct IterPriv
{
IterPriv();
IterPriv(std::vector <Node *>::const_iterator it);
IterPriv(std::map <Node *, Node *, ltnode>::const_iterator it);
enum ITER_TYPE { IT_NONE, IT_SEQ, IT_MAP };
ITER_TYPE type;
std::vector <Node *>::const_iterator seqIter;
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
};
}

View File

@@ -7,6 +7,7 @@
#include "scalar.h" #include "scalar.h"
#include "sequence.h" #include "sequence.h"
#include "map.h" #include "map.h"
#include "iterpriv.h"
namespace YAML namespace YAML
{ {
@@ -145,36 +146,36 @@ namespace YAML
// begin // begin
// Returns an iterator to the beginning of this (sequence or map). // Returns an iterator to the beginning of this (sequence or map).
Node::Iterator Node::begin() const Iterator Node::begin() const
{ {
if(!m_pContent) if(!m_pContent)
return Iterator(); return Iterator();
std::vector <Node *>::const_iterator seqIter; std::vector <Node *>::const_iterator seqIter;
if(m_pContent->GetBegin(seqIter)) if(m_pContent->GetBegin(seqIter))
return Iterator(seqIter); return Iterator(new IterPriv(seqIter));
std::map <Node *, Node *, ltnode>::const_iterator mapIter; std::map <Node *, Node *, ltnode>::const_iterator mapIter;
if(m_pContent->GetBegin(mapIter)) if(m_pContent->GetBegin(mapIter))
return Iterator(mapIter); return Iterator(new IterPriv(mapIter));
return Iterator(); return Iterator();
} }
// end // end
// . Returns an iterator to the end of this (sequence or map). // . Returns an iterator to the end of this (sequence or map).
Node::Iterator Node::end() const Iterator Node::end() const
{ {
if(!m_pContent) if(!m_pContent)
return Iterator(); return Iterator();
std::vector <Node *>::const_iterator seqIter; std::vector <Node *>::const_iterator seqIter;
if(m_pContent->GetEnd(seqIter)) if(m_pContent->GetEnd(seqIter))
return Iterator(seqIter); return Iterator(new IterPriv(seqIter));
std::map <Node *, Node *, ltnode>::const_iterator mapIter; std::map <Node *, Node *, ltnode>::const_iterator mapIter;
if(m_pContent->GetEnd(mapIter)) if(m_pContent->GetEnd(mapIter))
return Iterator(mapIter); return Iterator(new IterPriv(mapIter));
return Iterator(); return Iterator();
} }

View File

@@ -20,7 +20,11 @@ void run()
YAML::Node doc; YAML::Node doc;
parser.GetNextDocument(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&) { } catch(YAML::Exception&) {
std::cout << "Error parsing the yaml!\n"; std::cout << "Error parsing the yaml!\n";
} }

View File

@@ -175,6 +175,10 @@
RelativePath=".\src\iterator.cpp" RelativePath=".\src\iterator.cpp"
> >
</File> </File>
<File
RelativePath=".\src\iterpriv.cpp"
>
</File>
<File <File
RelativePath=".\src\map.cpp" RelativePath=".\src\map.cpp"
> >
@@ -262,7 +266,15 @@
> >
</File> </File>
<File <File
RelativePath=".\include\ltnode.h" RelativePath=".\include\iterator.h"
>
</File>
<File
RelativePath=".\src\iterpriv.h"
>
</File>
<File
RelativePath=".\src\ltnode.h"
> >
</File> </File>
<File <File