Skip to content

Commit 0fceb27

Browse files
committed
Updated README.md and added generate.WsjcppObjTreeNode
1 parent bb1a725 commit 0fceb27

File tree

8 files changed

+407
-3
lines changed

8 files changed

+407
-3
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-obj-tree_SOURCE_DIR})
99

1010
# include header dirs
1111
list (APPEND WSJCPP_INCLUDE_DIRS "src")
12+
list (APPEND WSJCPP_INCLUDE_DIRS "src/examples")
1213

1314
list (APPEND WSJCPP_SOURCES "./src/wsjcpp_obj_tree.h")
1415
list (APPEND WSJCPP_SOURCES "./src/wsjcpp_obj_tree.cpp")
1516
list (APPEND WSJCPP_SOURCES "src/main.cpp")
1617

18+
# examples
19+
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_obj_tree_node_building.h")
20+
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_obj_tree_node_building.cpp")
21+
1722
#### BEGIN_WSJCPP_APPEND
1823
#### END_WSJCPP_APPEND
1924

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,140 @@
33

44
Multi Object Tree
55

6+
## Integrate
7+
8+
via wsjcpp
9+
10+
```
11+
$ wsjcpp install https://github.com/wsjcpp/wsjcpp-obj-tree:master
12+
```
13+
14+
or download and include sources to your project:
15+
16+
* src.wsjcpp/wsjcpp_core/wsjcpp_core.h
17+
* src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp
18+
* src/wsjcpp_obj_tree.h
19+
* src/wsjcpp_obj_tree.cpp
20+
21+
22+
## Example
23+
24+
### Define tree use a chain declaration
25+
26+
```
27+
#include <wsjcpp_core.h>
28+
#include <wsjcpp_obj_tree.h>
29+
30+
int main(int argc, const char* argv[]) {
31+
32+
WsjcppObjTree tree;
33+
tree.addSupportType<WsjcppObjTreeNodeString>();
34+
tree.addSupportType<WsjcppObjTreeNodeInteger>();
35+
tree.addSupportType<WsjcppObjTreeNodeFloat>();
36+
tree.addSupportType<WsjcppObjTreeNodeDouble>();
37+
38+
WsjcppObjTreeChainDeclare chain(&tree);
39+
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+
;
60+
61+
return 0;
62+
}
63+
```
64+
65+
### Define tree use a addNode directrly
66+
67+
```
68+
#include <wsjcpp_core.h>
69+
#include <wsjcpp_obj_tree.h>
70+
71+
int main(int argc, const char* argv[]) {
72+
73+
WsjcppObjTree tree;
74+
tree.addSupportType<WsjcppObjTreeNodeString>();
75+
tree.addSupportType<WsjcppObjTreeNodeInteger>();
76+
tree.addSupportType<WsjcppObjTreeNodeFloat>();
77+
tree.addSupportType<WsjcppObjTreeNodeDouble>();
78+
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
84+
WsjcppObjTreeChainDeclare chain(&tree);
85+
chain.switchTo(pRootNode); // switch to specific node
86+
chain
87+
.add("CPU_SOCKET")
88+
.add("count")
89+
.add(1).up()
90+
.up()
91+
.add("frequency")
92+
.add(3.2).up()
93+
.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()
102+
.up()
103+
;
104+
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>();
119+
}
120+
};
121+
```
122+
123+
and use then
124+
125+
``` cpp
126+
CompStruct comp;
127+
WsjcppObjTreeChainDeclare chain(&comp);
128+
// chain.add("111")
129+
130+
```
131+
132+
### Define your node container
133+
134+
1 way: use a generator of code from wsjcpp
135+
136+
```
137+
$ wsjcpp generate WsjcppObjTreeNode Building Address 6
138+
```
139+
where:
140+
- "Building" - suffic of class
141+
- "Address" - name of class for initialization
142+
- "6" - id of type
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/wsjcpp-safe-scripting
2+
3+
# log_info rootdir
4+
# log_info script_filename
5+
6+
make_dir "src"
7+
8+
var in_class_name
9+
set_value in_class_name arg1
10+
normalize_class_name in_class_name
11+
12+
var class_name
13+
concat class_name "WsjcppObjTreeNode" in_class_name
14+
normalize_class_name class_name
15+
16+
var name_of_type
17+
concat name_of_type "WsjcppObjTreeNode" in_class_name
18+
convert_CamelCase_to_snake_case name_of_type name_of_type
19+
to_upper_case name_of_type
20+
21+
var init_class_name
22+
set_value init_class_name arg2
23+
24+
var id_of_type
25+
set_value id_of_type arg3
26+
27+
# convert_CamelCase_to_snake_case class_name class_name
28+
# normalize_class_name class_name
29+
30+
var base_filename
31+
convert_CamelCase_to_snake_case class_name base_filename
32+
# log_info base_filename
33+
34+
var filename_cpp
35+
concat filename_cpp "./src/" base_filename ".cpp"
36+
37+
var filename_h
38+
concat filename_h "./src/" base_filename ".h"
39+
40+
var ifndef_header
41+
set_value ifndef_header base_filename
42+
concat ifndef_header "_H"
43+
44+
to_upper_case ifndef_header
45+
46+
var content_header
47+
concat content_header "#ifndef " ifndef_header "
48+
#define " ifndef_header "
49+
50+
#include <wsjcpp_obj_tree.h>
51+
52+
class " init_class_name " {
53+
// nothing yet
54+
};
55+
56+
static uint16_t " name_of_type " = " id_of_type ";
57+
58+
class " class_name " : public WsjcppObjTreeNode {
59+
public:
60+
" class_name "(WsjcppObjTree *pTree, const " init_class_name " &nValue = " init_class_name "());
61+
static uint16_t staticType() { return " name_of_type "; };
62+
63+
const " init_class_name " &getValue();
64+
void setValue(const " init_class_name " &nValue);
65+
66+
// WsjcppObjTreeNode
67+
virtual int getDataSize() override;
68+
virtual const char *getData() override;
69+
70+
private:
71+
" init_class_name " m_value;
72+
};
73+
74+
WSJCPP_OBJ_TREE_CHAIN_DECLARE_INLINE(" init_class_name ", " class_name ")
75+
76+
#endif // " ifndef_header
77+
78+
79+
var content_source
80+
concat content_source "
81+
#include \"" base_filename ".h\"
82+
#include <wsjcpp_core.h>
83+
#include <wsjcpp_obj_tree.h>
84+
85+
// ---------------------------------------------------------------------
86+
// " class_name "
87+
88+
" class_name "::" class_name "(WsjcppObjTree *pTree, const " init_class_name " &nValue)
89+
: WsjcppObjTreeNode(pTree, " class_name "::staticType()) {
90+
this->setValue(nValue);
91+
}
92+
93+
// ---------------------------------------------------------------------
94+
95+
const " init_class_name " &" class_name "::getValue() {
96+
return m_value;
97+
}
98+
99+
// ---------------------------------------------------------------------
100+
101+
void " class_name "::setValue(const " init_class_name " &nValue) {
102+
m_value = nValue;
103+
}
104+
105+
// ---------------------------------------------------------------------
106+
107+
int " class_name "::getDataSize() {
108+
WsjcppLog::throw_err(\"" class_name "\", \"::getDataSize() Not implemented\");
109+
return sizeof(" init_class_name ");
110+
}
111+
112+
// ---------------------------------------------------------------------
113+
114+
const char *" class_name "::getData() {
115+
WsjcppLog::throw_err(\"" class_name "\", \"::getData() Not implemented\");
116+
return reinterpret_cast<const char *>(&m_value);
117+
}
118+
119+
"
120+
121+
var file_source
122+
concat file_source "src/" filename_cpp
123+
124+
write_file filename_h content_header
125+
write_file filename_cpp content_source
126+
127+
log_info "
128+
======
129+
Generated class:
130+
- " class_name "
131+
Generated files:
132+
- " filename_h "
133+
- " filename_cpp "
134+
======
135+
"
136+
137+
cmakelists_txt_append_wsjcpp filename_h
138+
cmakelists_txt_append_wsjcpp filename_cpp
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#include "wsjcpp_obj_tree_node_building.h"
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_obj_tree.h>
5+
6+
// ---------------------------------------------------------------------
7+
// Address
8+
9+
Address::Address() {
10+
11+
}
12+
13+
// ---------------------------------------------------------------------
14+
15+
Address::Address(const Address& address) {
16+
m_sStreet = address.getStreet();
17+
m_sBuilding = address.getBuilding();
18+
}
19+
20+
// ---------------------------------------------------------------------
21+
22+
Address::Address(std::string sStreet, std::string sBuilding) {
23+
m_sStreet = sStreet;
24+
m_sBuilding = sBuilding;
25+
}
26+
27+
// ---------------------------------------------------------------------
28+
29+
std::string Address::getStreet() const {
30+
return m_sStreet;
31+
}
32+
33+
// ---------------------------------------------------------------------
34+
35+
std::string Address::getBuilding() const {
36+
return m_sBuilding;
37+
}
38+
39+
// ---------------------------------------------------------------------
40+
// WsjcppObjTreeNodeBuilding
41+
42+
WsjcppObjTreeNodeBuilding::WsjcppObjTreeNodeBuilding(WsjcppObjTree *pTree, const Address &nValue)
43+
: WsjcppObjTreeNode(pTree, WsjcppObjTreeNodeBuilding::staticType()) {
44+
this->setValue(nValue);
45+
}
46+
47+
// ---------------------------------------------------------------------
48+
49+
const Address &WsjcppObjTreeNodeBuilding::getValue() {
50+
return m_value;
51+
}
52+
53+
// ---------------------------------------------------------------------
54+
55+
void WsjcppObjTreeNodeBuilding::setValue(const Address &nValue) {
56+
m_value = nValue;
57+
}
58+
59+
// ---------------------------------------------------------------------
60+
61+
int WsjcppObjTreeNodeBuilding::getDataSize() {
62+
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getDataSize() Not implemented");
63+
return sizeof(Address);
64+
}
65+
66+
// ---------------------------------------------------------------------
67+
68+
const char *WsjcppObjTreeNodeBuilding::getData() {
69+
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getData() Not implemented");
70+
return reinterpret_cast<const char *>(&m_value);
71+
}
72+
73+

0 commit comments

Comments
 (0)