File indexing completed on 2024-11-16 09:59:56
0001 #ifndef NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0002 #define NODE_DETAIL_NODE_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/dll.h"
0011 #include "yaml-cpp/emitterstyle.h"
0012 #include "yaml-cpp/node/detail/node_ref.h"
0013 #include "yaml-cpp/node/ptr.h"
0014 #include "yaml-cpp/node/type.h"
0015 #include <set>
0016 #include <atomic>
0017
0018 namespace YAML {
0019 namespace detail {
0020 class node {
0021 private:
0022 struct less {
0023 bool operator ()(const node* l, const node* r) const {return l->m_index < r->m_index;}
0024 };
0025
0026 public:
0027 node() : m_pRef(new node_ref), m_dependencies{}, m_index{} {}
0028 node(const node&) = delete;
0029 node& operator=(const node&) = delete;
0030
0031 bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; }
0032 const node_ref* ref() const { return m_pRef.get(); }
0033
0034 bool is_defined() const { return m_pRef->is_defined(); }
0035 const Mark& mark() const { return m_pRef->mark(); }
0036 NodeType::value type() const { return m_pRef->type(); }
0037
0038 const std::string& scalar() const { return m_pRef->scalar(); }
0039 const std::string& tag() const { return m_pRef->tag(); }
0040 EmitterStyle::value style() const { return m_pRef->style(); }
0041
0042 template <typename T>
0043 bool equals(const T& rhs, shared_memory_holder pMemory);
0044 bool equals(const char* rhs, shared_memory_holder pMemory);
0045
0046 void mark_defined() {
0047 if (is_defined())
0048 return;
0049
0050 m_pRef->mark_defined();
0051 for (node* dependency : m_dependencies)
0052 dependency->mark_defined();
0053 m_dependencies.clear();
0054 }
0055
0056 void add_dependency(node& rhs) {
0057 if (is_defined())
0058 rhs.mark_defined();
0059 else
0060 m_dependencies.insert(&rhs);
0061 }
0062
0063 void set_ref(const node& rhs) {
0064 if (rhs.is_defined())
0065 mark_defined();
0066 m_pRef = rhs.m_pRef;
0067 }
0068 void set_data(const node& rhs) {
0069 if (rhs.is_defined())
0070 mark_defined();
0071 m_pRef->set_data(*rhs.m_pRef);
0072 }
0073
0074 void set_mark(const Mark& mark) { m_pRef->set_mark(mark); }
0075
0076 void set_type(NodeType::value type) {
0077 if (type != NodeType::Undefined)
0078 mark_defined();
0079 m_pRef->set_type(type);
0080 }
0081 void set_null() {
0082 mark_defined();
0083 m_pRef->set_null();
0084 }
0085 void set_scalar(const std::string& scalar) {
0086 mark_defined();
0087 m_pRef->set_scalar(scalar);
0088 }
0089 void set_tag(const std::string& tag) {
0090 mark_defined();
0091 m_pRef->set_tag(tag);
0092 }
0093
0094
0095 void set_style(EmitterStyle::value style) {
0096 mark_defined();
0097 m_pRef->set_style(style);
0098 }
0099
0100
0101 std::size_t size() const { return m_pRef->size(); }
0102
0103 const_node_iterator begin() const {
0104 return static_cast<const node_ref&>(*m_pRef).begin();
0105 }
0106 node_iterator begin() { return m_pRef->begin(); }
0107
0108 const_node_iterator end() const {
0109 return static_cast<const node_ref&>(*m_pRef).end();
0110 }
0111 node_iterator end() { return m_pRef->end(); }
0112
0113
0114 void push_back(node& input, shared_memory_holder pMemory) {
0115 m_pRef->push_back(input, pMemory);
0116 input.add_dependency(*this);
0117 m_index = m_amount.fetch_add(1);
0118 }
0119 void insert(node& key, node& value, shared_memory_holder pMemory) {
0120 m_pRef->insert(key, value, pMemory);
0121 key.add_dependency(*this);
0122 value.add_dependency(*this);
0123 }
0124
0125
0126 template <typename Key>
0127 node* get(const Key& key, shared_memory_holder pMemory) const {
0128
0129
0130
0131 return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
0132 }
0133 template <typename Key>
0134 node& get(const Key& key, shared_memory_holder pMemory) {
0135 node& value = m_pRef->get(key, pMemory);
0136 value.add_dependency(*this);
0137 return value;
0138 }
0139 template <typename Key>
0140 bool remove(const Key& key, shared_memory_holder pMemory) {
0141 return m_pRef->remove(key, pMemory);
0142 }
0143
0144 node* get(node& key, shared_memory_holder pMemory) const {
0145
0146
0147
0148 return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
0149 }
0150 node& get(node& key, shared_memory_holder pMemory) {
0151 node& value = m_pRef->get(key, pMemory);
0152 key.add_dependency(*this);
0153 value.add_dependency(*this);
0154 return value;
0155 }
0156 bool remove(node& key, shared_memory_holder pMemory) {
0157 return m_pRef->remove(key, pMemory);
0158 }
0159
0160
0161 template <typename Key, typename Value>
0162 void force_insert(const Key& key, const Value& value,
0163 shared_memory_holder pMemory) {
0164 m_pRef->force_insert(key, value, pMemory);
0165 }
0166
0167 private:
0168 shared_node_ref m_pRef;
0169 using nodes = std::set<node*, less>;
0170 nodes m_dependencies;
0171 size_t m_index;
0172 static std::atomic<size_t> m_amount;
0173 };
0174 }
0175 }
0176
0177 #endif