node.h 3.5 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
12
#include "noncopyable.h"
#include <iostream>
13
14
15
#include <string>
#include <vector>
#include <map>
jbeder's avatar
jbeder committed
16
#include <memory>
17
18
19

namespace YAML
{
20
	class AliasManager;
21
22
	class Content;
	class Scanner;
23
	class Emitter;
24
25
	class EventHandler;
	struct NodeProperties;
26
27
28

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

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

		void Clear();
jbeder's avatar
jbeder committed
36
		std::auto_ptr<Node> Clone() const;
37
38
39
40
41
42
43
44
45
46
		void EmitEvents(EventHandler& eventHandler) const;
		void EmitEvents(AliasManager& am, EventHandler& eventHandler) const;

		void Init(CONTENT_TYPE type, const Mark& mark, const std::string& tag);
		void InitNull(const std::string& tag);
		void InitAlias(const Mark& mark, const Node& identity);
		
		void SetData(const std::string& data);
		void Append(std::auto_ptr<Node> pNode);
		void Insert(std::auto_ptr<Node> pKey, std::auto_ptr<Node> pValue);
47
48
49

		CONTENT_TYPE GetType() const;

50
		// file location of start of this node
51
		const Mark GetMark() const { return m_mark; }
52

53
54
55
		// accessors
		Iterator begin() const;
		Iterator end() const;
56
		std::size_t size() const;
57

58
		// extraction of scalars
59
60
61
		bool GetScalar(std::string& s) const;

		// we can specialize this for other values
62
		template <typename T>
63
		bool Read(T& value) const;
64

65
66
		template <typename T>
		const T Read() const;
67
68
69
		
		template <typename T>
		operator T() const;
70

71
72
		template <typename T>
		friend void operator >> (const Node& node, T& value);
73

74
		// retrieval for maps and sequences
75
		template <typename T>
76
		const Node *FindValue(const T& key) const;
77

78
79
		template <typename T>
		const Node& operator [] (const T& key) const;
80
81
82
		
		// specific to maps
		const Node *FindValue(const char *key) const;
83
		const Node& operator [] (const char *key) const;
84

85
86
87
88
		// for anchors/aliases
		const Node *Identity() const { return m_pIdentity; }
		bool IsAlias() const { return m_alias; }
		bool IsReferenced() const { return m_referenced; }
89
90
		
		// for tags
91
		const std::string GetTag() const { return IsAlias() ? m_pIdentity->GetTag() : m_tag; }
92

93
94
		// emitting
		friend Emitter& operator << (Emitter& out, const Node& node);
95
96
97
98
99

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

jbeder's avatar
jbeder committed
100
	private:
101
102
103
104
		// helper for sequences
		template <typename, bool> friend struct _FindFromNodeAtIndex;
		const Node *FindAtIndex(std::size_t i) const;
		
105
106
107
		// helper for maps
		template <typename T>
		const Node& GetValue(const T& key) const;
108
109
110

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

	private:
113
		Mark m_mark;
114
		std::string m_tag;
115
116
		Content *m_pContent;
		bool m_alias;
117
118
		const Node *m_pIdentity;
		mutable bool m_referenced;
119
	};
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	
	// comparisons with auto-conversion
	template <typename T>
	bool operator == (const T& value, const Node& node);
	
	template <typename T>
	bool operator == (const Node& node, const T& value);
	
	template <typename T>
	bool operator != (const T& value, const Node& node);
	
	template <typename T>
	bool operator != (const Node& node, const T& value);
	
	bool operator == (const char *value, const Node& node);
	bool operator == (const Node& node, const char *value);
	bool operator != (const char *value, const Node& node);
	bool operator != (const Node& node, const char *value);
138
}
139
140

#include "nodeimpl.h"
141
#include "nodereadimpl.h"
jbeder's avatar
jbeder committed
142
143

#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66