node.h 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
#pragma once

#include <string>
#include <vector>
#include <map>
#include "parserstate.h"
#include "exceptions.h"
#include "iterator.h"
9
#include "conversion.h"
10
11
12
13
14

namespace YAML
{
	class Content;
	class Scanner;
15
	class Emitter;
16
17
18
19
20
21
22
23
24
25
26
27
28
29

	enum CONTENT_TYPE { CT_NONE, CT_SCALAR, CT_SEQUENCE, CT_MAP };

	class Node
	{
	public:
		Node();
		~Node();

		void Clear();
		void Parse(Scanner *pScanner, const ParserState& state);

		CONTENT_TYPE GetType() const;

30
31
32
33
		// file location of start of this node
		int GetLine() const { return m_line; }
		int GetColumn() const { return m_column; }

34
35
36
37
38
		// accessors
		Iterator begin() const;
		Iterator end() const;
		unsigned size() const;

39
		// extraction of scalars
40
41
42
		bool GetScalar(std::string& s) const;

		// we can specialize this for other values
43
		template <typename T>
44
		bool Read(T& value) const;
45

46
47
		template <typename T>
		friend void operator >> (const Node& node, T& value);
48

49
		// just for maps
50
		template <typename T>
51
		const Node& GetValue(const T& key) const;
52

53
54
55
56
		template <typename T>
		const Node& operator [] (const T& key) const;

		const Node& operator [] (const char *key) const;
57

58
		// just for sequences
59
60
61
		const Node& operator [] (unsigned u) const;
		const Node& operator [] (int i) const;

62
63
64
65
66
		// for anchors/aliases
		const Node *Identity() const { return m_pIdentity; }
		bool IsAlias() const { return m_alias; }
		bool IsReferenced() const { return m_referenced; }

67
68
		// emitting
		friend Emitter& operator << (Emitter& out, const Node& node);
69
70
71
72
73

		// ordering
		int Compare(const Node& rhs) const;
		friend bool operator < (const Node& n1, const Node& n2);

jbeder's avatar
jbeder committed
74
75
76
77
78
	private:
		// shouldn't be copyable! (at least for now)
		Node(const Node& rhs);
		Node& operator = (const Node& rhs);

79
80
81
82
83
84
85
	private:
		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);

	private:
86
		int m_line, m_column;
87
88
89
		std::string m_anchor, m_tag;
		Content *m_pContent;
		bool m_alias;
90
91
		const Node *m_pIdentity;
		mutable bool m_referenced;
92
	};
93
94
95

	// templated things we need to keep inline in the header
	template <typename T>
96
97
98
99
100
101
	inline bool Node::Read(T& value) const {
		std::string scalar;
		if(!GetScalar(scalar))
			return false;

		return Convert(scalar, value);
102
103
104
105
106
	}

	template <typename T>
	inline void operator >> (const Node& node, T& value)
	{
107
		if(!node.Read(value))
108
			throw InvalidScalar(node.m_line, node.m_column);
109
110
111
112
113
114
115
116
117
118
	}

	template <typename T>
	inline const Node& Node::GetValue(const T& key) const
	{
		if(!m_pContent)
			throw BadDereference();

		for(Iterator it=begin();it!=end();++it) {
			T t;
119
			if(it.first().Read(t)) {
120
121
122
123
124
				if(key == t)
					return it.second();
			}
		}

125
		throw MakeTypedKeyNotFound(m_line, m_column, key);
126
127
128
129
130
131
132
133
134
135
136
137
	}

	template <typename T>
	inline const Node& Node::operator [] (const T& key) const
	{
		return GetValue(key);
	}

	inline const Node& Node::operator [] (const char *key) const
	{
		return GetValue(std::string(key));
	}
138
}