Coverage Report

Created: 2026-04-11 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/lib/yamlcpp/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
#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
82.0k
    : m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
21
22
inline Node::Node(NodeType::value type)
23
0
    : m_isValid(true),
24
0
      m_invalidKey{},
25
0
      m_pMemory(new detail::memory_holder),
26
0
      m_pNode(&m_pMemory->create_node()) {
27
0
  m_pNode->set_type(type);
28
0
}
29
30
template <typename T>
31
inline Node::Node(const T& rhs)
32
0
    : m_isValid(true),
33
0
      m_invalidKey{},
34
0
      m_pMemory(new detail::memory_holder),
35
0
      m_pNode(&m_pMemory->create_node()) {
36
0
  Assign(rhs);
37
0
}
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
274k
inline Node::Node(const Node&) = default;
46
47
inline Node::Node(Zombie)
48
75.2k
    : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(nullptr) {}
49
50
inline Node::Node(Zombie, const std::string& key)
51
4.63k
    : 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
66.4k
    : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
55
56
503k
inline Node::~Node() = default;
57
58
8.63k
inline void Node::EnsureNodeExists() const {
59
8.63k
  if (!m_isValid)
60
0
    throw InvalidNode(m_invalidKey);
61
8.63k
  if (!m_pNode) {
62
0
    m_pMemory.reset(new detail::memory_holder);
63
0
    m_pNode = &m_pMemory->create_node();
64
0
    m_pNode->set_null();
65
0
  }
66
8.63k
}
67
68
46.7k
inline bool Node::IsDefined() const {
69
46.7k
  if (!m_isValid) {
70
4.63k
    return false;
71
4.63k
  }
72
42.1k
  return m_pNode ? m_pNode->is_defined() : true;
73
46.7k
}
74
75
495
inline Mark Node::Mark() const {
76
495
  if (!m_isValid) {
77
0
    throw InvalidNode(m_invalidKey);
78
0
  }
79
495
  return m_pNode ? m_pNode->mark() : Mark::null_mark();
80
495
}
81
82
69.8k
inline NodeType::value Node::Type() const {
83
69.8k
  if (!m_isValid)
84
0
    throw InvalidNode(m_invalidKey);
85
69.8k
  return m_pNode ? m_pNode->type() : NodeType::Null;
86
69.8k
}
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;
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)
128
      throw TypedBadConversion<T>(node.Mark());
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
3.15k
  explicit as_if(const Node& node_) : node(node_) {}
140
  const Node& node;
141
142
3.15k
  std::string operator()() const {
143
3.15k
    if (node.Type() == NodeType::Null)
144
558
      return "null";
145
2.60k
    if (node.Type() != NodeType::Scalar)
146
495
      throw TypedBadConversion<std::string>(node.Mark());
147
2.10k
    return node.Scalar();
148
2.60k
  }
