Fast RTPS  Version 2.1.0
Fast RTPS
XMLTree.h
1 #ifndef _XML_TREE_
2 #define _XML_TREE_
3 
4 #include <map>
5 #include <memory>
6 #include <vector>
7 
8 namespace eprosima {
9 namespace fastrtps {
10 namespace xmlparser {
11 
12 enum class NodeType
13 {
14  PROFILES,
16  PUBLISHER,
17  SUBSCRIBER,
18  RTPS,
21  TYPE,
22  TOPIC,
25  ROOT,
26  TYPES,
27  LOG,
28  REQUESTER,
29  REPLIER
30 };
31 
32 class BaseNode
33 {
34  public:
35  BaseNode(NodeType type) : data_type_(type), parent_(nullptr){};
36  virtual ~BaseNode() = default;
37 
38  BaseNode(const BaseNode&) = delete;
39  BaseNode& operator=(const BaseNode&) = delete;
40 
41 // C++11 defaulted functions
42 // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
43 // supported.
44 #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
45  BaseNode(BaseNode&& other)
46  : data_type_(std::move(other.data_type_)), parent_(std::move(other.parent_)), children(std::move(children))
47  {
48  }
50  {
51  data_type_ = std::move(other.data_type_);
52  parent_ = std::move(other.parent_);
53  children = std::move(other.children);
54  return *this;
55  }
56 #else
57  BaseNode(BaseNode&&) = default;
58  BaseNode& operator=(BaseNode&&) = default;
59 #endif
60 
61  NodeType getType() const
62  {
63  return data_type_;
64  }
65 
66  void addChild(std::unique_ptr<BaseNode> child)
67  {
68  child->setParent(this);
69  children.push_back(std::move(child));
70  }
71 
72  bool removeChild(const size_t& index)
73  {
74  if (children.size() > index)
75  {
76  children.erase(children.begin() + index);
77  return true;
78  }
79  else
80  {
81  return false;
82  }
83  }
84 
85  BaseNode* getChild(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(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  NodeType data_type_;
116  BaseNode* parent_;
117  std::vector<std::unique_ptr<BaseNode>> children;
118 };
119 
120 template <class T>
121 class DataNode : public BaseNode
122 {
123  public:
124  DataNode(NodeType type);
125  DataNode(NodeType type, std::unique_ptr<T> data);
126  virtual ~DataNode();
127 
128  DataNode(const DataNode&) = delete;
129  DataNode& operator=(const DataNode&) = delete;
130 
131 // C++11 defaulted functions
132 // Vs2013 partly support defaulted functions. Defaulted move constructors and move assignment operators are not
133 // supported.
134 #if !defined(HAVE_CXX0X) || (defined(_MSC_VER) && _MSC_VER <= 1800)
136  : BaseNode(std::move(other)), attributes_(std::move(other.attributes_)), data_(std::move(other.data_))
137  {
138  }
140  {
141  BaseNode::operator=(std::move(other));
142  attributes__ = std::move(other.attributes_);
143  data_ = std::move(other.data_);
144  return *this;
145  }
146 #else
147  DataNode(DataNode&&) = default;
148  DataNode& operator=(DataNode&&) = default;
149 #endif
150 
151  T* get() const;
152  std::unique_ptr<T> getData();
153  void setData(std::unique_ptr<T> data);
154 
155  void addAttribute(const std::string& name, const std::string& value);
156  const std::map<std::string, std::string>& getAttributes();
157 
158  private:
159  std::map<std::string, std::string> attributes_;
160  std::unique_ptr<T> data_;
161 };
162 
163 template <class T>
164 DataNode<T>::DataNode(NodeType type) : BaseNode(type), attributes_(), data_(nullptr)
165 {
166 }
167 
168 template <class T>
169 DataNode<T>::DataNode(NodeType type, std::unique_ptr<T> data) : BaseNode(type), attributes_(), data_(std::move(data))
170 {
171 }
172 
173 template <class T>
175 {
176 }
177 
178 template <class T>
180 {
181  return data_.get();
182 }
183 
184 template <class T>
185 std::unique_ptr<T> DataNode<T>::getData()
186 {
187  return std::move(data_);
188 }
189 
190 template <class T>
191 void DataNode<T>::setData(std::unique_ptr<T> data)
192 {
193  data_ = std::move(data);
194 }
195 
196 template <class T>
197 void DataNode<T>::addAttribute(const std::string& name, const std::string& value)
198 {
199  attributes_[name] = value;
200 }
201 
202 template <class T>
203 const std::map<std::string, std::string>& DataNode<T>::getAttributes()
204 {
205  return attributes_;
206 }
207 
208 } // namespace xmlparser
209 } // namespace fastrtps
210 } // namespace eprosima
211 #endif // !_XML_TREE_
BaseNode(const BaseNode &)=delete
bool removeChild(const size_t &index)
Definition: XMLTree.h:72
BaseNode(BaseNode &&other)
Definition: XMLTree.h:45
size_t getNumChildren() const
Definition: XMLTree.h:104
void setParent(BaseNode *parent)
Definition: XMLTree.h:99
void addChild(std::unique_ptr< BaseNode > child)
Definition: XMLTree.h:66
BaseNode * getChild(const size_t &index) const
Definition: XMLTree.h:85
BaseNode & operator=(const BaseNode &)=delete
BaseNode & operator=(BaseNode &&other)
Definition: XMLTree.h:49
BaseNode * getParent() const
Definition: XMLTree.h:94
std::vector< std::unique_ptr< BaseNode > > & getChildren()
Definition: XMLTree.h:109
BaseNode(NodeType type)
Definition: XMLTree.h:35
NodeType getType() const
Definition: XMLTree.h:61
DataNode(NodeType type)
Definition: XMLTree.h:164
const std::map< std::string, std::string > & getAttributes()
Definition: XMLTree.h:203
DataNode(const DataNode &)=delete
void addAttribute(const std::string &name, const std::string &value)
Definition: XMLTree.h:197
virtual ~DataNode()
Definition: XMLTree.h:174
std::unique_ptr< T > getData()
Definition: XMLTree.h:185
void setData(std::unique_ptr< T > data)
Definition: XMLTree.h:191
DataNode(DataNode &&other)
Definition: XMLTree.h:135
DataNode & operator=(DataNode &&other)
Definition: XMLTree.h:139
DataNode & operator=(const DataNode &)=delete
T * get() const
Definition: XMLTree.h:179
NodeType
Definition: XMLTree.h:13
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23