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:
Jesse Beder 2008-07-02 21:41:54 +00:00
parent 2ccbfeff47
commit 2be40919de
8 changed files with 81 additions and 25 deletions

View file

@ -21,10 +21,12 @@ namespace YAML
virtual void Parse(Scanner *pScanner, const ParserState& state) = 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::map <Node *, Node *>::const_iterator& it) { return false; }
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) { return false; }
virtual bool GetEnd(std::map <Node *, 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) const { return false; }
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) const { 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
virtual void Read(std::string& s) { throw InvalidScalar(); }

View file

@ -14,12 +14,9 @@ struct Vec3 {
void operator >> (const YAML::Node& node, Vec3& v)
{
YAML::Node::Iterator it = node.begin();
*it >> v.x;
++it;
*it >> v.y;
++it;
*it >> v.z;
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
struct Room {
@ -59,9 +56,9 @@ struct Level {
void operator >> (const YAML::Node& node, Level& level)
{
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;
*it >> room;
rooms[i] >> room;
level.rooms.push_back(room);
}
}
@ -78,9 +75,8 @@ int main()
YAML::Document doc;
parser.GetNextDocument(doc);
const YAML::Node& root = doc.GetRoot();
Level level;
root >> level;
doc.GetRoot() >> level;
std::cout << level;
} catch(YAML::Exception&) {
std::cout << "Error parsing the yaml!\n";

View file

@ -24,13 +24,13 @@ namespace YAML
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();
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();
return true;

4
map.h
View file

@ -14,8 +14,8 @@ namespace YAML
virtual ~Map();
void Clear();
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it);
virtual bool GetEnd(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) const;
virtual void Parse(Scanner *pScanner, const ParserState& state);
virtual void Write(std::ostream& out, int indent);

View file

@ -173,6 +173,41 @@ namespace YAML
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

15
node.h
View file

@ -50,9 +50,10 @@ namespace YAML
Iterator begin() const;
Iterator end() const;
unsigned size() const;
template <typename T>
const Node& operator [] (const T& key) const {
const Node& GetValue(const T& key) const {
if(!m_pContent)
throw BadDereference();
@ -69,10 +70,18 @@ namespace YAML
throw BadDereference();
}
const Node& operator [] (const char *key) const {
return operator [] (std::string(key));
template <typename T>
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
friend void operator >> (const Node& node, std::string& s);
friend void operator >> (const Node& node, int& i);

View file

@ -22,18 +22,30 @@ namespace YAML
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();
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();
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)
{
Clear();

View file

@ -14,8 +14,10 @@ namespace YAML
virtual ~Sequence();
void Clear();
virtual bool GetBegin(std::vector <Node *>::const_iterator& it);
virtual bool GetEnd(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) const;
virtual Node *GetNode(unsigned i) const;
virtual unsigned GetSize() const;
virtual void Parse(Scanner *pScanner, const ParserState& state);
virtual void Write(std::ostream& out, int indent);