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

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
43
44
		// accessors
		Iterator begin() const;
		Iterator end() const;
		unsigned size() const;

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
		template <typename T>
		friend void operator >> (const Node& node, T& value);
54

55
		// just for maps
56
		template <typename T>
57
58
59
		const Node *FindValue(const T& key) const;
		const Node *FindValue(const char *key) const;
		
60
61
62
		template <typename T>
		const Node& operator [] (const T& key) const;
		const Node& operator [] (const char *key) const;
63

64
		// just for sequences
65
66
67
		const Node& operator [] (unsigned u) const;
		const Node& operator [] (int i) const;

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

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

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

Jesse Beder's avatar
Jesse Beder committed
80
	private:
81
82
83
84
85
		// helper for maps
		template <typename T>
		const Node& GetValue(const T& key) const;
		
		// helpers for parsing
86
87
88
89
90
91
		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:
92
		Mark m_mark;
93
94
95
		std::string m_anchor, m_tag;
		Content *m_pContent;
		bool m_alias;
96
97
		const Node *m_pIdentity;
		mutable bool m_referenced;
98
99
	};
}
100
101

#include "nodeimpl.h"
102
103

#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66