nodeimpl.h 2.54 KB
Newer Older
1
2
#pragma once

3
4
5
6
#ifndef NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66


7
8
#include "nodeutil.h"

9
10
11
namespace YAML
{
	// implementation of templated things
12
13
14
15
16
17
18
	template <typename T>
	inline const T Node::Read() const {
		T value;
		*this >> value;
		return value;
	}
	
19
20
21
22
23
	template <typename T>
	Node::operator T() const {
		return Read<T>();
	}

24
25
	template <typename T>
	inline void operator >> (const Node& node, T& value) {
26
		if(!ConvertScalar(node, value))
27
			throw InvalidScalar(node.m_mark);
28
29
30
31
	}
	
	template <typename T>
	inline const Node *Node::FindValue(const T& key) const {
32
33
34
35
36
37
38
39
40
41
42
43
		switch(GetType()) {
			case CT_MAP:
				return FindValueForKey(key);
			case CT_SEQUENCE:
				return FindFromNodeAtIndex(*this, key);
			default:
				return 0;
		}
	}
	
	template <typename T>
	inline const Node *Node::FindValueForKey(const T& key) const {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		for(Iterator it=begin();it!=end();++it) {
			T t;
			if(it.first().Read(t)) {
				if(key == t)
					return &it.second();
			}
		}
		
		return 0;
	}
	
	template <typename T>
	inline const Node& Node::GetValue(const T& key) const {
		if(!m_pContent)
			throw BadDereference();
		
60
61
62
63
64
		const Node *pValue = FindValue(key);
		if(!pValue)
			throw MakeTypedKeyNotFound(m_mark, key);

		return *pValue;
65
66
67
68
69
70
71
	}
	
	template <typename T>
	inline const Node& Node::operator [] (const T& key) const {
		return GetValue(key);
	}
	
72
73
74
75
	inline const Node *Node::FindValue(const char *key) const {
		return FindValue(std::string(key));
	}
	
76
77
78
	inline const Node& Node::operator [] (const char *key) const {
		return GetValue(std::string(key));
	}
79
80
81

	template <typename T>
	inline bool operator == (const T& value, const Node& node) {
82
		return value == node.operator T();
83
84
85
86
	}
	
	template <typename T>
	inline bool operator == (const Node& node, const T& value) {
87
		return value == node.operator T();
88
89
90
91
	}
	
	template <typename T>
	inline bool operator != (const T& value, const Node& node) {
92
		return value != node.operator T();
93
94
95
96
	}
	
	template <typename T>
	inline bool operator != (const Node& node, const T& value) {
97
		return value != node.operator T();
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	}

	inline bool operator == (const char *value, const Node& node) {
		return std::string(value) == node;
	}
	
	inline bool operator == (const Node& node, const char *value) {
		return std::string(value) == node;
	}
	
	inline bool operator != (const char *value, const Node& node) {
		return std::string(value) != node;
	}
	
	inline bool operator != (const Node& node, const char *value) {
		return std::string(value) != node;
	}
	
116
}
117
118

#endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66