mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-04-28 13:28:01 +03:00
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:
parent
09d7ab365f
commit
cc87c83b01
10 changed files with 143 additions and 68 deletions
29
include/iterator.h
Normal file
29
include/iterator.h
Normal 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;
|
||||
};
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
#include <map>
|
||||
#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 <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:
|
||||
Node();
|
||||
~Node();
|
||||
|
|
|
@ -3,4 +3,5 @@
|
|||
#include "crt.h"
|
||||
#include "parser.h"
|
||||
#include "node.h"
|
||||
#include "iterator.h"
|
||||
#include "exceptions.h"
|
|
@ -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
17
src/iterpriv.cpp
Normal 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
23
src/iterpriv.h
Normal 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;
|
||||
};
|
||||
}
|
13
src/node.cpp
13
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 <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();
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -175,6 +175,10 @@
|
|||
RelativePath=".\src\iterator.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\iterpriv.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\map.cpp"
|
||||
>
|
||||
|
@ -262,7 +266,15 @@
|
|||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\ltnode.h"
|
||||
RelativePath=".\include\iterator.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\iterpriv.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\ltnode.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue