My Project
impl.h
1 #ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3 
4 #if defined(_MSC_VER) || \
5  (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
6  (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
7 #pragma once
8 #endif
9 
10 #include "yaml-cpp/exceptions.h"
11 #include "yaml-cpp/node/detail/memory.h"
12 #include "yaml-cpp/node/detail/node.h"
13 #include "yaml-cpp/node/iterator.h"
14 #include "yaml-cpp/node/node.h"
15 #include <sstream>
16 #include <string>
17 
18 namespace YAML {
19 inline Node::Node()
20  : m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
21 
22 inline Node::Node(NodeType::value type)
23  : m_isValid(true),
24  m_invalidKey{},
25  m_pMemory(new detail::memory_holder),
26  m_pNode(&m_pMemory->create_node()) {
27  m_pNode->set_type(type);
28 }
29 
30 template <typename T>
31 inline Node::Node(const T& rhs)
32  : m_isValid(true),
33  m_invalidKey{},
34  m_pMemory(new detail::memory_holder),
35  m_pNode(&m_pMemory->create_node()) {
36  Assign(rhs);
37 }
38 
39 inline Node::Node(const detail::iterator_value& rhs)
40  : m_isValid(rhs.m_isValid),
41  m_invalidKey(rhs.m_invalidKey),
42  m_pMemory(rhs.m_pMemory),
43  m_pNode(rhs.m_pNode) {}
44 
45 inline Node::Node(const Node&) = default;
46 
47 inline Node::Node(Zombie)
48  : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(nullptr) {}
49 
50 inline Node::Node(Zombie, const std::string& key)
51  : m_isValid(false), m_invalidKey(key), m_pMemory{}, m_pNode(nullptr) {}
52 
53 inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
54  : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
55 
56 inline Node::~Node() = default;
57 
58 inline void Node::EnsureNodeExists() const {
59  if (!m_isValid)
60  throw InvalidNode(m_invalidKey);
61  if (!m_pNode) {
62  m_pMemory.reset(new detail::memory_holder);
63  m_pNode = &m_pMemory->create_node();
64  m_pNode->set_null();
65  }
66 }
67 
68 inline bool Node::IsDefined() const {
69  if (!m_isValid) {
70  return false;
71  }
72  return m_pNode ? m_pNode->is_defined() : true;
73 }
74 
75 inline Mark Node::Mark() const {
76  if (!m_isValid) {
77  throw InvalidNode(m_invalidKey);
78  }
79  return m_pNode ? m_pNode->mark() : Mark::null_mark();
80 }
81 
82 inline NodeType::value Node::Type() const {
83  if (!m_isValid)
84  throw InvalidNode(m_invalidKey);
85  return m_pNode ? m_pNode->type() : NodeType::Null;
86 }
87 
88 // access
89 
90 // template helpers
91 template <typename T, typename S>
92 struct as_if {
93  explicit as_if(const Node& node_) : node(node_) {}
94  const Node& node;
95 
96  T operator()(const S& fallback) const {
97  if (!node.m_pNode)
98  return fallback;
99 
100  T t = fallback;
101  if (convert<T>::decode(node, t))
102  return t;
103  return fallback;
104  }
105 };
106 
107 template <typename S>
108 struct as_if<std::string, S> {
109  explicit as_if(const Node& node_) : node(node_) {}
110  const Node& node;
111 
112  std::string operator()(const S& fallback) const {
113  if (node.Type() == NodeType::Null)
114  return "null";
115  if (node.Type() != NodeType::Scalar)
116  return fallback;
117  return node.Scalar();
118  }
119 };
120 
121 template <typename T>
122 struct as_if<T, void> {
123  explicit as_if(const Node& node_) : node(node_) {}
124  const Node& node;
125 
126  T operator()() const {
127  if (!node.m_pNode) // no fallback
128  throw InvalidNode(node.m_invalidKey);
129 
130  T t;
131  if (convert<T>::decode(node, t))
132  return t;
133  throw TypedBadConversion<T>(node.Mark());
134  }
135 };
136 
137 template <>
138 struct as_if<std::string, void> {
139  explicit as_if(const Node& node_) : node(node_) {}
140  const Node& node;
141 
142  std::string operator()() const {
143  if (node.Type() == NodeType::Undefined) // no fallback
144  throw InvalidNode(node.m_invalidKey);
145  if (node.Type() == NodeType::Null)
146  return "null";
147  if (node.Type() != NodeType::Scalar)
148  throw TypedBadConversion<std::string>(node.Mark());
149  return node.Scalar();
150  }
151 };
152 
153 // access functions
154 template <typename T>
155 inline T Node::as() const {
156  if (!m_isValid)
157  throw InvalidNode(m_invalidKey);
158  return as_if<T, void>(*this)();
159 }
160 
161 template <typename T, typename S>
162 inline T Node::as(const S& fallback) const {
163  if (!m_isValid)
164  return fallback;
165  return as_if<T, S>(*this)(fallback);
166 }
167 
168 inline const std::string& Node::Scalar() const {
169  if (!m_isValid)
170  throw InvalidNode(m_invalidKey);
171  return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
172 }
173 
174 inline const std::string& Node::Tag() const {
175  if (!m_isValid)
176  throw InvalidNode(m_invalidKey);
177  return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
178 }
179 
180 inline void Node::SetTag(const std::string& tag) {
181  EnsureNodeExists();
182  m_pNode->set_tag(tag);
183 }
184 
185 inline EmitterStyle::value Node::Style() const {
186  if (!m_isValid)
187  throw InvalidNode(m_invalidKey);
188  return m_pNode ? m_pNode->style() : EmitterStyle::Default;
189 }
190 
191 inline void Node::SetStyle(EmitterStyle::value style) {
192  EnsureNodeExists();
193  m_pNode->set_style(style);
194 }
195 
196 // assignment
197 inline bool Node::is(const Node& rhs) const {
198  if (!m_isValid || !rhs.m_isValid)
199  throw InvalidNode(m_invalidKey);
200  if (!m_pNode || !rhs.m_pNode)
201  return false;
202  return m_pNode->is(*rhs.m_pNode);
203 }
204 
205 template <typename T>
206 inline Node& Node::operator=(const T& rhs) {
207  Assign(rhs);
208  return *this;
209 }
210 
211 inline Node& Node::operator=(const Node& rhs) {
212  if (is(rhs))
213  return *this;
214  AssignNode(rhs);
215  return *this;
216 }
217 
218 inline void Node::reset(const YAML::Node& rhs) {
219  if (!m_isValid || !rhs.m_isValid)
220  throw InvalidNode(m_invalidKey);
221  m_pMemory = rhs.m_pMemory;
222  m_pNode = rhs.m_pNode;
223 }
224 
225 template <typename T>
226 inline void Node::Assign(const T& rhs) {
227  if (!m_isValid)
228  throw InvalidNode(m_invalidKey);
229  AssignData(convert<T>::encode(rhs));
230 }
231 
232 template <>
233 inline void Node::Assign(const std::string& rhs) {
234  EnsureNodeExists();
235  m_pNode->set_scalar(rhs);
236 }
237 
238 inline void Node::Assign(const char* rhs) {
239  EnsureNodeExists();
240  m_pNode->set_scalar(rhs);
241 }
242 
243 inline void Node::Assign(char* rhs) {
244  EnsureNodeExists();
245  m_pNode->set_scalar(rhs);
246 }
247 
248 inline void Node::AssignData(const Node& rhs) {
249  EnsureNodeExists();
250  rhs.EnsureNodeExists();
251 
252  m_pNode->set_data(*rhs.m_pNode);
253  m_pMemory->merge(*rhs.m_pMemory);
254 }
255 
256 inline void Node::AssignNode(const Node& rhs) {
257  if (!m_isValid)
258  throw InvalidNode(m_invalidKey);
259  rhs.EnsureNodeExists();
260 
261  if (!m_pNode) {
262  m_pNode = rhs.m_pNode;
263  m_pMemory = rhs.m_pMemory;
264  return;
265  }
266 
267  m_pNode->set_ref(*rhs.m_pNode);
268  m_pMemory->merge(*rhs.m_pMemory);
269  m_pNode = rhs.m_pNode;
270 }
271 
272 // size/iterator
273 inline std::size_t Node::size() const {
274  if (!m_isValid)
275  throw InvalidNode(m_invalidKey);
276  return m_pNode ? m_pNode->size() : 0;
277 }
278 
279 inline const_iterator Node::begin() const {
280  if (!m_isValid)
281  return const_iterator();
282  return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
283  : const_iterator();
284 }
285 
286 inline iterator Node::begin() {
287  if (!m_isValid)
288  return iterator();
289  return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
290 }
291 
292 inline const_iterator Node::end() const {
293  if (!m_isValid)
294  return const_iterator();
295  return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
296 }
297 
298 inline iterator Node::end() {
299  if (!m_isValid)
300  return iterator();
301  return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
302 }
303 
304 // sequence
305 template <typename T>
306 inline void Node::push_back(const T& rhs) {
307  if (!m_isValid)
308  throw InvalidNode(m_invalidKey);
309  push_back(Node(rhs));
310 }
311 
312 inline void Node::push_back(const Node& rhs) {
313  EnsureNodeExists();
314  rhs.EnsureNodeExists();
315 
316  m_pNode->push_back(*rhs.m_pNode, m_pMemory);
317  m_pMemory->merge(*rhs.m_pMemory);
318 }
319 
320 template<typename Key>
321 std::string key_to_string(const Key& key) {
323 }
324 
325 // indexing
326 template <typename Key>
327 inline const Node Node::operator[](const Key& key) const {
328  EnsureNodeExists();
329  detail::node* value =
330  static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
331  if (!value) {
332  return Node(ZombieNode, key_to_string(key));
333  }
334  return Node(*value, m_pMemory);
335 }
336 
337 template <typename Key>
338 inline Node Node::operator[](const Key& key) {
339  EnsureNodeExists();
340  detail::node& value = m_pNode->get(key, m_pMemory);
341  return Node(value, m_pMemory);
342 }
343 
344 template <typename Key>
345 inline bool Node::remove(const Key& key) {
346  EnsureNodeExists();
347  return m_pNode->remove(key, m_pMemory);
348 }
349 
350 inline const Node Node::operator[](const Node& key) const {
351  EnsureNodeExists();
352  key.EnsureNodeExists();
353  m_pMemory->merge(*key.m_pMemory);
354  detail::node* value =
355  static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
356  if (!value) {
357  return Node(ZombieNode, key_to_string(key));
358  }
359  return Node(*value, m_pMemory);
360 }
361 
362 inline Node Node::operator[](const Node& key) {
363  EnsureNodeExists();
364  key.EnsureNodeExists();
365  m_pMemory->merge(*key.m_pMemory);
366  detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
367  return Node(value, m_pMemory);
368 }
369 
370 inline bool Node::remove(const Node& key) {
371  EnsureNodeExists();
372  key.EnsureNodeExists();
373  return m_pNode->remove(*key.m_pNode, m_pMemory);
374 }
375 
376 // map
377 template <typename Key, typename Value>
378 inline void Node::force_insert(const Key& key, const Value& value) {
379  EnsureNodeExists();
380  m_pNode->force_insert(key, value, m_pMemory);
381 }
382 
383 // free functions
384 inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
385 } // namespace YAML
386 
387 #endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: impl.h:122
Definition: iterator.h:23
Definition: exceptions.h:249
Definition: convert.h:38
Definition: node.h:20
Definition: impl.h:92
Definition: exceptions.h:231
Definition: anchor.h:12
Definition: node.h:29
Definition: traits.h:121