node.h 2.56 KB
Newer Older
1
2
#pragma once

jbeder's avatar
jbeder committed
3
4
5
6
#ifndef NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66


7
#include "conversion.h"
8
9
#include "exceptions.h"
#include "iterator.h"
10
#include "mark.h"
11
#include "noncopyable.h"
12
#include "parserstate.h"
13
#include <iostream>
14
15
16
#include <string>
#include <vector>
#include <map>
17
18
19
20
21

namespace YAML
{
	class Content;
	class Scanner;
22
	class Emitter;
23
24
25

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

26
	class Node: private noncopyable
27
28
29
30
31
32
33
34
35
36
	{
	public:
		Node();
		~Node();

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

		CONTENT_TYPE GetType() const;

37
		// file location of start of this node
38
		const Mark GetMark() const { return m_mark; }
39

40
41
42
		// accessors
		Iterator begin() const;
		Iterator end() const;
43
		std::size_t size() const;
44

45
		// extraction of scalars
46
47
48
		bool GetScalar(std::string& s) const;

		// we can specialize this for other values
49
		template <typename T>
50
		bool Read(T& value) const;
51

52
53
54
		template <typename T>
		const T Read() const;

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

58
		// retrieval for maps and sequences
59
		template <typename T>
60
		const Node *FindValue(const T& key) const;
61

62
63
		template <typename T>
		const Node& operator [] (const T& key) const;
64
65
66
		
		// specific to maps
		const Node *FindValue(const char *key) const;
67
		const Node& operator [] (const char *key) const;
68

69
70
71
72
73
		// for anchors/aliases
		const Node *Identity() const { return m_pIdentity; }
		bool IsAlias() const { return m_alias; }
		bool IsReferenced() const { return m_referenced; }

74
75
		// emitting
		friend Emitter& operator << (Emitter& out, const Node& node);
76
77
78
79
80

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

jbeder's avatar
jbeder committed
81
	private:
82
83
84
85
		// helper for sequences
		template <typename, bool> friend struct _FindFromNodeAtIndex;
		const Node *FindAtIndex(std::size_t i) const;
		
86
87
88
		// helper for maps
		template <typename T>
		const Node& GetValue(const T& key) const;
89
90
91
92

		template <typename T>
		const Node *FindValueForKey(const T& key) const;

93
		// helpers for parsing
94
95
96
97
98
99
		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:
100
		Mark m_mark;
101
102
103
		std::string m_anchor, m_tag;
		Content *m_pContent;
		bool m_alias;
104
105
		const Node *m_pIdentity;
		mutable bool m_referenced;
106
107
	};
}
108
109

#include "nodeimpl.h"
jbeder's avatar
jbeder committed
110
111

#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66