node.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef NODE_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define NODE_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 <stdexcept>
  9. #include "yaml-cpp/dll.h"
  10. #include "yaml-cpp/emitterstyle.h"
  11. #include "yaml-cpp/mark.h"
  12. #include "yaml-cpp/node/detail/bool_type.h"
  13. #include "yaml-cpp/node/detail/iterator_fwd.h"
  14. #include "yaml-cpp/node/ptr.h"
  15. #include "yaml-cpp/node/type.h"
  16. namespace YAML {
  17. namespace detail {
  18. class node;
  19. class node_data;
  20. struct iterator_value;
  21. } // namespace detail
  22. } // namespace YAML
  23. namespace YAML {
  24. class YAML_CPP_API Node {
  25. public:
  26. friend class NodeBuilder;
  27. friend class NodeEvents;
  28. friend struct detail::iterator_value;
  29. friend class detail::node;
  30. friend class detail::node_data;
  31. template <typename>
  32. friend class detail::iterator_base;
  33. template <typename T, typename S>
  34. friend struct as_if;
  35. typedef YAML::iterator iterator;
  36. typedef YAML::const_iterator const_iterator;
  37. Node();
  38. explicit Node(NodeType::value type);
  39. template <typename T>
  40. explicit Node(const T& rhs);
  41. explicit Node(const detail::iterator_value& rhs);
  42. Node(const Node& rhs);
  43. ~Node();
  44. YAML::Mark Mark() const;
  45. NodeType::value Type() const;
  46. bool IsDefined() const;
  47. bool IsNull() const { return Type() == NodeType::Null; }
  48. bool IsScalar() const { return Type() == NodeType::Scalar; }
  49. bool IsSequence() const { return Type() == NodeType::Sequence; }
  50. bool IsMap() const { return Type() == NodeType::Map; }
  51. // bool conversions
  52. YAML_CPP_OPERATOR_BOOL()
  53. bool operator!() const { return !IsDefined(); }
  54. // access
  55. template <typename T>
  56. T as() const;
  57. template <typename T, typename S>
  58. T as(const S& fallback) const;
  59. const std::string& Scalar() const;
  60. const std::string& Tag() const;
  61. void SetTag(const std::string& tag);
  62. // style
  63. // WARNING: This API might change in future releases.
  64. EmitterStyle::value Style() const;
  65. void SetStyle(EmitterStyle::value style);
  66. // assignment
  67. bool is(const Node& rhs) const;
  68. template <typename T>
  69. Node& operator=(const T& rhs);
  70. Node& operator=(const Node& rhs);
  71. void reset(const Node& rhs = Node());
  72. // size/iterator
  73. std::size_t size() const;
  74. const_iterator begin() const;
  75. iterator begin();
  76. const_iterator end() const;
  77. iterator end();
  78. // sequence
  79. template <typename T>
  80. void push_back(const T& rhs);
  81. void push_back(const Node& rhs);
  82. // indexing
  83. template <typename Key>
  84. const Node operator[](const Key& key) const;
  85. template <typename Key>
  86. Node operator[](const Key& key);
  87. template <typename Key>
  88. bool remove(const Key& key);
  89. const Node operator[](const Node& key) const;
  90. Node operator[](const Node& key);
  91. bool remove(const Node& key);
  92. // map
  93. template <typename Key, typename Value>
  94. void force_insert(const Key& key, const Value& value);
  95. private:
  96. enum Zombie { ZombieNode };
  97. explicit Node(Zombie);
  98. explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
  99. void EnsureNodeExists() const;
  100. template <typename T>
  101. void Assign(const T& rhs);
  102. void Assign(const char* rhs);
  103. void Assign(char* rhs);
  104. void AssignData(const Node& rhs);
  105. void AssignNode(const Node& rhs);
  106. private:
  107. bool m_isValid;
  108. mutable detail::shared_memory_holder m_pMemory;
  109. mutable detail::node* m_pNode;
  110. };
  111. YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs);
  112. YAML_CPP_API Node Clone(const Node& node);
  113. template <typename T>
  114. struct convert;
  115. }
  116. #endif // NODE_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66