node.h 3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once

#include <string>
#include <ios>
#include <vector>
#include <map>
#include "parserstate.h"
#include "exceptions.h"
#include "iterator.h"

namespace YAML
{
	class Content;
	class Scanner;

	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);
		void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const;

		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
40
41
42
43
44
45
46
		// extraction of scalars
		bool Read(std::string& s) const;
		bool Read(int& i) const;
		bool Read(unsigned& u) const;
		bool Read(long& l) const;
		bool Read(float& f) const;
		bool Read(double& d) const;
		bool Read(char& c) const;
47
		bool Read(bool& b) const;
48
49

		// so you can specialize for other values
50
		template <typename T>
51
		friend bool Read(const Node& node, T& value);
52

53
54
		template <typename T>
		friend void operator >> (const Node& node, T& value);
55

56
		// just for maps
57
		template <typename T>
58
		const Node& GetValue(const T& key) const;
59

60
61
62
63
		template <typename T>
		const Node& operator [] (const T& key) const;

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

65
		// just for sequences
66
67
68
69
70
71
72
73
74
75
		const Node& operator [] (unsigned u) const;
		const Node& operator [] (int i) const;

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

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

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

81
82
83
84
85
86
87
	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:
88
		int m_line, m_column;
89
90
91
92
		std::string m_anchor, m_tag;
		Content *m_pContent;
		bool m_alias;
	};
93
94
95
96
97
98
99
100
101
102
103
104

	// templated things we need to keep inline in the header
	template <typename T>
	inline bool Read(const Node& node, T& value)
	{
		return node.Read(value);
	}

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

	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;
			if(YAML::Read(it.first(), t)) {
				if(key == t)
					return it.second();
			}
		}

122
		throw KeyNotFound(m_line, m_column);
123
124
125
126
127
128
129
130
131
132
133
134
	}

	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));
	}
135
}