149
};
150
151
// access functions
152
template <typename T>
153
3.15k
inline T Node::as() const {
154
3.15k
  if (!m_isValid)
155
0
    throw InvalidNode(m_invalidKey);
156
3.15k
  return as_if<T, void>(*this)();
157
3.15k
}
158
159
template <typename T, typename S>
160
inline T Node::as(const S& fallback) const {
161
  if (!m_isValid)
162
    return fallback;
163
  return as_if<T, S>(*this)(fallback);
164
}
165
166
12.4k
inline const std::string& Node::Scalar() const {
167
12.4k
  if (!m_isValid)
168
0
    throw InvalidNode(m_invalidKey);
169
12.4k
  return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
170
12.4k
}
171
172
0
inline const std::string& Node::Tag() const {
173
0
  if (!m_isValid)
174
0
    throw InvalidNode(m_invalidKey);
175
0
  return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
176
0
}
177
178
0
inline void Node::SetTag(const std::string& tag) {
179
0
  EnsureNodeExists();
180
0
  m_pNode->set_tag(tag);
181
0
}
182
183
0
inline EmitterStyle::value Node::Style() const {
184
0
  if (!m_isValid)
185
0
    throw InvalidNode(m_invalidKey);
186
0
  return m_pNode ? m_pNode->style() : EmitterStyle::Default;
187
0
}
188
189
0
inline void Node::SetStyle(EmitterStyle::value style) {
190
0
  EnsureNodeExists();
191
0
  m_pNode->set_style(style);
192
0
}
193
194
// assignment
195
277
inline bool Node::is(const Node& rhs) const {
196
277
  if (!m_isValid || !rhs.m_isValid)
197
0
    throw InvalidNode(m_invalidKey);
198
277
  if (!m_pNode || !rhs.m_pNode)
199
277
    return false;
200
0
  return m_pNode->is(*rhs.m_pNode);
201
277
}
202
203
template <typename T>
204
0
inline Node& Node::operator=(const T& rhs) {
205
0
  Assign(rhs);
206
0
  return *this;
207
0
}
Unexecuted instantiation: YAML::Node& YAML::Node::operator=<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: YAML::Node& YAML::Node::operator=<int>(int const&)
208
209
277
inline Node& Node::operator=(const Node& rhs) {
210
277
  if (is(rhs))
211
0
    return *this;
212
277
  AssignNode(rhs);
213
277
  return *this;
214
277
}
215
216
0
inline void Node::reset(const YAML::Node& rhs) {
217
0
  if (!m_isValid || !rhs.m_isValid)
218
0
    throw InvalidNode(m_invalidKey);
219
0
  m_pMemory = rhs.m_pMemory;
220
0
  m_pNode = rhs.m_pNode;
221
0
}
222
223
template <typename T>
224
0
inline void Node::Assign(const T& rhs) {
225
0
  if (!m_isValid)
226
0
    throw InvalidNode(m_invalidKey);
227
0
  AssignData(convert<T>::encode(rhs));
228
0
}
229
230
template <>
231
0
inline void Node::Assign(const std::string& rhs) {
232
0
  EnsureNodeExists();
233
0
  m_pNode->set_scalar(rhs);
234
0
}
235
236
0
inline void Node::Assign(const char* rhs) {
237
0
  EnsureNodeExists();
238
0
  m_pNode->set_scalar(rhs);
239
0
}
240
241
0
inline void Node::Assign(char* rhs) {
242
0
  EnsureNodeExists();
243
0
  m_pNode->set_scalar(rhs);
244
0
}
245
246
0
inline void Node::AssignData(const Node& rhs) {
247
0
  EnsureNodeExists();
248
0
  rhs.EnsureNodeExists();
249
0
250
0
  m_pNode->set_data(*rhs.m_pNode);
251
0
  m_pMemory->merge(*rhs.m_pMemory);
252
0
}
253
254
277
inline void Node::AssignNode(const Node& rhs) {
255
277
  if (!m_isValid)
256
0
    throw InvalidNode(m_invalidKey);
257
277
  rhs.EnsureNodeExists();
258
259
277
  if (!m_pNode) {
260
277
    m_pNode = rhs.m_pNode;
261
277
    m_pMemory = rhs.m_pMemory;
262
277
    return;
263
277
  }
264
265
0
  m_pNode->set_ref(*rhs.m_pNode);
266
0
  m_pMemory->merge(*rhs.m_pMemory);
267
0
  m_pNode = rhs.m_pNode;
268
0
}
269
270
// size/iterator
271
5.20k
inline std::size_t Node::size() const {
272
5.20k
  if (!m_isValid)
273
0
    throw InvalidNode(m_invalidKey);
274
5.20k
  return m_pNode ? m_pNode->size() : 0;
275
5.20k
}
276
277
0
inline const_iterator Node::begin() const {
278
0
  if (!m_isValid)
279
0
    return const_iterator();
280
0
  return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
281
0
                 : const_iterator();
282
0
}
283
284
550
inline iterator Node::begin() {
285
550
  if (!m_isValid)
286
0
    return iterator();
287
550
  return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
288
550
}
289
290
0
inline const_iterator Node::end() const {
291
0
  if (!m_isValid)
292
0
    return const_iterator();
293
0
  return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
294
0
}
295
296
550
inline iterator Node::end() {
297
550
  if (!m_isValid)
298
0
    return iterator();
299
550
  return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
300
550
}
301
302
// sequence
303
template <typename T>
304
0
inline void Node::push_back(const T& rhs) {
305
0
  if (!m_isValid)
306
0
    throw InvalidNode(m_invalidKey);
307
0
  push_back(Node(rhs));
308
0
}
309
310
0
inline void Node::push_back(const Node& rhs) {
311
0
  EnsureNodeExists();
312
0
  rhs.EnsureNodeExists();
313
0
314
0
  m_pNode->push_back(*rhs.m_pNode, m_pMemory);
315
0
  m_pMemory->merge(*rhs.m_pMemory);
316
0
}
317
318
template<typename Key>
319
4.63k
std::string key_to_string(const Key& key) {
320
4.63k
  return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
321
4.63k
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > YAML::key_to_string<YAML::Node>(YAML::Node const&)
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > YAML::key_to_string<char [3]>(char const (&) [3])
Line
Count
Source
319
2.21k
std::string key_to_string(const Key& key) {
320
2.21k
  return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
321
2.21k
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > YAML::key_to_string<char [8]>(char const (&) [8])
Line
Count
Source
319
2.01k
std::string key_to_string(const Key& key) {
320
2.01k
  return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
321
2.01k
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > YAML::key_to_string<char [7]>(char const (&) [7])
Line
Count
Source
319
406
std::string key_to_string(const Key& key) {
320
406
  return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
321
406
}
322
323
// indexing
324
template <typename Key>
325
8.36k
inline const Node Node::operator[](const Key& key) const {
326
8.36k
  EnsureNodeExists();
327
8.36k
  detail::node* value =
328
8.36k
      static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
329
8.36k
  if (!value) {
330
4.63k
    return Node(ZombieNode, key_to_string(key));
331
4.63k
  }
332
3.72k
  return Node(*value, m_pMemory);
333
8.36k
}
YAML::Node const YAML::Node::operator[]<char [3]>(char const (&) [3]) const
Line
Count
Source
325
3.88k
inline const Node Node::operator[](const Key& key) const {
326
3.88k
  EnsureNodeExists();
327
3.88k
  detail::node* value =
328
3.88k
      static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
329
3.88k
  if (!value) {
330
2.21k
    return Node(ZombieNode, key_to_string(key));
331
2.21k
  }
332
1.66k
  return Node(*value, m_pMemory);
333
3.88k
}
YAML::Node const YAML::Node::operator[]<char [8]>(char const (&) [8]) const
Line
Count
Source
325
3.05k
inline const Node Node::operator[](const Key& key) const {
326
3.05k
  EnsureNodeExists();
327
3.05k
  detail::node* value =
328
3.05k
      static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
329
3.05k
  if (!value) {
330
2.01k
    return Node(ZombieNode, key_to_string(key));
331
2.01k
  }
332
1.04k
  return Node(*value, m_pMemory);
333
3.05k
}
YAML::Node const YAML::Node::operator[]<char [7]>(char const (&) [7]) const
Line
Count
Source
325
1.42k
inline const Node Node::operator[](const Key& key) const {
326
1.42k
  EnsureNodeExists();
327
1.42k
  detail::node* value =
328
1.42k
      static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
329
1.42k
  if (!value) {
330
406
    return Node(ZombieNode, key_to_string(key));
331
406
  }
332
1.01k
  return Node(*value, m_pMemory);
333
1.42k
}
334
335
template <typename Key>
336
0
inline Node Node::operator[](const Key& key) {
337
0
  EnsureNodeExists();
338
0
  detail::node& value = m_pNode->get(key, m_pMemory);
339
0
  return Node(value, m_pMemory);
340
0
}
341
342
template <typename Key>
343
inline bool Node::remove(const Key& key) {
344
  EnsureNodeExists();
345
  return m_pNode->remove(key, m_pMemory);
346
}
347
348
0
inline const Node Node::operator[](const Node& key) const {
349
0
  EnsureNodeExists();
350
0
  key.EnsureNodeExists();
351
0
  m_pMemory->merge(*key.m_pMemory);
352
0
  detail::node* value =
353
0
      static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
354
0
  if (!value) {
355
0
    return Node(ZombieNode, key_to_string(key));
356
0
  }
357
0
  return Node(*value, m_pMemory);
358
0
}
359
360
0
inline Node Node::operator[](const Node& key) {
361
0
  EnsureNodeExists();
362
0
  key.EnsureNodeExists();
363
0
  m_pMemory->merge(*key.m_pMemory);
364
0
  detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
365
0
  return Node(value, m_pMemory);
366
0
}
367
368
0
inline bool Node::remove(const Node& key) {
369
0
  EnsureNodeExists();
370
0
  key.EnsureNodeExists();
371
0
  return m_pNode->remove(*key.m_pNode, m_pMemory);
372
0
}
373
374
// map
375
template <typename Key, typename Value>
376
inline void Node::force_insert(const Key& key, const Value& value) {
377
  EnsureNodeExists();
378
  m_pNode->force_insert(key, value, m_pMemory);
379
}
380
381
// free functions
382
0
inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
383
}  // namespace YAML
384
385
#endif  // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66