forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE02-MockGUI.cpp
More file actions
111 lines (88 loc) · 3.22 KB
/
E02-MockGUI.cpp
File metadata and controls
111 lines (88 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: E02-MockGUI.cpp
@Time: 2023/9/16 21:22
@Desc: 本example主要展示,在不修改之前任何功能(node中的逻辑)的情况下,
通过aspect和event功能,在界面上展示当前正在执行的node,
模拟 GUI 中node的变亮(或者变暗)的操作。
***************************/
#include <iostream>
#include <cmath>
#include <set>
#include "CGraph.h"
using namespace CGraph;
const static char* EXAMPLE_PARAM_KEY = "example-param-key";
const static char* EXAMPLE_EVENT_KEY = "example-event-key";
struct ProcessGParam : public GParam {
CVoid change(const std::string& name, bool isBegin) {
if (isBegin) {
running_elements_.insert(name);
} else {
running_elements_.erase(name);
}
}
CVoid print() {
std::cout << "<";
for (const auto& cur : running_elements_) {
std::cout << " " << cur << " ";
}
std::cout << "> is running..." << std::endl;
}
protected:
std::set<std::string> running_elements_;
};
class ShowGEvent : public GEvent {
CVoid trigger(GEventParamPtr param) override {
auto p = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(ProcessGParam, EXAMPLE_PARAM_KEY);
CGRAPH_PARAM_READ_CODE_BLOCK(p);
p->print();
}
};
class SwitchGAspect : public GAspect {
public:
CStatus beginRun() override {
auto p = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(ProcessGParam, EXAMPLE_PARAM_KEY);
{
CGRAPH_PARAM_WRITE_CODE_BLOCK(p)
p->change(this->getName(), true);
}
notify(EXAMPLE_EVENT_KEY, GEventType::SYNC);
return CStatus();
}
CVoid finishRun(const CStatus& curStatus) override {
auto p = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(ProcessGParam, EXAMPLE_PARAM_KEY);
{
CGRAPH_PARAM_WRITE_CODE_BLOCK(p)
p->change(this->getName(), false);
}
notify(EXAMPLE_EVENT_KEY, GEventType::SYNC);
}
};
class ActionGNode : public GNode {
CStatus run() override {
int ms = std::abs((int)std::random_device{}()) % 4000 + 1000;
CGRAPH_SLEEP_MILLISECOND(ms); // 一个算子,随机休息一段时间,时长 1000~5000 ms
return CStatus();
}
};
void example_mock_gui() {
GElementPtr a, b, c, d, e, f, g = nullptr;
auto pipeline = GPipelineFactory::create();
pipeline->registerGElement<ActionGNode>(&a, {}, "nodeA");
pipeline->registerGElement<ActionGNode>(&b, {a}, "nodeB");
pipeline->registerGElement<ActionGNode>(&c, {a}, "nodeC");
pipeline->registerGElement<ActionGNode>(&d, {b}, "nodeD");
pipeline->registerGElement<ActionGNode>(&e, {b, c}, "nodeE");
pipeline->registerGElement<ActionGNode>(&f, {d, e}, "nodeF");
pipeline->registerGElement<ActionGNode>(&g, {e}, "nodeG");
pipeline->createGParam<ProcessGParam>(EXAMPLE_PARAM_KEY);
pipeline->addGEvent<ShowGEvent>(EXAMPLE_EVENT_KEY);
pipeline->addGAspect<SwitchGAspect>(); // 在每个node,开始和结束的时候,去触发 EXAMPLE_EVENT_KEY 事件,显示当前正在running的node信息
pipeline->process();
GPipelineFactory::clear();
}
int main() {
example_mock_gui();
return 0;
}