node_data.cpp 2.5 KB
Newer Older
Jesse Beder's avatar
Jesse Beder committed
1
#include "yaml-cpp/value/detail/node_data.h"
2
3
#include "yaml-cpp/value/detail/node.h"
#include <sstream>
Jesse Beder's avatar
Jesse Beder committed
4
5
6
7
8

namespace YAML
{
	namespace detail
	{
9
		node_data::node_data(): m_isDefined(false), m_type(ValueType::Null)
Jesse Beder's avatar
Jesse Beder committed
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

		void node_data::set_type(ValueType::value type)
		{
			if(type == ValueType::Undefined) {
				m_type = type;
				m_isDefined = false;
				return;
			}
			

			m_isDefined = true;
			if(type == m_type)
				return;
			
			m_type = type;
			
			switch(m_type) {
				case ValueType::Null:
					break;
				case ValueType::Scalar:
					m_scalar.clear();
					break;
				case ValueType::Sequence:
					m_sequence.clear();
					break;
				case ValueType::Map:
					m_map.clear();
					break;
				case ValueType::Undefined:
					assert(false);
					break;
			}
		}

		void node_data::set_null()
		{
			m_isDefined = true;
			m_type = ValueType::Null;
		}
		
		void node_data::set_scalar(const std::string& scalar)
		{
			m_isDefined = true;
			m_type = ValueType::Scalar;
			m_scalar = scalar;
		}

		// indexing
60
		shared_node node_data::get(shared_node pKey) const
61
62
63
64
65
66
67
68
69
70
71
72
		{
			if(m_type != ValueType::Map)
				return shared_node(new node);
			
			for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
				if(it->first == pKey)
					return it->second;
			}
			
			return shared_node(new node);
		}
		
73
		shared_node node_data::get(shared_node pKey)
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
		{
			switch(m_type) {
				case ValueType::Undefined:
				case ValueType::Null:
				case ValueType::Scalar:
					m_type = ValueType::Map;
					m_map.clear();
					break;
				case ValueType::Sequence:
					convert_sequence_to_map();
					break;
				case ValueType::Map:
					break;
			}

			for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
				if(it->first == pKey)
					return it->second;
			}
			
			shared_node pValue(new node);
			m_map.push_back(kv_pair(pKey, pValue));
			return pValue;
		}
		
		bool node_data::remove(shared_node pKey)
		{
			if(m_type != ValueType::Map)
				return false;

			for(node_map::iterator it=m_map.begin();it!=m_map.end();++it) {
				if(it->first == pKey) {
					m_map.erase(it);
					return true;
				}
			}
			
			return false;
		}

		void node_data::convert_sequence_to_map()
		{
			assert(m_type == ValueType::Sequence);
			
			m_map.clear();
			for(std::size_t i=0;i<m_sequence.size();i++) {
				std::stringstream stream;
				stream << i;

				shared_node pKey(new node);
				pKey->set_scalar(stream.str());
				m_map.push_back(kv_pair(pKey, m_sequence[i]));
			}
			
			m_sequence.clear();
			m_type = ValueType::Map;
		}
Jesse Beder's avatar
Jesse Beder committed
131
132
	}
}