XMLTree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef _XML_TREE_
  2. #define _XML_TREE_
  3. #include <map>
  4. #include <memory>
  5. #include <vector>
  6. namespace eprosima {
  7. namespace fastrtps {
  8. namespace xmlparser {
  9. enum class NodeType
  10. {
  11. PROFILES,
  12. PARTICIPANT,
  13. PUBLISHER,
  14. SUBSCRIBER,
  15. RTPS,
  16. QOS_PROFILE,
  17. APPLICATION,
  18. TYPE,
  19. TOPIC,
  20. DATA_WRITER,
  21. DATA_READER,
  22. ROOT,
  23. TYPES,
  24. LOG,
  25. REQUESTER,
  26. REPLIER
  27. };
  28. class BaseNode
  29. {
  30. public:
  31. BaseNode(NodeType type) : data_type_(type), parent_(nullptr){};
  32. virtual ~BaseNode() = default;
  33. BaseNode(const BaseNode&) = delete;
  34. BaseNode& operator=(const BaseNode&) = delete;
  35. // C++11 defaulted functions
  36. // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
  37. // supported.
  38. #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
  39. BaseNode(BaseNode&& other)
  40. : data_type_(std::move(other.data_type_)), parent_(std::move(other.parent_)), children(std::move(children))
  41. {
  42. }
  43. BaseNode& operator=(BaseNode&& other)
  44. {
  45. data_type_ = std::move(other.data_type_);
  46. parent_ = std::move(other.parent_);
  47. children = std::move(other.children);
  48. return *this;
  49. }
  50. #else
  51. BaseNode(BaseNode&&) = default;
  52. BaseNode& operator=(BaseNode&&) = default;
  53. #endif
  54. NodeType getType() const
  55. {
  56. return data_type_;
  57. }
  58. void addChild(std::unique_ptr<BaseNode> child)
  59. {
  60. child->setParent(this);
  61. children.push_back(std::move(child));
  62. }
  63. bool removeChild(const size_t& index)
  64. {
  65. if (children.size() > index)
  66. {
  67. children.erase(children.begin() + index);
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. BaseNode* getChild(const size_t& index) const
  76. {
  77. if (children.empty())
  78. {
  79. return nullptr;
  80. }
  81. return children[index].get();
  82. }
  83. BaseNode* getParent() const
  84. {
  85. return parent_;
  86. }
  87. void setParent(BaseNode* parent)
  88. {
  89. parent_ = parent;
  90. }
  91. size_t getNumChildren() const
  92. {
  93. return children.size();
  94. }
  95. std::vector<std::unique_ptr<BaseNode>>& getChildren()
  96. {
  97. return children;
  98. }
  99. private:
  100. NodeType data_type_;
  101. BaseNode* parent_;
  102. std::vector<std::unique_ptr<BaseNode>> children;
  103. };
  104. template <class T>
  105. class DataNode : public BaseNode
  106. {
  107. public:
  108. DataNode(NodeType type);
  109. DataNode(NodeType type, std::unique_ptr<T> data);
  110. virtual ~DataNode();
  111. DataNode(const DataNode&) = delete;
  112. DataNode& operator=(const DataNode&) = delete;
  113. // C++11 defaulted functions
  114. // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
  115. // supported.
  116. #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
  117. DataNode(DataNode&& other)
  118. : BaseNode(std::move(other)), attributes_(std::move(other.attributes_)), data_(std::move(other.data_))
  119. {
  120. }
  121. DataNode& operator=(DataNode&& other)
  122. {
  123. BaseNode::operator=(std::move(other));
  124. attributes__ = std::move(other.attributes_);
  125. data_ = std::move(other.data_);
  126. return *this;
  127. }
  128. #else
  129. DataNode(DataNode&&) = default;
  130. DataNode& operator=(DataNode&&) = default;
  131. #endif
  132. T* get() const;
  133. std::unique_ptr<T> getData();
  134. void setData(std::unique_ptr<T> data);
  135. void addAttribute(const std::string& name, const std::string& value);
  136. const std::map<std::string, std::string>& getAttributes();
  137. private:
  138. std::map<std::string, std::string> attributes_;
  139. std::unique_ptr<T> data_;
  140. };
  141. template <class T>
  142. DataNode<T>::DataNode(NodeType type) : BaseNode(type), attributes_(), data_(nullptr)
  143. {
  144. }
  145. template <class T>
  146. DataNode<T>::DataNode(NodeType type, std::unique_ptr<T> data) : BaseNode(type), attributes_(), data_(std::move(data))
  147. {
  148. }
  149. template <class T>
  150. DataNode<T>::~DataNode()
  151. {
  152. }
  153. template <class T>
  154. T* DataNode<T>::get() const
  155. {
  156. return data_.get();
  157. }
  158. template <class T>
  159. std::unique_ptr<T> DataNode<T>::getData()
  160. {
  161. return std::move(data_);
  162. }
  163. template <class T>
  164. void DataNode<T>::setData(std::unique_ptr<T> data)
  165. {
  166. data_ = std::move(data);
  167. }
  168. template <class T>
  169. void DataNode<T>::addAttribute(const std::string& name, const std::string& value)
  170. {
  171. attributes_[name] = value;
  172. }
  173. template <class T>
  174. const std::map<std::string, std::string>& DataNode<T>::getAttributes()
  175. {
  176. return attributes_;
  177. }
  178. } // namespace xmlparser
  179. } // namespace fastrtps
  180. } // namespace eprosima
  181. #endif // !_XML_TREE_