Fast RTPS  Version 2.4.1
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 
52  // C++11 defaulted functions
53  // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
54  // supported.
55 #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
57  BaseNode&& other)
58  : data_type_(std::move(other.data_type_))
59  , parent_(std::move(other.parent_))
60  , children(std::move(other.children))
61  {
62  }
63 
65  BaseNode&& other)
66  {
67  data_type_ = std::move(other.data_type_);
68  parent_ = std::move(other.parent_);
69  children = std::move(other.children);
70  return *this;
71  }
72 
73 #else
74  BaseNode(
75  BaseNode&&) = default;
77  BaseNode&&) = default;
78 #endif // if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
79 
80  NodeType getType() const
81  {
82  return data_type_;
83  }
84 
85  void addChild(
86  std::unique_ptr<BaseNode> child)
87  {
88  child->setParent(this);
89  children.push_back(std::move(child));
90  }
91 
93  const size_t& index)
94  {
95  if (children.size() > index)
96  {
97  children.erase(children.begin() + index);
98  return true;
99  }
100  else
101  {
102  return false;
103  }
104  }
105 
107  const size_t& index) const
108  {
109  if (children.empty())
110  {
111  return nullptr;
112  }
113  return children[index].get();
114  }
115 
117  {
118  return parent_;
119  }
120 
121  void setParent(
122  BaseNode* parent)
123  {
124  parent_ = parent;
125  }
126 
127  size_t getNumChildren() const
128  {
129  return children.size();
130  }
131 
132  std::vector<std::unique_ptr<BaseNode>>& getChildren()
133  {
134  return children;
135  }
136 
137 private:
138 
139  NodeType data_type_;
140  BaseNode* parent_;
141  std::vector<std::unique_ptr<BaseNode>> children;
142 };
143 
144 template <class T>
145 class DataNode : public BaseNode
146 {
147 public:
148 
149  DataNode(
150  NodeType type);
151  DataNode(
152  NodeType type,
153  std::unique_ptr<T> data);
154  virtual ~DataNode();
155 
157  const DataNode&) = delete;
159  const DataNode&) = delete;
160 
161  // C++11 defaulted functions
162  // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
163  // supported.
164 #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
166  DataNode&& other)
167  : BaseNode(std::move(other))
168  , attributes_(std::move(other.attributes_))
169  , data_(std::move(other.data_))
170  {
171  }
172 
174  DataNode&& other)
175  {
176  BaseNode::operator =(std::move(other));
177  attributes_ = std::move(other.attributes_);
178  data_ = std::move(other.data_);
179  return *this;
180  }
181 
182 #else
183  DataNode(
184  DataNode&&) = default;
186  DataNode&&) = default;
187 #endif // if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
188 
189  T* get() const;
190  std::unique_ptr<T> getData();
191  void setData(
192  std::unique_ptr<T> data);
193 
194  void addAttribute(
195  const std::string& name,
196  const std::string& value);
197  const std::map<std::string, std::string>& getAttributes();
198 
199 private:
200 
201  std::map<std::string, std::string> attributes_;
202  std::unique_ptr<T> data_;
203 };
204 
205 template <class T>
207  NodeType type)
208  : BaseNode(type)
209  , attributes_()
210  , data_(nullptr)
211 {
212 }
213 
214 template <class T>
216  NodeType type,
217  std::unique_ptr<T> data)
218  : BaseNode(type)
219  , attributes_()
220  , data_(std::move(data))
221 {
222 }
223 
224 template <class T>
226 {
227 }
228 
229 template <class T>
231 {
232  return data_.get();
233 }
234 
235 template <class T>
236 std::unique_ptr<T> DataNode<T>::getData()
237 {
238  return std::move(data_);
239 }
240 
241 template <class T>
243  std::unique_ptr<T> data)
244 {
245  data_ = std::move(data);
246 }
247 
248 template <class T>
250  const std::string& name,
251  const std::string& value)
252 {
253  attributes_[name] = value;
254 }
255 
256 template <class T>
257 const std::map<std::string, std::string>& DataNode<T>::getAttributes()
258 {
259  return attributes_;
260 }
261 
262 } // namespace xmlparser
263 } // namespace fastrtps
264 } // namespace eprosima
265 #endif // !_XML_TREE_
BaseNode(const BaseNode &)=delete
bool removeChild(const size_t &index)
Definition: XMLTree.h:92
BaseNode(BaseNode &&other)
Definition: XMLTree.h:56
size_t getNumChildren() const
Definition: XMLTree.h:127
void setParent(BaseNode *parent)
Definition: XMLTree.h:121
void addChild(std::unique_ptr< BaseNode > child)
Definition: XMLTree.h:85
BaseNode * getChild(const size_t &index) const
Definition: XMLTree.h:106
BaseNode & operator=(const BaseNode &)=delete
BaseNode * getParent() const
Definition: XMLTree.h:116
std::vector< std::unique_ptr< BaseNode > > & getChildren()
Definition: XMLTree.h:132
BaseNode(NodeType type)
Definition: XMLTree.h:38
NodeType getType() const
Definition: XMLTree.h:80
DataNode(NodeType type)
Definition: XMLTree.h:206
const std::map< std::string, std::string > & getAttributes()
Definition: XMLTree.h:257
DataNode(const DataNode &)=delete
void addAttribute(const std::string &name, const std::string &value)
Definition: XMLTree.h:249
virtual ~DataNode()
Definition: XMLTree.h:225
std::unique_ptr< T > getData()
Definition: XMLTree.h:236
void setData(std::unique_ptr< T > data)
Definition: XMLTree.h:242
DataNode(DataNode &&other)
Definition: XMLTree.h:165
DataNode & operator=(const DataNode &)=delete
T * get() const
Definition: XMLTree.h:230
NodeType
Definition: XMLTree.h:14
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23