File indexing completed on 2026-12-04 11:17:34
0001 #ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0002 #define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0003
0004 #if defined(_MSC_VER) || \
0005 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
0006 (__GNUC__ >= 4))
0007 #pragma once
0008 #endif
0009
0010 #include "yaml-cpp/exceptions.h"
0011 #include "yaml-cpp/node/detail/memory.h"
0012 #include "yaml-cpp/node/detail/node.h"
0013 #include "yaml-cpp/node/iterator.h"
0014 #include "yaml-cpp/node/node.h"
0015 #include <sstream>
0016 #include <string>
0017
0018 namespace YAML {
0019 inline Node::Node()
0020 : m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
0021
0022 inline Node::Node(NodeType::value type)
0023 : m_isValid(true),
0024 m_invalidKey{},
0025 m_pMemory(new detail::memory_holder),
0026 m_pNode(&m_pMemory->create_node()) {
0027 m_pNode->set_type(type);
0028 }
0029
0030 template <typename T>
0031 inline Node::Node(const T& rhs)
0032 : m_isValid(true),
0033 m_invalidKey{},
0034 m_pMemory(new detail::memory_holder),
0035 m_pNode(&m_pMemory->create_node()) {
0036 Assign(rhs);
0037 }
0038
0039 inline Node::Node(const detail::iterator_value& rhs)
0040 : m_isValid(rhs.m_isValid),
0041 m_invalidKey(rhs.m_invalidKey),
0042 m_pMemory(rhs.m_pMemory),
0043 m_pNode(rhs.m_pNode) {}
0044
0045 inline Node::Node(const Node&) = default;
0046
0047 inline Node::Node(Zombie)
0048 : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(nullptr) {}
0049
0050 inline Node::Node(Zombie, const std::string& key)
0051 : m_isValid(false), m_invalidKey(key), m_pMemory{}, m_pNode(nullptr) {}
0052
0053 inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
0054 : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
0055
0056 inline Node::~Node() = default;
0057
0058 inline void Node::EnsureNodeExists() const {
0059 if (!m_isValid)
0060 throw InvalidNode(m_invalidKey);
0061 if (!m_pNode) {
0062 m_pMemory.reset(new detail::memory_holder);
0063 m_pNode = &m_pMemory->create_node();
0064 m_pNode->set_null();
0065 }
0066 }
0067
0068 inline bool Node::IsDefined() const {
0069 if (!m_isValid) {
0070 return false;
0071 }
0072 return m_pNode ? m_pNode->is_defined() : true;
0073 }
0074
0075 inline Mark Node::Mark() const {
0076 if (!m_isValid) {
0077 throw InvalidNode(m_invalidKey);
0078 }
0079 return m_pNode ? m_pNode->mark() : Mark::null_mark();
0080 }
0081
0082 inline NodeType::value Node::Type() const {
0083 if (!m_isValid)
0084 throw InvalidNode(m_invalidKey);
0085 return m_pNode ? m_pNode->type() : NodeType::Null;
0086 }
0087
0088
0089
0090
0091 template <typename T, typename S>
0092 struct as_if {
0093 explicit as_if(const Node& node_) : node(node_) {}
0094 const Node& node;
0095
0096 T operator()(const S& fallback) const {
0097 if (!node.m_pNode)
0098 return fallback;
0099
0100 T t = fallback;
0101 if (convert<T>::decode(node, t))
0102 return t;
0103 return fallback;
0104 }
0105 };
0106
0107 template <typename S>
0108 struct as_if<std::string, S> {
0109 explicit as_if(const Node& node_) : node(node_) {}
0110 const Node& node;
0111
0112 std::string operator()(const S& fallback) const {
0113 if (node.Type() == NodeType::Null)
0114 return "null";
0115 if (node.Type() != NodeType::Scalar)
0116 return fallback;
0117 return node.Scalar();
0118 }
0119 };
0120
0121 template <typename T>
0122 struct as_if<T, void> {
0123 explicit as_if(const Node& node_) : node(node_) {}
0124 const Node& node;
0125
0126 T operator()() const {
0127 if (!node.m_pNode)
0128 throw InvalidNode(node.m_invalidKey);
0129
0130 T t;
0131 if (convert<T>::decode(node, t))
0132 return t;
0133 throw TypedBadConversion<T>(node.Mark());
0134 }
0135 };
0136
0137 template <>
0138 struct as_if<std::string, void> {
0139 explicit as_if(const Node& node_) : node(node_) {}
0140 const Node& node;
0141
0142 std::string operator()() const {
0143 if (node.Type() == NodeType::Undefined)
0144 throw InvalidNode(node.m_invalidKey);
0145 if (node.Type() == NodeType::Null)
0146 return "null";
0147 if (node.Type() != NodeType::Scalar)
0148 throw TypedBadConversion<std::string>(node.Mark());
0149 return node.Scalar();
0150 }
0151 };
0152
0153
0154 template <typename T>
0155 inline T Node::as() const {
0156 if (!m_isValid)
0157 throw InvalidNode(m_invalidKey);
0158 return as_if<T, void>(*this)();
0159 }
0160
0161 template <typename T, typename S>
0162 inline T Node::as(const S& fallback) const {
0163 if (!m_isValid)
0164 return fallback;
0165 return as_if<T, S>(*this)(fallback);
0166 }
0167
0168 inline const std::string& Node::Scalar() const {
0169 if (!m_isValid)
0170 throw InvalidNode(m_invalidKey);
0171 return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
0172 }
0173
0174 inline const std::string& Node::Tag() const {
0175 if (!m_isValid)
0176 throw InvalidNode(m_invalidKey);
0177 return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
0178 }
0179
0180 inline void Node::SetTag(const std::string& tag) {
0181 EnsureNodeExists();
0182 m_pNode->set_tag(tag);
0183 }
0184
0185 inline EmitterStyle::value Node::Style() const {
0186 if (!m_isValid)
0187 throw InvalidNode(m_invalidKey);
0188 return m_pNode ? m_pNode->style() : EmitterStyle::Default;
0189 }
0190
0191 inline void Node::SetStyle(EmitterStyle::value style) {
0192 EnsureNodeExists();
0193 m_pNode->set_style(style);
0194 }
0195
0196
0197 inline bool Node::is(const Node& rhs) const {
0198 if (!m_isValid || !rhs.m_isValid)
0199 throw InvalidNode(m_invalidKey);
0200 if (!m_pNode || !rhs.m_pNode)
0201 return false;
0202 return m_pNode->is(*rhs.m_pNode);
0203 }
0204
0205 template <typename T>
0206 inline Node& Node::operator=(const T& rhs) {
0207 Assign(rhs);
0208 return *this;
0209 }
0210
0211 inline Node& Node::operator=(const Node& rhs) {
0212 if (is(rhs))
0213 return *this;
0214 AssignNode(rhs);
0215 return *this;
0216 }
0217
0218 inline void Node::reset(const YAML::Node& rhs) {
0219 if (!m_isValid || !rhs.m_isValid)
0220 throw InvalidNode(m_invalidKey);
0221 m_pMemory = rhs.m_pMemory;
0222 m_pNode = rhs.m_pNode;
0223 }
0224
0225 template <typename T>
0226 inline void Node::Assign(const T& rhs) {
0227 if (!m_isValid)
0228 throw InvalidNode(m_invalidKey);
0229 AssignData(convert<T>::encode(rhs));
0230 }
0231
0232 template <>
0233 inline void Node::Assign(const std::string& rhs) {
0234 EnsureNodeExists();
0235 m_pNode->set_scalar(rhs);
0236 }
0237
0238 inline void Node::Assign(const char* rhs) {
0239 EnsureNodeExists();
0240 m_pNode->set_scalar(rhs);
0241 }
0242
0243 inline void Node::Assign(char* rhs) {
0244 EnsureNodeExists();
0245 m_pNode->set_scalar(rhs);
0246 }
0247
0248 inline void Node::AssignData(const Node& rhs) {
0249 EnsureNodeExists();
0250 rhs.EnsureNodeExists();
0251
0252 m_pNode->set_data(*rhs.m_pNode);
0253 m_pMemory->merge(*rhs.m_pMemory);
0254 }
0255
0256 inline void Node::AssignNode(const Node& rhs) {
0257 if (!m_isValid)
0258 throw InvalidNode(m_invalidKey);
0259 rhs.EnsureNodeExists();
0260
0261 if (!m_pNode) {
0262 m_pNode = rhs.m_pNode;
0263 m_pMemory = rhs.m_pMemory;
0264 return;
0265 }
0266
0267 m_pNode->set_ref(*rhs.m_pNode);
0268 m_pMemory->merge(*rhs.m_pMemory);
0269 m_pNode = rhs.m_pNode;
0270 }
0271
0272
0273 inline std::size_t Node::size() const {
0274 if (!m_isValid)
0275 throw InvalidNode(m_invalidKey);
0276 return m_pNode ? m_pNode->size() : 0;
0277 }
0278
0279 inline const_iterator Node::begin() const {
0280 if (!m_isValid)
0281 return const_iterator();
0282 return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
0283 : const_iterator();
0284 }
0285
0286 inline iterator Node::begin() {
0287 if (!m_isValid)
0288 return iterator();
0289 return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
0290 }
0291
0292 inline const_iterator Node::end() const {
0293 if (!m_isValid)
0294 return const_iterator();
0295 return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
0296 }
0297
0298 inline iterator Node::end() {
0299 if (!m_isValid)
0300 return iterator();
0301 return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
0302 }
0303
0304
0305 template <typename T>
0306 inline void Node::push_back(const T& rhs) {
0307 if (!m_isValid)
0308 throw InvalidNode(m_invalidKey);
0309 push_back(Node(rhs));
0310 }
0311
0312 inline void Node::push_back(const Node& rhs) {
0313 EnsureNodeExists();
0314 rhs.EnsureNodeExists();
0315
0316 m_pNode->push_back(*rhs.m_pNode, m_pMemory);
0317 m_pMemory->merge(*rhs.m_pMemory);
0318 }
0319
0320 template<typename Key>
0321 std::string key_to_string(const Key& key) {
0322 return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
0323 }
0324
0325
0326 template <typename Key>
0327 inline const Node Node::operator[](const Key& key) const {
0328 EnsureNodeExists();
0329 detail::node* value =
0330 static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
0331 if (!value) {
0332 return Node(ZombieNode, key_to_string(key));
0333 }
0334 return Node(*value, m_pMemory);
0335 }
0336
0337 template <typename Key>
0338 inline Node Node::operator[](const Key& key) {
0339 EnsureNodeExists();
0340 detail::node& value = m_pNode->get(key, m_pMemory);
0341 return Node(value, m_pMemory);
0342 }
0343
0344 template <typename Key>
0345 inline bool Node::remove(const Key& key) {
0346 EnsureNodeExists();
0347 return m_pNode->remove(key, m_pMemory);
0348 }
0349
0350 inline const Node Node::operator[](const Node& key) const {
0351 EnsureNodeExists();
0352 key.EnsureNodeExists();
0353 m_pMemory->merge(*key.m_pMemory);
0354 detail::node* value =
0355 static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
0356 if (!value) {
0357 return Node(ZombieNode, key_to_string(key));
0358 }
0359 return Node(*value, m_pMemory);
0360 }
0361
0362 inline Node Node::operator[](const Node& key) {
0363 EnsureNodeExists();
0364 key.EnsureNodeExists();
0365 m_pMemory->merge(*key.m_pMemory);
0366 detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
0367 return Node(value, m_pMemory);
0368 }
0369
0370 inline bool Node::remove(const Node& key) {
0371 EnsureNodeExists();
0372 key.EnsureNodeExists();
0373 return m_pNode->remove(*key.m_pNode, m_pMemory);
0374 }
0375
0376
0377 template <typename Key, typename Value>
0378 inline void Node::force_insert(const Key& key, const Value& value) {
0379 EnsureNodeExists();
0380 m_pNode->force_insert(key, value, m_pMemory);
0381 }
0382
0383
0384 inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
0385 }
0386
0387 #endif