node.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include "yaml-cpp/emitterstyle.h"
  9. #include "yaml-cpp/dll.h"
  10. #include "yaml-cpp/node/type.h"
  11. #include "yaml-cpp/node/ptr.h"
  12. #include "yaml-cpp/node/detail/node_ref.h"
  13. #include <set>
  14. namespace YAML {
  15. namespace detail {
  16. class node {
  17. public:
  18. node() : m_pRef(new node_ref) {}
  19. node(const node&) = delete;
  20. node& operator=(const node&) = delete;
  21. bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; }
  22. const node_ref* ref() const { return m_pRef.get(); }
  23. bool is_defined() const { return m_pRef->is_defined(); }
  24. const Mark& mark() const { return m_pRef->mark(); }
  25. NodeType::value type() const { return m_pRef->type(); }
  26. const std::string& scalar() const { return m_pRef->scalar(); }
  27. const std::string& tag() const { return m_pRef->tag(); }
  28. EmitterStyle::value style() const { return m_pRef->style(); }
  29. template <typename T>
  30. bool equals(const T& rhs, shared_memory_holder pMemory);
  31. bool equals(const char* rhs, shared_memory_holder pMemory);
  32. void mark_defined() {
  33. if (is_defined())
  34. return;
  35. m_pRef->mark_defined();
  36. for (nodes::iterator it = m_dependencies.begin();
  37. it != m_dependencies.end(); ++it)
  38. (*it)->mark_defined();
  39. m_dependencies.clear();
  40. }
  41. void add_dependency(node& rhs) {
  42. if (is_defined())
  43. rhs.mark_defined();
  44. else
  45. m_dependencies.insert(&rhs);
  46. }
  47. void set_ref(const node& rhs) {
  48. if (rhs.is_defined())
  49. mark_defined();
  50. m_pRef = rhs.m_pRef;
  51. }
  52. void set_data(const node& rhs) {
  53. if (rhs.is_defined())
  54. mark_defined();
  55. m_pRef->set_data(*rhs.m_pRef);
  56. }
  57. void set_mark(const Mark& mark) { m_pRef->set_mark(mark); }
  58. void set_type(NodeType::value type) {
  59. if (type != NodeType::Undefined)
  60. mark_defined();
  61. m_pRef->set_type(type);
  62. }
  63. void set_null() {
  64. mark_defined();
  65. m_pRef->set_null();
  66. }
  67. void set_scalar(const std::string& scalar) {
  68. mark_defined();
  69. m_pRef->set_scalar(scalar);
  70. }
  71. void set_tag(const std::string& tag) {
  72. mark_defined();
  73. m_pRef->set_tag(tag);
  74. }
  75. // style
  76. void set_style(EmitterStyle::value style) {
  77. mark_defined();
  78. m_pRef->set_style(style);
  79. }
  80. // size/iterator
  81. std::size_t size() const { return m_pRef->size(); }
  82. const_node_iterator begin() const {
  83. return static_cast<const node_ref&>(*m_pRef).begin();
  84. }
  85. node_iterator begin() { return m_pRef->begin(); }
  86. const_node_iterator end() const {
  87. return static_cast<const node_ref&>(*m_pRef).end();
  88. }
  89. node_iterator end() { return m_pRef->end(); }
  90. // sequence
  91. void push_back(node& input, shared_memory_holder pMemory) {
  92. m_pRef->push_back(input, pMemory);
  93. input.add_dependency(*this);
  94. }
  95. void insert(node& key, node& value, shared_memory_holder pMemory) {
  96. m_pRef->insert(key, value, pMemory);
  97. key.add_dependency(*this);
  98. value.add_dependency(*this);
  99. }
  100. // indexing
  101. template <typename Key>
  102. node* get(const Key& key, shared_memory_holder pMemory) const {
  103. // NOTE: this returns a non-const node so that the top-level Node can wrap
  104. // it, and returns a pointer so that it can be NULL (if there is no such
  105. // key).
  106. return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
  107. }
  108. template <typename Key>
  109. node& get(const Key& key, shared_memory_holder pMemory) {
  110. node& value = m_pRef->get(key, pMemory);
  111. value.add_dependency(*this);
  112. return value;
  113. }
  114. template <typename Key>
  115. bool remove(const Key& key, shared_memory_holder pMemory) {
  116. return m_pRef->remove(key, pMemory);
  117. }
  118. node* get(node& key, shared_memory_holder pMemory) const {
  119. // NOTE: this returns a non-const node so that the top-level Node can wrap
  120. // it, and returns a pointer so that it can be NULL (if there is no such
  121. // key).
  122. return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
  123. }
  124. node& get(node& key, shared_memory_holder pMemory) {
  125. node& value = m_pRef->get(key, pMemory);
  126. key.add_dependency(*this);
  127. value.add_dependency(*this);
  128. return value;
  129. }
  130. bool remove(node& key, shared_memory_holder pMemory) {
  131. return m_pRef->remove(key, pMemory);
  132. }
  133. // map
  134. template <typename Key, typename Value>
  135. void force_insert(const Key& key, const Value& value,
  136. shared_memory_holder pMemory) {
  137. m_pRef->force_insert(key, value, pMemory);
  138. }
  139. private:
  140. shared_node_ref m_pRef;
  141. typedef std::set<node*> nodes;
  142. nodes m_dependencies;
  143. };
  144. }
  145. }
  146. #endif // NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66