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