node.h 2.69 KB
Newer Older
beder's avatar
beder committed
1
2
3
#pragma once

#include <string>
4
#include <ios>
5
6
#include <vector>
#include <map>
7
#include "parserstate.h"
8
#include "exceptions.h"
beder's avatar
beder committed
9
10
11

namespace YAML
{
12
	class Content;
13
	class Scanner;
beder's avatar
beder committed
14
15
16

	class Node
	{
17
18
19
20
21
22
23
24
25
26
27
28
	public:
		class Iterator
		{
		public:
			Iterator();
			Iterator(std::vector <Node *>::const_iterator it);
			Iterator(std::map <Node *, Node *>::const_iterator it);
			~Iterator();

			friend bool operator == (const Iterator& it, const Iterator& jt);
			friend bool operator != (const Iterator& it, const Iterator& jt);
			Iterator& operator ++ ();
beder's avatar
beder committed
29
			Iterator operator ++ (int);
30
			const Node& operator * ();
beder's avatar
beder committed
31
			const Node *operator -> ();
32
33
34
35
36
37
38
39
40
41
42
			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 *>::const_iterator mapIter;
		};

beder's avatar
beder committed
43
44
45
46
	public:
		Node();
		~Node();

47
		void Clear();
48
		void Parse(Scanner *pScanner, const ParserState& state);
49
		void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const;
50

51
		// accessors
52
53
		Iterator begin() const;
		Iterator end() const;
54
		unsigned size() const;
55
56

		template <typename T>
57
		const Node& GetValue(const T& key) const {
58
59
60
61
62
63
64
65
66
			if(!m_pContent)
				throw BadDereference();

			for(Iterator it=begin();it!=end();++it) {
				T t;
				try {
					it.first() >> t;
					if(key == t)
						return it.second();
beder's avatar
beder committed
67
				} catch(RepresentationException&) {
68
69
70
71
72
73
				}
			}

			throw BadDereference();
		}

74
75
76
77
78
		template <typename T>
		const Node& operator [] (const T& key) const {
			return GetValue(key);
		}

79
		const Node& operator [] (const char *key) const {
80
			return GetValue(std::string(key));
81
82
		}

83
84
85
		const Node& operator [] (unsigned u) const;
		const Node& operator [] (int i) const;

86
87
88
89
90
91
92
93
94
		// extraction
		friend void operator >> (const Node& node, std::string& s);
		friend void operator >> (const Node& node, int& i);
		friend void operator >> (const Node& node, unsigned& u);
		friend void operator >> (const Node& node, long& l);
		friend void operator >> (const Node& node, float& f);
		friend void operator >> (const Node& node, double& d);
		friend void operator >> (const Node& node, char& c);

95
96
97
		// insertion
		friend std::ostream& operator << (std::ostream& out, const Node& node);

98
	private:
99
100
101
102
		void ParseHeader(Scanner *pScanner, const ParserState& state);
		void ParseTag(Scanner *pScanner, const ParserState& state);
		void ParseAnchor(Scanner *pScanner, const ParserState& state);
		void ParseAlias(Scanner *pScanner, const ParserState& state);
103

beder's avatar
beder committed
104
	private:
105
106
		bool m_alias;
		std::string m_anchor, m_tag;
107
		Content *m_pContent;
beder's avatar
beder committed
108
109
	};
}