mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-05 06:21:26 +12:00
Specialized the overloaded [] operator for int/unsigned, and added a size() function, so that you can iterate through a sequence node like a vector.
This commit is contained in:
parent
2ccbfeff47
commit
2be40919de
8 changed files with 81 additions and 25 deletions
10
content.h
10
content.h
|
@ -21,10 +21,12 @@ namespace YAML
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state) = 0;
|
virtual void Parse(Scanner *pScanner, const ParserState& state) = 0;
|
||||||
virtual void Write(std::ostream& out, int indent) = 0;
|
virtual void Write(std::ostream& out, int indent) = 0;
|
||||||
|
|
||||||
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) { return false; }
|
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) const { return false; }
|
||||||
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) { return false; }
|
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const { return false; }
|
||||||
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) { return false; }
|
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) const { return false; }
|
||||||
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it) { return false; }
|
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it) const { return false; }
|
||||||
|
virtual Node *GetNode(unsigned i) const { return 0; }
|
||||||
|
virtual unsigned GetSize() const { return 0; }
|
||||||
|
|
||||||
// extraction
|
// extraction
|
||||||
virtual void Read(std::string& s) { throw InvalidScalar(); }
|
virtual void Read(std::string& s) { throw InvalidScalar(); }
|
||||||
|
|
16
main.cpp
16
main.cpp
|
@ -14,12 +14,9 @@ struct Vec3 {
|
||||||
|
|
||||||
void operator >> (const YAML::Node& node, Vec3& v)
|
void operator >> (const YAML::Node& node, Vec3& v)
|
||||||
{
|
{
|
||||||
YAML::Node::Iterator it = node.begin();
|
node[0] >> v.x;
|
||||||
*it >> v.x;
|
node[1] >> v.y;
|
||||||
++it;
|
node[2] >> v.z;
|
||||||
*it >> v.y;
|
|
||||||
++it;
|
|
||||||
*it >> v.z;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Room {
|
struct Room {
|
||||||
|
@ -59,9 +56,9 @@ struct Level {
|
||||||
void operator >> (const YAML::Node& node, Level& level)
|
void operator >> (const YAML::Node& node, Level& level)
|
||||||
{
|
{
|
||||||
const YAML::Node& rooms = node["rooms"];
|
const YAML::Node& rooms = node["rooms"];
|
||||||
for(YAML::Node::Iterator it=rooms.begin();it!=rooms.end();++it) {
|
for(unsigned i=0;i<rooms.size();i++) {
|
||||||
Room room;
|
Room room;
|
||||||
*it >> room;
|
rooms[i] >> room;
|
||||||
level.rooms.push_back(room);
|
level.rooms.push_back(room);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,9 +75,8 @@ int main()
|
||||||
YAML::Document doc;
|
YAML::Document doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
const YAML::Node& root = doc.GetRoot();
|
|
||||||
Level level;
|
Level level;
|
||||||
root >> level;
|
doc.GetRoot() >> level;
|
||||||
std::cout << level;
|
std::cout << level;
|
||||||
} catch(YAML::Exception&) {
|
} catch(YAML::Exception&) {
|
||||||
std::cout << "Error parsing the yaml!\n";
|
std::cout << "Error parsing the yaml!\n";
|
||||||
|
|
4
map.cpp
4
map.cpp
|
@ -24,13 +24,13 @@ namespace YAML
|
||||||
m_data.clear();
|
m_data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::GetBegin(std::map <Node *, Node *>::const_iterator& it)
|
bool Map::GetBegin(std::map <Node *, Node *>::const_iterator& it) const
|
||||||
{
|
{
|
||||||
it = m_data.begin();
|
it = m_data.begin();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::GetEnd(std::map <Node *, Node *>::const_iterator& it)
|
bool Map::GetEnd(std::map <Node *, Node *>::const_iterator& it) const
|
||||||
{
|
{
|
||||||
it = m_data.end();
|
it = m_data.end();
|
||||||
return true;
|
return true;
|
||||||
|
|
4
map.h
4
map.h
|
@ -14,8 +14,8 @@ namespace YAML
|
||||||
virtual ~Map();
|
virtual ~Map();
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it);
|
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const;
|
||||||
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it);
|
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it) const;
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(std::ostream& out, int indent);
|
virtual void Write(std::ostream& out, int indent);
|
||||||
|
|
||||||
|
|
35
node.cpp
35
node.cpp
|
@ -173,6 +173,41 @@ namespace YAML
|
||||||
return Iterator();
|
return Iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// size
|
||||||
|
// . Returns the size of this node, if it's a sequence node.
|
||||||
|
// . Otherwise, returns zero.
|
||||||
|
unsigned Node::size() const
|
||||||
|
{
|
||||||
|
if(!m_pContent)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return m_pContent->GetSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node& Node::operator [] (unsigned u) const
|
||||||
|
{
|
||||||
|
if(!m_pContent)
|
||||||
|
throw BadDereference();
|
||||||
|
|
||||||
|
Node *pNode = m_pContent->GetNode(u);
|
||||||
|
if(pNode)
|
||||||
|
return *pNode;
|
||||||
|
|
||||||
|
return GetValue(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node& Node::operator [] (int i) const
|
||||||
|
{
|
||||||
|
if(!m_pContent)
|
||||||
|
throw BadDereference();
|
||||||
|
|
||||||
|
Node *pNode = m_pContent->GetNode(i);
|
||||||
|
if(pNode)
|
||||||
|
return *pNode;
|
||||||
|
|
||||||
|
return GetValue(i);
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
// Extraction
|
// Extraction
|
||||||
|
|
||||||
|
|
15
node.h
15
node.h
|
@ -50,9 +50,10 @@ namespace YAML
|
||||||
|
|
||||||
Iterator begin() const;
|
Iterator begin() const;
|
||||||
Iterator end() const;
|
Iterator end() const;
|
||||||
|
unsigned size() const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const Node& operator [] (const T& key) const {
|
const Node& GetValue(const T& key) const {
|
||||||
if(!m_pContent)
|
if(!m_pContent)
|
||||||
throw BadDereference();
|
throw BadDereference();
|
||||||
|
|
||||||
|
@ -69,10 +70,18 @@ namespace YAML
|
||||||
throw BadDereference();
|
throw BadDereference();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Node& operator [] (const char *key) const {
|
template <typename T>
|
||||||
return operator [] (std::string(key));
|
const Node& operator [] (const T& key) const {
|
||||||
|
return GetValue(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Node& operator [] (const char *key) const {
|
||||||
|
return GetValue(std::string(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node& operator [] (unsigned u) const;
|
||||||
|
const Node& operator [] (int i) const;
|
||||||
|
|
||||||
// extraction
|
// extraction
|
||||||
friend void operator >> (const Node& node, std::string& s);
|
friend void operator >> (const Node& node, std::string& s);
|
||||||
friend void operator >> (const Node& node, int& i);
|
friend void operator >> (const Node& node, int& i);
|
||||||
|
|
16
sequence.cpp
16
sequence.cpp
|
@ -22,18 +22,30 @@ namespace YAML
|
||||||
m_data.clear();
|
m_data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sequence::GetBegin(std::vector <Node *>::const_iterator& it)
|
bool Sequence::GetBegin(std::vector <Node *>::const_iterator& it) const
|
||||||
{
|
{
|
||||||
it = m_data.begin();
|
it = m_data.begin();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sequence::GetEnd(std::vector <Node *>::const_iterator& it)
|
bool Sequence::GetEnd(std::vector <Node *>::const_iterator& it) const
|
||||||
{
|
{
|
||||||
it = m_data.end();
|
it = m_data.end();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node *Sequence::GetNode(unsigned i) const
|
||||||
|
{
|
||||||
|
if(i < m_data.size())
|
||||||
|
return m_data[i];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned Sequence::GetSize() const
|
||||||
|
{
|
||||||
|
return m_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
void Sequence::Parse(Scanner *pScanner, const ParserState& state)
|
void Sequence::Parse(Scanner *pScanner, const ParserState& state)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
|
|
|
@ -14,8 +14,10 @@ namespace YAML
|
||||||
virtual ~Sequence();
|
virtual ~Sequence();
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
virtual bool GetBegin(std::vector <Node *>::const_iterator& it);
|
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) const;
|
||||||
virtual bool GetEnd(std::vector <Node *>::const_iterator& it);
|
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) const;
|
||||||
|
virtual Node *GetNode(unsigned i) const;
|
||||||
|
virtual unsigned GetSize() const;
|
||||||
|
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(std::ostream& out, int indent);
|
virtual void Write(std::ostream& out, int indent);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue