diff --git a/content.h b/content.h index 525a10a8d7..298f4db54e 100644 --- a/content.h +++ b/content.h @@ -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 ::const_iterator& it) { return false; } - virtual bool GetBegin(std::map ::const_iterator& it) { return false; } - virtual bool GetEnd(std::vector ::const_iterator& it) { return false; } - virtual bool GetEnd(std::map ::const_iterator& it) { return false; } + virtual bool GetBegin(std::vector ::const_iterator& it) const { return false; } + virtual bool GetBegin(std::map ::const_iterator& it) const { return false; } + virtual bool GetEnd(std::vector ::const_iterator& it) const { return false; } + virtual bool GetEnd(std::map ::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(); } diff --git a/main.cpp b/main.cpp index 3b0fd885c6..acf1114c6c 100644 --- a/main.cpp +++ b/main.cpp @@ -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> 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"; diff --git a/map.cpp b/map.cpp index a6da682db4..25cb504250 100644 --- a/map.cpp +++ b/map.cpp @@ -24,13 +24,13 @@ namespace YAML m_data.clear(); } - bool Map::GetBegin(std::map ::const_iterator& it) + bool Map::GetBegin(std::map ::const_iterator& it) const { it = m_data.begin(); return true; } - bool Map::GetEnd(std::map ::const_iterator& it) + bool Map::GetEnd(std::map ::const_iterator& it) const { it = m_data.end(); return true; diff --git a/map.h b/map.h index bceb7058ff..14ddd302eb 100644 --- a/map.h +++ b/map.h @@ -14,8 +14,8 @@ namespace YAML virtual ~Map(); void Clear(); - virtual bool GetBegin(std::map ::const_iterator& it); - virtual bool GetEnd(std::map ::const_iterator& it); + virtual bool GetBegin(std::map ::const_iterator& it) const; + virtual bool GetEnd(std::map ::const_iterator& it) const; virtual void Parse(Scanner *pScanner, const ParserState& state); virtual void Write(std::ostream& out, int indent); diff --git a/node.cpp b/node.cpp index 4f9e7d9224..39ff95832a 100644 --- a/node.cpp +++ b/node.cpp @@ -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 diff --git a/node.h b/node.h index 8a76c7191f..47f659b39d 100644 --- a/node.h +++ b/node.h @@ -50,9 +50,10 @@ namespace YAML Iterator begin() const; Iterator end() const; + unsigned size() const; template - 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 + 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); diff --git a/sequence.cpp b/sequence.cpp index 40dc3e058d..fce8639682 100644 --- a/sequence.cpp +++ b/sequence.cpp @@ -22,18 +22,30 @@ namespace YAML m_data.clear(); } - bool Sequence::GetBegin(std::vector ::const_iterator& it) + bool Sequence::GetBegin(std::vector ::const_iterator& it) const { it = m_data.begin(); return true; } - bool Sequence::GetEnd(std::vector ::const_iterator& it) + bool Sequence::GetEnd(std::vector ::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(); diff --git a/sequence.h b/sequence.h index 389711299e..5be190e882 100644 --- a/sequence.h +++ b/sequence.h @@ -14,8 +14,10 @@ namespace YAML virtual ~Sequence(); void Clear(); - virtual bool GetBegin(std::vector ::const_iterator& it); - virtual bool GetEnd(std::vector ::const_iterator& it); + virtual bool GetBegin(std::vector ::const_iterator& it) const; + virtual bool GetEnd(std::vector ::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);