1 #ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 2 #define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 4 #if defined(_MSC_VER) || \ 5 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ 6 (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 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" 20 : m_isValid(true), m_invalidKey{}, m_pMemory(
nullptr), m_pNode(
nullptr) {}
22 inline Node::Node(NodeType::value type)
25 m_pMemory(
new detail::memory_holder),
26 m_pNode(&m_pMemory->create_node()) {
27 m_pNode->set_type(type);
31 inline Node::Node(
const T& rhs)
34 m_pMemory(
new detail::memory_holder),
35 m_pNode(&m_pMemory->create_node()) {
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) {}
45 inline Node::Node(
const Node&) =
default;
47 inline Node::Node(Zombie)
48 : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(
nullptr) {}
50 inline Node::Node(Zombie,
const std::string& key)
51 : m_isValid(false), m_invalidKey(key), m_pMemory{}, m_pNode(
nullptr) {}
53 inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
54 : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
56 inline Node::~Node() =
default;
58 inline void Node::EnsureNodeExists()
const {
60 throw InvalidNode(m_invalidKey);
62 m_pMemory.reset(
new detail::memory_holder);
63 m_pNode = &m_pMemory->create_node();
68 inline bool Node::IsDefined()
const {
72 return m_pNode ? m_pNode->is_defined() :
true;
75 inline Mark Node::Mark()
const {
77 throw InvalidNode(m_invalidKey);
79 return m_pNode ? m_pNode->mark() : Mark::null_mark();
82 inline NodeType::value Node::Type()
const {
84 throw InvalidNode(m_invalidKey);
85 return m_pNode ? m_pNode->type() : NodeType::Null;
91 template <
typename T,
typename S>
93 explicit as_if(
const Node& node_) : node(node_) {}
96 T operator()(
const S& fallback)
const {
107 template <
typename S>
109 explicit as_if(
const Node& node_) : node(node_) {}
112 std::string operator()(
const S& fallback)
const {
113 if (node.Type() == NodeType::Null)
115 if (node.Type() != NodeType::Scalar)
117 return node.Scalar();
121 template <
typename T>
123 explicit as_if(
const Node& node_) : node(node_) {}
126 T operator()()
const {
139 explicit as_if(
const Node& node_) : node(node_) {}
142 std::string operator()()
const {
143 if (node.Type() == NodeType::Undefined)
145 if (node.Type() == NodeType::Null)
147 if (node.Type() != NodeType::Scalar)
149 return node.Scalar();
154 template <
typename T>
155 inline T Node::as()
const {
161 template <
typename T,
typename S>
162 inline T Node::as(
const S& fallback)
const {
168 inline const std::string& Node::Scalar()
const {
171 return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
174 inline const std::string& Node::Tag()
const {
177 return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
180 inline void Node::SetTag(
const std::string& tag) {
182 m_pNode->set_tag(tag);
185 inline EmitterStyle::value Node::Style()
const {
188 return m_pNode ? m_pNode->style() : EmitterStyle::Default;
191 inline void Node::SetStyle(EmitterStyle::value style) {
193 m_pNode->set_style(style);
197 inline bool Node::is(
const Node& rhs)
const {
198 if (!m_isValid || !rhs.m_isValid)
200 if (!m_pNode || !rhs.m_pNode)
202 return m_pNode->is(*rhs.m_pNode);
205 template <
typename T>
206 inline Node& Node::operator=(
const T& rhs) {
211 inline Node& Node::operator=(
const Node& rhs) {
218 inline void Node::reset(
const YAML::Node& rhs) {
219 if (!m_isValid || !rhs.m_isValid)
221 m_pMemory = rhs.m_pMemory;
222 m_pNode = rhs.m_pNode;
225 template <
typename T>
226 inline void Node::Assign(
const T& rhs) {
233 inline void Node::Assign(
const std::string& rhs) {
235 m_pNode->set_scalar(rhs);
238 inline void Node::Assign(
const char* rhs) {
240 m_pNode->set_scalar(rhs);
243 inline void Node::Assign(
char* rhs) {
245 m_pNode->set_scalar(rhs);
248 inline void Node::AssignData(
const Node& rhs) {
250 rhs.EnsureNodeExists();
252 m_pNode->set_data(*rhs.m_pNode);
253 m_pMemory->merge(*rhs.m_pMemory);
256 inline void Node::AssignNode(
const Node& rhs) {
259 rhs.EnsureNodeExists();
262 m_pNode = rhs.m_pNode;
263 m_pMemory = rhs.m_pMemory;
267 m_pNode->set_ref(*rhs.m_pNode);
268 m_pMemory->merge(*rhs.m_pMemory);
269 m_pNode = rhs.m_pNode;
273 inline std::size_t Node::size()
const {
276 return m_pNode ? m_pNode->size() : 0;
305 template <
typename T>
306 inline void Node::push_back(
const T& rhs) {
309 push_back(
Node(rhs));
312 inline void Node::push_back(
const Node& rhs) {
314 rhs.EnsureNodeExists();
316 m_pNode->push_back(*rhs.m_pNode, m_pMemory);
317 m_pMemory->merge(*rhs.m_pMemory);
320 template<
typename Key>
321 std::string key_to_string(
const Key& key) {
326 template <
typename Key>
327 inline const Node Node::operator[](
const Key& key)
const {
330 static_cast<const detail::node&
>(*m_pNode).get(key, m_pMemory);
332 return Node(ZombieNode, key_to_string(key));
334 return Node(*value, m_pMemory);
337 template <
typename Key>
338 inline Node Node::operator[](
const Key& key) {
341 return Node(value, m_pMemory);
344 template <
typename Key>
345 inline bool Node::remove(
const Key& key) {
347 return m_pNode->remove(key, m_pMemory);
350 inline const Node Node::operator[](
const Node& key)
const {
352 key.EnsureNodeExists();
353 m_pMemory->merge(*key.m_pMemory);
355 static_cast<const detail::node&
>(*m_pNode).get(*key.m_pNode, m_pMemory);
357 return Node(ZombieNode, key_to_string(key));
359 return Node(*value, m_pMemory);
362 inline Node Node::operator[](
const Node& key) {
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);
370 inline bool Node::remove(
const Node& key) {
372 key.EnsureNodeExists();
373 return m_pNode->remove(*key.m_pNode, m_pMemory);
377 template <
typename Key,
typename Value>
378 inline void Node::force_insert(
const Key& key,
const Value& value) {
380 m_pNode->force_insert(key, value, m_pMemory);
384 inline bool operator==(
const Node& lhs,
const Node& rhs) {
return lhs.is(rhs); }
387 #endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: iterator.h:23
Definition: exceptions.h:249
Definition: exceptions.h:231