Skip to content

Commit

Permalink
[example] add example 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Sep 16, 2023
1 parent 9312ada commit 402091b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *")

cmake_minimum_required(VERSION 3.2.5)

project(CGraph VERSION 2.5.1)
project(CGraph VERSION 2.5.2)

# CGraph默认使用C++11版本,推荐使用C++17版本。暂不支持C++11以下版本
set(CMAKE_CXX_STANDARD 11)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ int main() {
[2023.09.15 - v2.5.1 - Chunel]
* 提供`fence`(栅栏)功能
* 提供`coordinator`(协调)功能
[2023.09.16 - v2.5.2 - Chunel]
* 优化`message`(消息)功能,可以设定写入阻塞时的处理方式
* 添加`example`相关内容,针对不同行业,提供一些简单的实现用例
* 添加`example`相关内容,针对不同行业,提供一些简单实现
</details>
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
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})
Expand Down
104 changes: 104 additions & 0 deletions example/E02-MockGUI.cpp
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;
}
10 changes: 6 additions & 4 deletions src/GraphCtrl/GraphAspect/GAspectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class GAspectManager : public GAspectObject,

/**
* 执行切面逻辑
* @param type
* @param curStatus
* @return
*/
CStatus reflect(const GAspectType& type, const CStatus& curStatus = CStatus()) {
CStatus reflect(const GAspectType& type,
const CStatus& curStatus = CStatus()) {
CGRAPH_FUNCTION_BEGIN

for (GAspectPtr aspect : aspect_arr_) {
Expand Down Expand Up @@ -108,9 +112,7 @@ class GAspectManager : public GAspectObject,
*/
CStatus popLast() {
CGRAPH_FUNCTION_BEGIN
if (0 == getSize()) {
CGRAPH_RETURN_ERROR_STATUS("no aspect to pop")
}
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(0 == getSize(), "no aspect to pop")

auto* last = aspect_arr_.back();
CGRAPH_DELETE_PTR(last);
Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GElement.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GElementPtr GElement::addGAspect(TParam* param) {

template<typename TAspect, typename ...Args,
c_enable_if_t<std::is_base_of<GTemplateAspect<Args...>, TAspect>::value, int>>
GElement* GElement::addGAspect(Args... args) {
GElementPtr GElement::addGAspect(Args... args) {
if (!aspect_manager_) {
aspect_manager_ = CGRAPH_SAFE_MALLOC_COBJECT(GAspectManager)
}
Expand Down
1 change: 0 additions & 1 deletion src/GraphCtrl/GraphElement/GElementRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,4 @@ GElementRepository::~GElementRepository() {
}
}


CGRAPH_NAMESPACE_END
8 changes: 8 additions & 0 deletions src/UtilsCtrl/ThreadPool/Queue/UAtomicRingBufferQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ class UAtomicRingBufferQueue : public UQueueObject {
}

protected:
/**
* 当前队列是否为满
* @return
*/
CBool isFull() {
// 空出来一个位置,这个时候不让 tail写入
return head_ == (tail_ + 1) % capacity_;
}

/**
* 当前队列是否为空
* @return
*/
CBool isEmpty() {
return head_ == tail_;
}
Expand Down

0 comments on commit 402091b

Please sign in to comment.