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:
Jesse Beder
2008-07-21 02:54:39 +00:00
parent 09d7ab365f
commit cc87c83b01
10 changed files with 143 additions and 68 deletions

View File

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

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

10
src/ltnode.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace YAML
{
class Node;
struct ltnode {
bool operator()(const Node *pNode1, const Node *pNode2) const;
};
}

View File

@@ -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 <Node *>::const_iterator seqIter;
if(m_pContent->GetBegin(seqIter))
return Iterator(seqIter);
return Iterator(new IterPriv(seqIter));
std::map <Node *, Node *, ltnode>::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 <Node *>::const_iterator seqIter;
if(m_pContent->GetEnd(seqIter))
return Iterator(seqIter);
return Iterator(new IterPriv(seqIter));
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
if(m_pContent->GetEnd(mapIter))
return Iterator(mapIter);
return Iterator(new IterPriv(mapIter));
return Iterator();
}