Fast RTPS  Version 2.9.0
Fast RTPS
XMLTree.h
1 #ifndef _XML_TREE_
2 #define _XML_TREE_
3 
4 #include <map>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 namespace eprosima {
10 namespace fastrtps {
11 namespace xmlparser {
12 
13 enum class NodeType
14 {
15  PROFILES,
17  PUBLISHER,
18  SUBSCRIBER,
19  RTPS,
22  TYPE,
23  TOPIC,
26  ROOT,
27  TYPES,
28  LOG,
29  REQUESTER,
30  REPLIER,
32 };
33 
34 class BaseNode
35 {
36 public:
37 
39  NodeType type)
40  : data_type_(type)
41  , parent_(nullptr)
42  {
43  }
44 
45  virtual ~BaseNode() = default;
46 
48  const BaseNode&) = delete;
50  const BaseNode&) = delete;
51 
53  BaseNode&&) = default;
55  BaseNode&&) = default;
56 
57  NodeType getType() const
58  {
59  return data_type_;
60  }
61 
62  void addChild(
63  std::unique_ptr<BaseNode> child)
64  {
65  child->setParent(this);
66  children.push_back(std::move(child));
67  }
68 
70  const size_t& index)
71  {
72  if (children.size() > index)
73  {
74  children.erase(children.begin() + index);
75  return true;
76  }
77  else
78  {
79  return false;
80  }
81  }
82 
84  const size_t& index) const
85  {
86  if (children.empty())
87  {
88  return nullptr;
89  }
90  return children[index].get();
91  }
92 
94  {
95  return parent_;
96  }
97 
98  void setParent(
99  BaseNode* parent)
100  {
101  parent_ = parent;
102  }
103 
104  size_t getNumChildren() const
105  {
106  return children.size();
107  }
108 
109  std::vector<std::unique_ptr<BaseNode>>& getChildren()
110  {
111  return children;
112  }
113 
114 private:
115 
116  NodeType data_type_;
117  BaseNode* parent_;
118  std::vector<std::unique_ptr<BaseNode>> children;
119 };
120 
121 template <class T>
122 class DataNode : public BaseNode
123 {
124 public:
125 
126  DataNode(
127  NodeType type);
128  DataNode(
129  NodeType type,
130  std::unique_ptr<T> data);
131  virtual ~DataNode();
132 
134  const DataNode&) = delete;
136  const DataNode&) = delete;
137 
139  DataNode&&) = default;
141  DataNode&&) = default;
142 
143  T* get() const;
144  std::unique_ptr<T> getData();
145  void setData(
146  std::unique_ptr<T> data);
147 
148  void addAttribute(
149  const std::string& name,
150  const std::string& value);
151  const std::map<std::string, std::string>& getAttributes();
152 
153 private:
154 
155  std::map<std::string, std::string> attributes_;
156  std::unique_ptr<T> data_;
157 };
158 
159 template <class T>
161  NodeType type)
162  : BaseNode(type)
163  , attributes_()
164  , data_(nullptr)
165 {
166 }
167 
168 template <class T>
170  NodeType type,
171  std::unique_ptr<T> data)
172  : BaseNode(type)
173  , attributes_()
174  , data_(std::move(data))
175 {
176 }
177 
178 template <class T>
180 {
181 }
182 
183 template <class T>
185 {
186  return data_.get();
187 }
188 
189 template <class T>
190 std::unique_ptr<T> DataNode<T>::getData()
191 {
192  return std::move(data_);
193 }
194 
195 template <class T>
197  std::unique_ptr<T> data)
198 {
199  data_ = std::move(data);
200 }
201 
202 template <class T>
204  const std::string& name,
205  const std::string& value)
206 {
207  attributes_[name] = value;
208 }
209 
210 template <class T>
211 const std::map<std::string, std::string>& DataNode<T>::getAttributes()
212 {
213  return attributes_;
214 }
215 
216 } // namespace xmlparser
217 } // namespace fastrtps
218 } // namespace eprosima
219 #endif // !_XML_TREE_
BaseNode(const BaseNode &)=delete
bool removeChild(const size_t &index)
Definition: XMLTree.h:69
size_t getNumChildren() const
Definition: XMLTree.h:104
void setParent(BaseNode *parent)
Definition: XMLTree.h:98
void addChild(std::unique_ptr< BaseNode > child)
Definition: XMLTree.h:62
BaseNode * getChild(const size_t &index) const
Definition: XMLTree.h:83
BaseNode & operator=(const BaseNode &)=delete
BaseNode * getParent() const
Definition: XMLTree.h:93
std::vector< std::unique_ptr< BaseNode > > & getChildren()
Definition: XMLTree.h:109
BaseNode(NodeType type)
Definition: XMLTree.h:38
NodeType getType() const
Definition: XMLTree.h:57
DataNode(NodeType type)
Definition: XMLTree.h:160
const std::map< std::string, std::string > & getAttributes()
Definition: XMLTree.h:211
DataNode(const DataNode &)=delete
void addAttribute(const std::string &name, const std::string &value)
Definition: XMLTree.h:203
virtual ~DataNode()
Definition: XMLTree.h:179
std::unique_ptr< T > getData()
Definition: XMLTree.h:190
void setData(std::unique_ptr< T > data)
Definition: XMLTree.h:196
DataNode & operator=(const DataNode &)=delete
T * get() const
Definition: XMLTree.h:184
NodeType
Definition: XMLTree.h:14
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23