Overloaded the iterator's -> operator.

This commit is contained in:
Jesse Beder 2008-07-02 01:32:19 +00:00
parent d56b54b34f
commit 901d16a96f
4 changed files with 18 additions and 17 deletions

View file

@ -37,6 +37,14 @@ namespace YAML
throw BadDereference(); throw BadDereference();
} }
const Node *Node::Iterator::operator -> ()
{
if(type == IT_SEQ)
return &**seqIter;
throw BadDereference();
}
const Node& Node::Iterator::first() const Node& Node::Iterator::first()
{ {
if(type == IT_MAP) if(type == IT_MAP)

View file

@ -18,17 +18,13 @@ int main()
const YAML::Node& root = doc.GetRoot(); const YAML::Node& root = doc.GetRoot();
for(YAML::Node::Iterator it=root.begin();it!=root.end();++it) { for(YAML::Node::Iterator it=root.begin();it!=root.end();++it) {
std::string name; std::cout << "Sequence:";
(*it)["name"] >> name; for(YAML::Node::Iterator jt=it->begin();jt!=it->end();++jt) {
std::cout << "Name: " << name << std::endl; int value;
*jt >> value;
int age; std::cout << " " << value;
(*it)["age"] >> age; }
std::cout << "Age: " << age << std::endl; std::cout << std::endl;
std::string school;
(*it)["school"] >> school;
std::cout << "School: " << school << std::endl;
} }
} catch(YAML::Exception& e) { } catch(YAML::Exception& e) {
std::cout << "Error parsing the yaml!\n"; std::cout << "Error parsing the yaml!\n";

1
node.h
View file

@ -27,6 +27,7 @@ namespace YAML
friend bool operator != (const Iterator& it, const Iterator& jt); friend bool operator != (const Iterator& it, const Iterator& jt);
Iterator& operator ++ (); Iterator& operator ++ ();
const Node& operator * (); const Node& operator * ();
const Node *operator -> ();
const Node& first(); const Node& first();
const Node& second(); const Node& second();

View file

@ -1,8 +1,4 @@
--- ---
- name: Jesse - [1, 2, 3]
age: 23 - [2, 4, 6]
school: University of Illinois
- name: Naftali
age: 21
school: Rhode Island School of Design
... ...