summaryrefslogtreecommitdiffstats
path: root/yaml-cpp/src/node.cpp
blob: 360b4ad92a74b1d03df5ee365661b484ba8e0acb (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "yaml-cpp/node.h"
#include "yaml-cpp/aliasmanager.h"
#include "yaml-cpp/emitfromevents.h"
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/eventhandler.h"
#include "iterpriv.h"
#include "nodebuilder.h"
#include "nodeownership.h"
#include "scanner.h"
#include "tag.h"
#include "token.h"
#include <cassert>
#include <stdexcept>

namespace YAML
{
	bool ltnode::operator()(const Node *pNode1, const Node *pNode2) const {
		return *pNode1 < *pNode2;
	}

	Node::Node(): m_pOwnership(new NodeOwnership), m_type(NodeType::Null)
	{
	}

	Node::Node(NodeOwnership& owner): m_pOwnership(new NodeOwnership(&owner)), m_type(NodeType::Null)
	{
	}

	Node::~Node()
	{
		Clear();
	}

	void Node::Clear()
	{
		m_pOwnership.reset(new NodeOwnership);
		m_type = NodeType::Null;
		m_tag.clear();
		m_scalarData.clear();
		m_seqData.clear();
		m_mapData.clear();
	}
	
	bool Node::IsAliased() const
	{
		return m_pOwnership->IsAliased(*this);
	}

	Node& Node::CreateNode()
	{
		return m_pOwnership->Create();
	}

	std::auto_ptr<Node> Node::Clone() const
	{
		std::auto_ptr<Node> pNode(new Node);
		NodeBuilder nodeBuilder(*pNode);
		EmitEvents(nodeBuilder);
		return pNode;
	}

	void Node::EmitEvents(EventHandler& eventHandler) const
	{
		eventHandler.OnDocumentStart(m_mark);
		AliasManager am;
		EmitEvents(am, eventHandler);
		eventHandler.OnDocumentEnd();
	}

	void Node::EmitEvents(AliasManager& am, EventHandler& eventHandler) const
	{
		anchor_t anchor = NullAnchor;
		if(IsAliased()) {
			anchor = am.LookupAnchor(*this);
			if(anchor) {
				eventHandler.OnAlias(m_mark, anchor);
				return;
			}
			
			am.RegisterReference(*this);
			anchor = am.LookupAnchor(*this);
		}
		
		switch(m_type) {
			case NodeType::Null:
				eventHandler.OnNull(m_mark, anchor);
				break;
			case NodeType::Scalar:
				eventHandler.OnScalar(m_mark, m_tag, anchor, m_scalarData);
				break;
			case NodeType::Sequence:
				eventHandler.OnSequenceStart(m_mark, m_tag, anchor);
				for(std::size_t i=0;i<m_seqData.size();i++)
					m_seqData[i]->EmitEvents(am, eventHandler);
				eventHandler.OnSequenceEnd();
				break;
			case NodeType::Map:
				eventHandler.OnMapStart(m_mark, m_tag, anchor);
				for(node_map::const_iterator it=m_mapData.begin();it!=m_mapData.end();++it) {
					it->first->EmitEvents(am, eventHandler);
					it->second->EmitEvents(am, eventHandler);
				}
				eventHandler.OnMapEnd();
				break;
		}
	}

	void Node::Init(NodeType::value type, const Mark& mark, const std::string& tag)
	{
		Clear();
		m_mark = mark;
		m_type = type;
		m_tag = tag;
	}

	void Node::MarkAsAliased()
	{
		m_pOwnership->MarkAsAliased(*this);
	}
	
	void Node::SetScalarData(const std::string& data)
	{
		assert(m_type == NodeType::Scalar); // TODO: throw?
		m_scalarData = data;
	}

	void Node::Append(Node& node)
	{
		assert(m_type == NodeType::Sequence); // TODO: throw?
		m_seqData.push_back(&node);
	}
	
	void Node::Insert(Node& key, Node& value)
	{
		assert(m_type == NodeType::Map); // TODO: throw?
		m_mapData[&key] = &value;
	}

	// begin
	// Returns an iterator to the beginning of this (sequence or map).
	Iterator Node::begin() const
	{
		switch(m_type) {
			case NodeType::Null:
			case NodeType::Scalar:
				return Iterator();
			case NodeType::Sequence:
				return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_seqData.begin())));
			case NodeType::Map:
				return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_mapData.begin())));
		}
		
		assert(false);
		return Iterator();
	}

	// end
	// . Returns an iterator to the end of this (sequence or map).
	Iterator Node::end() const
	{
		switch(m_type) {
			case NodeType::Null:
			case NodeType::Scalar:
				return Iterator();
			case NodeType::Sequence:
				return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_seqData.end())));
			case NodeType::Map:
				return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_mapData.end())));
		}
		
		assert(false);
		return Iterator();
	}

	// size
	// . Returns the size of a sequence or map node
	// . Otherwise, returns zero.
	std::size_t Node::size() const
	{
		switch(m_type) {
			case NodeType::Null:
			case NodeType::Scalar:
				return 0;
			case NodeType::Sequence:
				return m_seqData.size();
			case NodeType::Map:
				return m_mapData.size();
		}
		
		assert(false);
		return 0;
	}

	const Node *Node::FindAtIndex(std::size_t i) const
	{
		if(m_type == NodeType::Sequence)
			return m_seqData[i];
		return 0;
	}

	bool Node::GetScalar(std::string& s) const
	{
		switch(m_type) {
			case NodeType::Null:
				s = "~";
				return true;
			case NodeType::Scalar:
				s = m_scalarData;
				return true;
			case NodeType::Sequence:
			case NodeType::Map:
				return false;
		}
		
		assert(false);
		return false;
	}

	Emitter& operator << (Emitter& out, const Node& node)
	{
		EmitFromEvents emitFromEvents(out);
		node.EmitEvents(emitFromEvents);
		return out;
	}

	int Node::Compare(const Node& rhs) const
	{
		if(m_type != rhs.m_type)
			return rhs.m_type - m_type;
		
		switch(m_type) {
			case NodeType::Null:
				return 0;
			case NodeType::Scalar:
				return m_scalarData.compare(rhs.m_scalarData);
			case NodeType::Sequence:
				if(m_seqData.size() < rhs.m_seqData.size())
					return 1;
				else if(m_seqData.size() > rhs.m_seqData.size())
					return -1;
				for(std::size_t i=0;i<m_seqData.size();i++)
					if(int cmp = m_seqData[i]->Compare(*rhs.m_seqData[i]))
						return cmp;
				return 0;
			case NodeType::Map:
				if(m_mapData.size() < rhs.m_mapData.size())
					return 1;
				else if(m_mapData.size() > rhs.m_mapData.size())
					return -1;
				node_map::const_iterator it = m_mapData.begin();
				node_map::const_iterator jt = rhs.m_mapData.begin();
				for(;it!=m_mapData.end() && jt!=rhs.m_mapData.end();it++, jt++) {
					if(int cmp = it->first->Compare(*jt->first))
						return cmp;
					if(int cmp = it->second->Compare(*jt->second))
						return cmp;
				}
				return 0;
		}
		
		assert(false);
		return 0;
	}

	bool operator < (const Node& n1, const Node& n2)
	{
		return n1.Compare(n2) < 0;
	}
}