Skip to content

Commit 6e98b41

Browse files
committed
Implemented toString for printing
1 parent 0fceb27 commit 6e98b41

File tree

7 files changed

+438
-91
lines changed

7 files changed

+438
-91
lines changed

README.md

Lines changed: 263 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44
Multi Object Tree
55

6+
* Check circles in tree
7+
* Every node have list of pointers to childs and pointer to parent
8+
* Tree can has several root nodes
9+
* Chain style declaration for more comfortable
10+
* Every node can be customized
11+
* Tree has full list of pointers to nodes so searching in nodes is non recursive
12+
* Tree contains list of supported types and check it
13+
* Every type of node must have id (for example like this: `static uint16_t WSJCPP_OBJ_TREE_NODE_INTEGER = 2;`)
14+
* Write tree to binary file
15+
* Read tree from binary file
16+
617
## Integrate
718

819
via wsjcpp
@@ -18,10 +29,202 @@ or download and include sources to your project:
1829
* src/wsjcpp_obj_tree.h
1930
* src/wsjcpp_obj_tree.cpp
2031

32+
## Examples
2133

22-
## Example
34+
### Define your tree example
2335

24-
### Define tree use a chain declaration
36+
``` cpp
37+
class CompStruct : public WsjcppObjTree {
38+
public:
39+
CompStruct() {
40+
addSupportType<WsjcppObjTreeNodeString>();
41+
addSupportType<WsjcppObjTreeNodeInteger>();
42+
addSupportType<WsjcppObjTreeNodeFloat>();
43+
addSupportType<WsjcppObjTreeNodeDouble>();
44+
}
45+
};
46+
```
47+
48+
and use then
49+
50+
``` cpp
51+
CompStruct comp;
52+
WsjcppObjTreeChainDeclare chain(&comp);
53+
// chain.add("111")
54+
55+
```
56+
57+
### Define your node container
58+
59+
1 way: use a generator of code from wsjcpp
60+
61+
```
62+
$ wsjcpp generate WsjcppObjTreeNode Building Address 6
63+
```
64+
Where:
65+
- "Building" - suffix of class
66+
- "Address" - name of class for initialization
67+
- "6" - id of type
68+
69+
*So please not forget implemenet some methods*
70+
71+
2 way: example of full code
72+
73+
example of header:
74+
``` cpp
75+
#ifndef WSJCPP_OBJ_TREE_NODE_BUILDING_H
76+
#define WSJCPP_OBJ_TREE_NODE_BUILDING_H
77+
78+
#include <wsjcpp_obj_tree.h>
79+
80+
class Address {
81+
public:
82+
Address();
83+
Address(const Address& address);
84+
Address(std::string sStreet, std::string sBuilding);
85+
std::string getStreetName() const;
86+
std::string getBuildingNumber() const;
87+
bool operator==(const Address& rhs) const;
88+
89+
private:
90+
std::string m_sStreetName;
91+
std::string m_sBuildingNumber;
92+
};
93+
94+
static uint16_t WSJCPP_OBJ_TREE_NODE_BUILDING = 60;
95+
96+
class WsjcppObjTreeNodeBuilding : public WsjcppObjTreeNode {
97+
public:
98+
WsjcppObjTreeNodeBuilding(WsjcppObjTree *pTree, const Address &nValue = Address());
99+
static uint16_t staticType() { return WSJCPP_OBJ_TREE_NODE_BUILDING; };
100+
101+
const Address &getValue();
102+
void setValue(const Address &nValue);
103+
104+
// custom
105+
void setNumberOfFloors(int n);
106+
int getNumberOfFloors();
107+
108+
// WsjcppObjTreeNode
109+
virtual int getDataSize() override;
110+
virtual const char *getData() override;
111+
virtual std::string toString(const std::string &sIntent = "") override;
112+
113+
private:
114+
Address m_value;
115+
int m_nNumberOfFloors;
116+
};
117+
118+
WSJCPP_OBJ_TREE_CHAIN_DECLARE_INLINE(Address, WsjcppObjTreeNodeBuilding)
119+
120+
#endif // WSJCPP_OBJ_TREE_NODE_BUILDING_H
121+
```
122+
123+
example of implementation
124+
``` cpp
125+
126+
#include "wsjcpp_obj_tree_node_building.h"
127+
#include <wsjcpp_core.h>
128+
#include <wsjcpp_obj_tree.h>
129+
130+
// ---------------------------------------------------------------------
131+
// Address
132+
133+
Address::Address() {
134+
135+
}
136+
137+
// ---------------------------------------------------------------------
138+
139+
Address::Address(const Address& address) {
140+
m_sStreetName = address.getStreetName();
141+
m_sBuildingNumber = address.getBuildingNumber();
142+
}
143+
144+
// ---------------------------------------------------------------------
145+
146+
Address::Address(std::string sStreetName, std::string sBuildingNumber) {
147+
m_sStreetName = sStreetName;
148+
m_sBuildingNumber = sBuildingNumber;
149+
}
150+
151+
// ---------------------------------------------------------------------
152+
153+
std::string Address::getStreetName() const {
154+
return m_sStreetName;
155+
}
156+
157+
// ---------------------------------------------------------------------
158+
159+
std::string Address::getBuildingNumber() const {
160+
return m_sBuildingNumber;
161+
}
162+
163+
// ---------------------------------------------------------------------
164+
165+
bool Address::operator==(const Address& rhs) const {
166+
return this->getStreetName() == rhs.getStreetName()
167+
&& this->getBuildingNumber() == rhs.getBuildingNumber();
168+
}
169+
170+
// ---------------------------------------------------------------------
171+
// WsjcppObjTreeNodeBuilding
172+
173+
WsjcppObjTreeNodeBuilding::WsjcppObjTreeNodeBuilding(WsjcppObjTree *pTree, const Address &nValue)
174+
: WsjcppObjTreeNode(pTree, WsjcppObjTreeNodeBuilding::staticType()) {
175+
this->setValue(nValue);
176+
}
177+
178+
// ---------------------------------------------------------------------
179+
180+
const Address &WsjcppObjTreeNodeBuilding::getValue() {
181+
return m_value;
182+
}
183+
184+
// ---------------------------------------------------------------------
185+
186+
void WsjcppObjTreeNodeBuilding::setValue(const Address &nValue) {
187+
m_value = nValue;
188+
}
189+
190+
// ---------------------------------------------------------------------
191+
192+
void WsjcppObjTreeNodeBuilding::setNumberOfFloors(int n) {
193+
m_nNumberOfFloors = n;
194+
}
195+
196+
// ---------------------------------------------------------------------
197+
198+
int WsjcppObjTreeNodeBuilding::getNumberOfFloors() {
199+
return m_nNumberOfFloors;
200+
}
201+
202+
// ---------------------------------------------------------------------
203+
204+
int WsjcppObjTreeNodeBuilding::getDataSize() {
205+
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getDataSize() Not implemented");
206+
return sizeof(Address);
207+
}
208+
209+
// ---------------------------------------------------------------------
210+
211+
const char *WsjcppObjTreeNodeBuilding::getData() {
212+
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getData() Not implemented");
213+
return reinterpret_cast<const char *>(&m_value);
214+
}
215+
216+
// ---------------------------------------------------------------------
217+
218+
std::string WsjcppObjTreeNodeBuilding::toString(const std::string &sIntent) {
219+
return
220+
"Building: " + m_value.getStreetName() + ", " + m_value.getBuildingNumber()
221+
+ "\n" + sIntent + "number-of-floors: " + std::to_string(m_nNumberOfFloors);
222+
;
223+
}
224+
225+
```
226+
227+
### Define tree use a addNode directrly
25228
26229
```
27230
#include <wsjcpp_core.h>
@@ -35,36 +238,22 @@ int main(int argc, const char* argv[]) {
35238
tree.addSupportType<WsjcppObjTreeNodeFloat>();
36239
tree.addSupportType<WsjcppObjTreeNodeDouble>();
37240

38-
WsjcppObjTreeChainDeclare chain(&tree);
241+
WsjcppObjTreeNode *pRootNode = new WsjcppObjTreeNodeString(&tree, "Motherboard");
242+
tree.addNode(nullptr, pRootNode);
39243

40-
chain
41-
.add("Motherboard")
42-
.add("CPU_SOCKET")
43-
.add("count")
44-
.add(1).up()
45-
.up()
46-
.add("frequency")
47-
.add(3.2).up()
48-
.up()
49-
.add("GPU_SOCKET")
50-
.add(1).up()
51-
.add("USB-A")
52-
.add(4).up()
53-
.add("PCI")
54-
.add(3).up()
55-
.add("PCI_EXPRESS")
56-
.add(1).up()
57-
.up()
58-
.up()
59-
;
244+
WsjcppObjTreeNode *pCPUNode = new WsjcppObjTreeNodeString(&tree, "CPU");
245+
tree.addNode(pRootNode, pCPUNode);
246+
247+
// etc... but in this case I like "chain declaration" see next
60248

61249
return 0;
62250
}
63251
```
64252
65-
### Define tree use a addNode directrly
253+
### Define tree use a chain declaration
66254
67255
```
256+
#include <iostream>
68257
#include <wsjcpp_core.h>
69258
#include <wsjcpp_obj_tree.h>
70259

@@ -75,68 +264,70 @@ int main(int argc, const char* argv[]) {
75264
tree.addSupportType<WsjcppObjTreeNodeInteger>();
76265
tree.addSupportType<WsjcppObjTreeNodeFloat>();
77266
tree.addSupportType<WsjcppObjTreeNodeDouble>();
267+
tree.addSupportType<WsjcppObjTreeNodeBuilding>();
78268

79-
// add node directly
80-
WsjcppObjTreeNode *pRootNode = new WsjcppObjTreeNodeString(&tree, "Motherboard");
81-
tree.addNode(nullptr, pRootNode);
82-
83-
// in next step use a chain declaration
269+
Address addr0("Morpheus","1/35a");
270+
84271
WsjcppObjTreeChainDeclare chain(&tree);
85-
chain.switchTo(pRootNode); // switch to specific node
272+
86273
chain
87-
.add("CPU_SOCKET")
88-
.add("count")
274+
.add(addr0)
275+
.up()
276+
.add("Motherboard")
277+
.add("CPU_SOCKET")
278+
.add("count")
279+
.add(1).up()
280+
.up()
281+
.add("frequency")
282+
.add(3.2).up()
283+
.up()
284+
.up()
285+
.add("GPU_SOCKET")
89286
.add(1).up()
90287
.up()
91-
.add("frequency")
92-
.add(3.2).up()
288+
.add("USB-A")
289+
.add(4).up()
290+
.up()
291+
.add("PCI")
292+
.add(3).up()
293+
.up()
294+
.add("PCI_EXPRESS")
295+
.add(1).up()
93296
.up()
94-
.add("GPU_SOCKET")
95-
.add(1).up()
96-
.add("USB-A")
97-
.add(4).up()
98-
.add("PCI")
99-
.add(3).up()
100-
.add("PCI_EXPRESS")
101-
.add(1).up()
102297
.up()
103298
;
104299

105-
return 0;
106-
}
107-
```
108-
109-
### Define your tree example
110-
111-
``` cpp
112-
class CompStruct : public WsjcppObjTree {
113-
public:
114-
CompStruct() {
115-
addSupportType<WsjcppObjTreeNodeString>();
116-
addSupportType<WsjcppObjTreeNodeInteger>();
117-
addSupportType<WsjcppObjTreeNodeFloat>();
118-
addSupportType<WsjcppObjTreeNodeDouble>();
300+
std::vector<WsjcppObjTreeNodeBuilding *> vFoundNodes;
301+
if (tree.findNodes(addr0, vFoundNodes) > 0) {
302+
for (int i = 0; i < vFoundNodes.size(); i++) {
303+
vFoundNodes[i]->setNumberOfFloors(5);
119304
}
120-
};
121-
```
305+
}
122306

123-
and use then
124-
125-
``` cpp
126-
CompStruct comp;
127-
WsjcppObjTreeChainDeclare chain(&comp);
128-
// chain.add("111")
307+
std::cout << tree.toString();
129308

309+
return 0;
310+
}
130311
```
131312
132-
### Define your node container
133-
134-
1 way: use a generator of code from wsjcpp
313+
rhis example will print next:
135314
136315
```
137-
$ wsjcpp generate WsjcppObjTreeNode Building Address 6
316+
├─ Building: st. Morpheus, 1/35a
317+
│ number-of-floors: 5
318+
└─ string: Motherboard
319+
├─ string: CPU_SOCKET
320+
│ ├─ string: count
321+
│ │ └─ int: 1
322+
│ └─ string: frequency
323+
│ └─ double: 3.200000
324+
├─ string: GPU_SOCKET
325+
│ └─ int: 1
326+
├─ string: USB-A
327+
│ └─ int: 4
328+
├─ string: PCI
329+
│ └─ int: 3
330+
└─ string: PCI_EXPRESS
331+
└─ int: 1
138332
```
139-
where:
140-
- "Building" - suffic of class
141-
- "Address" - name of class for initialization
142-
- "6" - id of type
333+

0 commit comments

Comments
 (0)