Coverage Report

Created: 2026-12-01 06:38

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