File indexing completed on 2024-11-16 09:59:57
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& rhs) = 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;
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 TypedBadConversion<T>(node.Mark());
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::Null)
0144 return "null";
0145 if (node.Type() != NodeType::Scalar)
0146 throw TypedBadConversion<std::string>(node.Mark());
0147 return node.Scalar();
0148 }
0149 };
0150
0151
0152 template <typename T>
0153 inline T Node::as() const {
0154 if (!m_isValid)
0155 throw InvalidNode(m_invalidKey);
0156 return as_if<T, void>(*this)();
0157 }
0158
0159 template <typename T, typename S>
0160 inline T Node::as(const S& fallback) const {
0161 if (!m_isValid)
0162 return fallback;
0163 return as_if<T, S>(*this)(fallback);
0164 }
0165
0166 inline const std::string& Node::Scalar() const {
0167 if (!m_isValid)
0168 throw InvalidNode(m_invalidKey);
0169 return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
0170 }
0171
0172 inline const std::string& Node::Tag() const {
0173 if (!m_isValid)
0174 throw InvalidNode(m_invalidKey);
0175 return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
0176 }
0177
0178 inline void Node::SetTag(const std::string& tag) {
0179 EnsureNodeExists();
0180 m_pNode->set_tag(tag);
0181 }
0182
0183 inline EmitterStyle::value Node::Style() const {
0184 if (!m_isValid)
0185 throw InvalidNode(m_invalidKey);
0186 return m_pNode ? m_pNode->style() : EmitterStyle::Default;
0187 }
0188
0189 inline void Node::SetStyle(EmitterStyle::value style) {
0190 EnsureNodeExists();
0191 m_pNode->set_style(style);
0192 }
0193
0194
0195 inline bool Node::is(const Node& rhs) const {
0196 if (!m_isValid || !rhs.m_isValid)
0197 throw InvalidNode(m_invalidKey);
0198 if (!m_pNode || !rhs.m_pNode)
0199 return false;
0200 return m_pNode->is(*rhs.m_pNode);
0201 }
0202
0203 template <typename T>
0204 inline Node& Node::operator=(const T& rhs) {
0205 Assign(rhs);
0206 return *this;
0207 }
0208
0209 inline Node& Node::operator=(const Node& rhs) {
0210 if (is(rhs))
0211 return *this;
0212 AssignNode(rhs);
0213 return *this;
0214 }
0215
0216 inline void Node::reset(const YAML::Node& rhs) {
0217 if (!m_isValid || !rhs.m_isValid)
0218 throw InvalidNode(m_invalidKey);
0219 m_pMemory = rhs.m_pMemory;
0220 m_pNode = rhs.m_pNode;
0221 }
0222
0223 template <typename T>
0224 inline void Node::Assign(const T& rhs) {
0225 if (!m_isValid)
0226 throw InvalidNode(m_invalidKey);
0227 AssignData(convert<T>::encode(rhs));
0228 }
0229
0230 template <>
0231 inline void Node::Assign(const std::string& rhs) {
0232 EnsureNodeExists();
0233 m_pNode->set_scalar(rhs);
0234 }
0235
0236 inline void Node::Assign(const char* rhs) {
0237 EnsureNodeExists();
0238 m_pNode->set_scalar(rhs);
0239 }
0240
0241 inline void Node::Assign(char* rhs) {
0242 EnsureNodeExists();
0243 m_pNode->set_scalar(rhs);
0244 }
0245
0246 inline void Node::AssignData(const Node& rhs) {
0247 EnsureNodeExists();
0248 rhs.EnsureNodeExists();
0249
0250 m_pNode->set_data(*rhs.m_pNode);
0251 m_pMemory->merge(*rhs.m_pMemory);
0252 }
0253
0254 inline void Node::AssignNode(const Node& rhs) {
0255 if (!m_isValid)
0256 throw InvalidNode(m_invalidKey);
0257 rhs.EnsureNodeExists();
0258
0259 if (!m_pNode) {
0260 m_pNode = rhs.m_pNode;
0261 m_pMemory = rhs.m_pMemory;
0262 return;
0263 }
0264
0265 m_pNode->set_ref(*rhs.m_pNode);
0266 m_pMemory->merge(*rhs.m_pMemory);
0267 m_pNode = rhs.m_pNode;
0268 }
0269
0270
0271 inline std::size_t Node::size() const {
0272 if (!m_isValid)
0273 throw InvalidNode(m_invalidKey);
0274 return m_pNode ? m_pNode->size() : 0;
0275 }
0276
0277 inline const_iterator Node::begin() const {
0278 if (!m_isValid)
0279 return const_iterator();
0280 return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
0281 : const_iterator();
0282 }
0283
0284 inline iterator Node::begin() {
0285 if (!m_isValid)
0286 return iterator();
0287 return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
0288 }
0289
0290 inline const_iterator Node::end() const {
0291 if (!m_isValid)
0292 return const_iterator();
0293 return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
0294 }
0295
0296 inline iterator Node::end() {
0297 if (!m_isValid)
0298 return iterator();
0299 return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
0300 }
0301
0302
0303 template <typename T>
0304 inline void Node::push_back(const T& rhs) {
0305 if (!m_isValid)
0306 throw InvalidNode(m_invalidKey);
0307 push_back(Node(rhs));
0308 }
0309
0310 inline void Node::push_back(const Node& rhs) {
0311 EnsureNodeExists();
0312 rhs.EnsureNodeExists();
0313
0314 m_pNode->push_back(*rhs.m_pNode, m_pMemory);
0315 m_pMemory->merge(*rhs.m_pMemory);
0316 }
0317
0318 template<typename Key>
0319 std::string key_to_string(const Key& key) {
0320 return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
0321 }
0322
0323
0324 template <typename Key>
0325 inline const Node Node::operator[](const Key& key) const {
0326 EnsureNodeExists();
0327 detail::node* value =
0328 static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
0329 if (!value) {
0330 return Node(ZombieNode, key_to_string(key));
0331 }
0332 return Node(*value, m_pMemory);
0333 }
0334
0335 template <typename Key>
0336 inline Node Node::operator[](const Key& key) {
0337 EnsureNodeExists();
0338 detail::node& value = m_pNode->get(key, m_pMemory);
0339 return Node(value, m_pMemory);
0340 }
0341
0342 template <typename Key>
0343 inline bool Node::remove(const Key& key) {
0344 EnsureNodeExists();
0345 return m_pNode->remove(key, m_pMemory);
0346 }
0347
0348 inline const Node Node::operator[](const Node& key) const {
0349 EnsureNodeExists();
0350 key.EnsureNodeExists();
0351 m_pMemory->merge(*key.m_pMemory);
0352 detail::node* value =
0353 static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
0354 if (!value) {
0355 return Node(ZombieNode, key_to_string(key));
0356 }
0357 return Node(*value, m_pMemory);
0358 }
0359
0360 inline Node Node::operator[](const Node& key) {
0361 EnsureNodeExists();
0362 key.EnsureNodeExists();
0363 m_pMemory->merge(*key.m_pMemory);
0364 detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory);
0365 return Node(value, m_pMemory);
0366 }
0367
0368 inline bool Node::remove(const Node& key) {
0369 EnsureNodeExists();
0370 key.EnsureNodeExists();
0371 return m_pNode->remove(*key.m_pNode, m_pMemory);
0372 }
0373
0374
0375 template <typename Key, typename Value>
0376 inline void Node::force_insert(const Key& key, const Value& value) {
0377 EnsureNodeExists();
0378 m_pNode->force_insert(key, value, m_pMemory);
0379 }
0380
0381
0382 inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
0383 }
0384
0385 #endif