commonlib
dx_node.h
Go to the documentation of this file.
1 
11 #pragma once
12 #include "xml_exception.h"
13 
14 BEGIN_NAMESPACE_SERIALIZATION
15 
16 class DxNode;
17 struct DxNodeList;
18 struct DxNodeExtInfo;
19 
20 using DxNodePtr = std::shared_ptr<DxNode>;
21 using DxNodeWPtr = std::weak_ptr<DxNode>;
22 using DxNodeListPtr = std::shared_ptr<DxNodeList>;
23 using DxNodeExtInfoPtr = std::shared_ptr<DxNodeExtInfo>;
24 
28 struct DxNodeList
29 {
30  std::vector<DxNodePtr> list;
31 };
32 
37 {
38  DxNodeListPtr children = nullptr;
39  DxNodeListPtr attributes = nullptr;
40 };
41 
45 class DxNode
46 {
47 public:
48 
52  std::string name;
53 
57  std::string value;
58 
62  DxNodeExtInfoPtr extInfo = nullptr;
63 
67  DxNodeWPtr parent;
68 
72  size_t childIndex = 0;
73 
78  : name("")
79  , value("")
80  {
81  }
82 
87  explicit DxNode(const std::string& name, const std::string& value = "")
88  : name(name)
89  , value(value)
90  {
91  }
92 
96  ~DxNode();
97 
104  static DxNodePtr dxNodeCreate(const std::string& nodeName, const std::string& value);
105 
110  static void dxNodeDestroy(DxNodePtr& node);
111 
117  static void dxNodeSetValue(const DxNodePtr& node, const std::string& value);
118 
123  static void dxNodeMakeChildNodes(const DxNodePtr& node);
124 
130  static void dxNodeRemoveChild(const DxNodePtr& node, DxNodePtr& childNode);
131 
137  static void dxNodeRemoveChild(const DxNodePtr& node, const std::string& name);
138 
144  static void dxNodeRemoveChild(const DxNodePtr& node, const size_t childIndex);
145 
150  static void dxNodeClearChildNodes(const DxNodePtr& node);
151 
157  static void dxNodeRemoveAttributeNode(const DxNodePtr& node, DxNodePtr& attributeNode);
158 
164  static void dxNodeRemoveAttributeNode(const DxNodePtr& node, const std::string& attributeName);
165 
171  static void dxNodeRemoveAttributeNode(const DxNodePtr& node, const size_t attributeIndex);
172 
179  static DxNodePtr dxNodeFindChildNode(const DxNodePtr& node, const std::string& childNodeName);
180 
187  static DxNodePtr dxNodeFindAttributeNode(const DxNodePtr& node, const std::string& attributeNodeName);
188 
194  static void dxNodeAddChildNode(const DxNodePtr& node, const DxNodePtr& childNode);
195 
202  static DxNodePtr dxNodeGetAttribute(const DxNodePtr& node, const size_t attributeIndex);
203 
209  static void dxNodeAddAttributeNode(const DxNodePtr& node, const DxNodePtr& attributeNode);
210 
216  static size_t dxNodeGetChildCount(const DxNodePtr& node);
217 
224  static DxNodePtr dxNodeGetChild(const DxNodePtr& node, const size_t childIndex);
225 
231  static DxNodePtr dxNodeGetNextSibling(const DxNodePtr& node);
232 
238  static size_t dxNodeGetAttributeCount(const DxNodePtr& node);
239 
244  static void dxNodeClearAttributeNodes(const DxNodePtr& node);
245 
251  static DxNodePtr dxNodeClone(const DxNodePtr& sourceNode);
252 
253 private:
254 
255  static void BuildTree(const DxNodePtr& sourceNode, const DxNodePtr& destinationNode);
256  static void dxNodeListRemove(const DxNodeListPtr& nodeList, DxNodePtr& node);
257  static void dxNodeListClear(DxNodeListPtr& nodeList);
258  static void dxNodeListAdd(const DxNodeListPtr& nodeList, const DxNodePtr& node);
259  static DxNodePtr dxNodeListFindNode(const DxNodeListPtr& nodeList, const std::string& nodeName);
260  static void dxNodeListDestroy(DxNodeListPtr& nodeList);
261  static void dxNodeListCreate(DxNodeListPtr& nodeList);
262 };
263 
264 
265 END_NAMESPACE_SERIALIZATION
std::string value
Value of a node.
Definition: dx_node.h:57
static void dxNodeAddAttributeNode(const DxNodePtr &node, const DxNodePtr &attributeNode)
Function adds a given attribute to a given node.
DxNodeExtInfo struct that contains shared pointers to node child and attribute elements.
Definition: dx_node.h:36
static void dxNodeClearAttributeNodes(const DxNodePtr &node)
Function clears all attribute elements of a given node.
static DxNodePtr dxNodeFindChildNode(const DxNodePtr &node, const std::string &childNodeName)
Function returns a shared pointer to a child node with a given name.
static size_t dxNodeGetChildCount(const DxNodePtr &node)
Function returns a number of child elements of a given node.
static void dxNodeClearChildNodes(const DxNodePtr &node)
Function clears all children elements of a given node.
size_t childIndex
Child index of a node.
Definition: dx_node.h:72
static void dxNodeAddChildNode(const DxNodePtr &node, const DxNodePtr &childNode)
Function adds a given child to a given node.
static DxNodePtr dxNodeClone(const DxNodePtr &sourceNode)
Function creates and returns a copy of a given node.
XmlException class that inherits from std::exception and is used for exception handling in xml serial...
DxNode(const std::string &name, const std::string &value="")
Constructor that creates an instance od a DxNode class with a given name.
Definition: dx_node.h:87
static void dxNodeRemoveChild(const DxNodePtr &node, DxNodePtr &childNode)
Function removes a given child from a given node.
DxNode class defines an internal xml node and implements functions for creating, modifying and removi...
Definition: dx_node.h:45
DxNodeWPtr parent
Weak pointer to a parent node.
Definition: dx_node.h:67
static void dxNodeSetValue(const DxNodePtr &node, const std::string &value)
Function sets a value to a node.
DxNode()
Default constructor that creates an instance od a DxNode class with default values.
Definition: dx_node.h:77
static DxNodePtr dxNodeGetNextSibling(const DxNodePtr &node)
Function returns a sibling of a given node (next child of a given node parent).
static DxNodePtr dxNodeFindAttributeNode(const DxNodePtr &node, const std::string &attributeNodeName)
Function returns a shared pointer to an attribute node with a given name.
static void dxNodeRemoveAttributeNode(const DxNodePtr &node, DxNodePtr &attributeNode)
Function removes a given attribute from a given node.
DxNodeExtInfoPtr extInfo
Shared pointer to a DxNodeExtInfo that contains pointers to child and attribute elements.
Definition: dx_node.h:62
static DxNodePtr dxNodeGetChild(const DxNodePtr &node, const size_t childIndex)
Function returns a child with a given index.
static DxNodePtr dxNodeCreate(const std::string &nodeName, const std::string &value)
Function creates and returns a new node with a given name and value.
static void dxNodeMakeChildNodes(const DxNodePtr &node)
Function creates a new pointer to child elements and assigns it to a given node.
DxNodeList struct that contains a DxNode vector.
Definition: dx_node.h:28
~DxNode()
Destructor.
static void dxNodeDestroy(DxNodePtr &node)
Function recursively clears all child nodes and sets node pointer to nullptr.
std::string name
Name of a node.
Definition: dx_node.h:52
static DxNodePtr dxNodeGetAttribute(const DxNodePtr &node, const size_t attributeIndex)
Function returns an attribute with a given index.
static size_t dxNodeGetAttributeCount(const DxNodePtr &node)
Function returns a number of attribute elements of a given node.