node.h 2.74 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>
Jesse Beder's avatar
Jesse Beder committed
17
#include <memory>
18
19
20
21
22

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

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

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

		void Clear();
Jesse Beder's avatar
Jesse Beder committed
34
		std::auto_ptr<Node> Clone() const;
35
36
37
38
		void Parse(Scanner *pScanner, const ParserState& state);

		CONTENT_TYPE GetType() const;

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

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

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

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

54
55
56
		template <typename T>
		const T Read() const;

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

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

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

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

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

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

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

		template <typename T>
		const Node *FindValueForKey(const T& key) const;
Jesse Beder's avatar
Jesse Beder committed
94
95
96
		
		// helper for cloning
		Node(const Mark& mark, const std::string& anchor, const std::string& tag, const Content *pContent);
97

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

#include "nodeimpl.h"
115
116

#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66