Skip to content

Commit

Permalink
[feat] add GRegion & GCondition python version.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Jan 27, 2025
1 parent 37d89f5 commit 7ecbf2d
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ int main() {
* 修复辅助线程异常等待问题
* 更新`tutorial`内容

[2025.01.26 - v2.7.0 - Chunel]
[2025.01.27 - v3.0.0 - Chunel]
* 提供`stage`(阶段)功能,用于`element`之间同步运行
* 提供简单 Python 封装功能
* 提供 Python 封装
* 更新`tutorial`内容

</details>
Expand Down
28 changes: 21 additions & 7 deletions python/pyCGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ PYBIND11_MODULE(pyCGraph, m) {
py::arg("runTimes") = 1)
.def("destroy", &GPipelinePy::destroy)
.def("registerGElement", &GPipelinePy::registerGElement,
py::arg("element"),
py::arg("depends") = GElementPtrSet{},
py::arg("name") = CGRAPH_EMPTY,
py::arg("loop") = CGRAPH_DEFAULT_LOOP_TIMES,
"register a GElement with dependencies, name, and loop count.");
py::arg("element"),
py::arg("depends") = GElementPtrSet{},
py::arg("name") = CGRAPH_EMPTY,
py::arg("loop") = CGRAPH_DEFAULT_LOOP_TIMES,
"register a GElement with dependencies, name, and loop count.");

py::class_<GElement, GElementPyw, std::unique_ptr<GElement, py::nodelete> >(m, "GElement")
.def(py::init<>());
.def(py::init<>())
.def("getName", &GElement::getName)
.def("setName", &GElement::setName)
.def("addDependGElements", &GElement::addDependGElements,
py::arg("elements"))
.def("setLoop", &GElement::setLoop);

py::class_<GNode, GNodePyw, GElement, std::unique_ptr<GNode, py::nodelete> >(m, "GNode")
.def(py::init<>());

py::class_<GRegionPy, GElement, std::unique_ptr<GRegionPy, py::nodelete> >(m, "GRegion")
.def(py::init<>())
.def("addGElement", &GRegionPy::addGElement,
py::arg("element"));

py::class_<GConditionPyw, GElement, std::unique_ptr<GConditionPyw, py::nodelete> >(m, "GCondition")
.def(py::init<>())
.def("getName", &GNode::getName);
.def("addGElement", &GConditionPyw::addGElement,
py::arg("element"));
}
24 changes: 24 additions & 0 deletions python/wrapper/GConditionPyw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CGRAPH_GCONDITION_PYW_H
#define CGRAPH_GCONDITION_PYW_H

#include <pybind11/pybind11.h>

#include "CGraph.h"

namespace py = pybind11;

class GConditionPyw : public CGraph::GCondition {
public:
explicit GConditionPyw() : CGraph::GCondition() {};
~GConditionPyw() override {};

CIndex choose() override {
PYBIND11_OVERLOAD_PURE(CIndex, GConditionPyw, choose);
}

CStatus addGElement(CGraph::GElementPtr element) {
return addElement(element);
}
};

#endif // CGRAPH_GCONDITION_PYW_H
6 changes: 3 additions & 3 deletions python/wrapper/GElementPyw.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CGRAPH_GELEMENTPY_H
#define CGRAPH_GELEMENTPY_H
#ifndef CGRAPH_GELEMENT_PYW_H
#define CGRAPH_GELEMENT_PYW_H

#include <pybind11/pybind11.h>

Expand All @@ -14,4 +14,4 @@ class GElementPyw : public CGraph::GElement {
}
};

#endif // CGRAPH_GELEMENTPY_H
#endif // CGRAPH_GELEMENT_PYW_H
18 changes: 11 additions & 7 deletions python/wrapper/GNodePyw.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CGRAPH_GNODEPY_H
#define CGRAPH_GNODEPY_H
#ifndef CGRAPH_GNODE_PYW_H
#define CGRAPH_GNODE_PYW_H

#include <pybind11/pybind11.h>

Expand All @@ -9,21 +9,25 @@ namespace py = pybind11;

class GNodePyw : public CGraph::GNode {
protected:
CStatus run() override {
PYBIND11_OVERLOAD_PURE(CStatus, GNode, run);
}

CStatus init() override {
PYBIND11_OVERLOAD(CStatus, GNode, init);
}

CStatus run() override {
PYBIND11_OVERLOAD_PURE(CStatus, GNode, run);
}

CStatus destroy() override {
PYBIND11_OVERLOAD(CStatus, GNode, destroy);
}

CBool isHold() override {
PYBIND11_OVERLOAD(CBool, GNode, isHold);
}

CBool isMatch() override {
PYBIND11_OVERLOAD(CBool, GNode, isMatch);
}
};

#endif // CGRAPH_GNODEPY_H
#endif // CGRAPH_GNODE_PYW_H
8 changes: 4 additions & 4 deletions python/wrapper/GPipelinePy.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CGRAPH_GPIPELINEPY_H
#define CGRAPH_GPIPELINEPY_H
#ifndef CGRAPH_GPIPELINE_PY_H
#define CGRAPH_GPIPELINE_PY_H

#include "CGraph.h"

class GPipelinePy : public CGraph::GPipeline {
public:
explicit GPipelinePy() : CGraph::GPipeline() {}
virtual ~GPipelinePy() {}
~GPipelinePy() override {}

CStatus registerGElement(CGraph::GElementPtr element,
const CGraph::GElementPtrSet &depends = CGraph::GElementPtrSet{},
Expand All @@ -16,4 +16,4 @@ class GPipelinePy : public CGraph::GPipeline {
}
};

#endif // CGRAPH_GPIPELINEPY_H
#endif // CGRAPH_GPIPELINE_PY_H
16 changes: 16 additions & 0 deletions python/wrapper/GRegionPy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CGRAPH_GREGION_PY_H
#define CGRAPH_GREGION_PY_H

#include "CGraph.h"

class GRegionPy : public CGraph::GRegion {
public:
explicit GRegionPy() : CGraph::GRegion() {};
~GRegionPy() override {};

CStatus addGElement(CGraph::GElementPtr element) {
return addElement(element);
}
};

#endif // CGRAPH_GREGION_PY_H
8 changes: 5 additions & 3 deletions python/wrapper/PyWrapperInclude.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef CGRAPH_PYWRAPPERINCLUDE_H
#define CGRAPH_PYWRAPPERINCLUDE_H
#ifndef CGRAPH_PYWRAPPER_INCLUDE_H
#define CGRAPH_PYWRAPPER_INCLUDE_H

#include "GPipelinePy.h"
#include "GElementPyw.h"
#include "GNodePyw.h"
#include "GRegionPy.h"
#include "GConditionPyw.h"

#endif // CGRAPH_PYWRAPPERINCLUDE_H
#endif // CGRAPH_PYWRAPPER_INCLUDE_H
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class GElement : public GElementObject,
CBool visible_ { true }; // 判定可见的,如果被删除的话,则认为是不可见的
CBool is_init_ { false }; // 判断是否init
GElementType element_type_ { GElementType::ELEMENT }; // 用于区分element 内部类型
std::atomic<GElementState> cur_state_ { GElementState::CREATE }; // 当前执行状态
std::atomic<GElementState> cur_state_ { GElementState::NORMAL }; // 当前执行状态
internal::GElementShape shape_ { internal::GElementShape::NORMAL }; // 元素位置类型

/** 配置相关信息 */
Expand Down
3 changes: 1 addition & 2 deletions src/GraphCtrl/GraphElement/GElementDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ enum class GElementType {


enum class GElementState {
CREATE = 0x0000, // 创建状态(暂未init的情况,包含 destroy之后的情况)
NORMAL = 0x1000, // 正常执行状态
NORMAL = 0x0000, // 正常状态
CANCEL = 0x1001, // 取消状态
YIELD = 0x1002, // 暂停状态
TIMEOUT = 0x1010, // 超时状态
Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GElementRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ CStatus GElementRepository::destroy() {
* 当程序 cancel完成之后,就会重新恢复 CREATE的状态
* 问题详见: https://github.com/ChunelFeng/CGraph/issues/153
*/
status = pushAllState(GElementState::CREATE);
status = pushAllState(GElementState::NORMAL);
CGRAPH_FUNCTION_END
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GElementRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GElementRepository : public GElementObject {

private:
GElementPtrSet elements_ {}; // 用于记录所有的element信息
GElementState cur_state_ { GElementState::CREATE }; // 当前状态信息
GElementState cur_state_ { GElementState::NORMAL }; // 当前状态信息
GElementPtrSet async_elements_ {}; // 所有异步执行的逻辑,到后来一次性统一获取执行结果信息

friend class GPipeline;
Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GGroup/GGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class GGroup : public GElement {

CBool isSerializable() const override;

private:
explicit GGroup();

private:
CStatus addManagers(GParamManagerPtr paramManager,
GEventManagerPtr eventManager,
GStageManagerPtr stageManager) override;
Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GGroup/GRegion/GRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class GRegion : public GGroup {
CStatus run() final;
CStatus destroy() final;

private:
CStatus addElement(GElementPtr element) final;

private:
CVoid dump(std::ostream& oss) final;

CBool isSerializable() const final;
Expand Down

0 comments on commit 7ecbf2d

Please sign in to comment.