-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9312ada
commit 402091b
Showing
8 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# 对应 example 中的内容 | ||
set(CGRAPH_EXAMPLE_LIST | ||
E01-AutoPilot | ||
E02-MockGUI | ||
) | ||
|
||
foreach(example ${CGRAPH_EXAMPLE_LIST}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@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 ProcessParam : 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 ShowEvent : public GEvent { | ||
CVoid trigger(GEventParamPtr param) override { | ||
auto p = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(ProcessParam, EXAMPLE_PARAM_KEY); | ||
// CGRAPH_PARAM_READ_CODE_BLOCK(p); // 本例中,对 param的处理,并未考虑并发之间的影响。实际操作中,需要注意 | ||
p->print(); | ||
} | ||
}; | ||
|
||
|
||
class SwitchAspect : public GAspect { | ||
public: | ||
CStatus beginRun() override { | ||
auto p = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(ProcessParam, EXAMPLE_PARAM_KEY); | ||
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(ProcessParam, EXAMPLE_PARAM_KEY); | ||
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<ProcessParam>(EXAMPLE_PARAM_KEY); | ||
pipeline->addGEvent<ShowEvent>(EXAMPLE_EVENT_KEY); | ||
pipeline->addGAspect<SwitchAspect>(); // 在每个node,开始和结束的时候,去触发 EXAMPLE_EVENT_KEY 事件,显示当前正在running的node信息 | ||
|
||
pipeline->process(); | ||
GPipelineFactory::clear(); | ||
} | ||
|
||
|
||
int main() { | ||
example_mock_gui(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,5 +135,4 @@ GElementRepository::~GElementRepository() { | |
} | ||
} | ||
|
||
|
||
CGRAPH_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters