Skip to content

Commit 6d1fd05

Browse files
committed
[feat] add aspect for python, change cpp version for this feat
1 parent 81be2c0 commit 6d1fd05

14 files changed

Lines changed: 175 additions & 20 deletions

File tree

python/PyCGraph.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ PYBIND11_MODULE(PyCGraph, m) {
5757
.def("isOK", &CStatus::isOK)
5858
.def("isErr", &CStatus::isErr);
5959

60+
py::class_<GAspect, PywGAspect, std::unique_ptr<GAspect, py::nodelete> >(m, "GAspect")
61+
.def(py::init<>())
62+
.def("getName", &GAspect::__getName_4py)
63+
.def("getGParam", &GAspect::__getGParam_4py)
64+
.def("getGParamWithNoEmpty", &GAspect::__getGParamWithNoEmpty_4py);
65+
6066
py::class_<GParam, PywGParam, std::unique_ptr<GParam, py::nodelete> >(m, "GParam")
6167
.def(py::init<>())
6268
.def("lock", &GParam::lock,
@@ -106,6 +112,8 @@ PYBIND11_MODULE(PyCGraph, m) {
106112
.def("getName", &GElement::getName)
107113
.def("setName", &GElement::setName)
108114
.def("setLevel", &GElement::setLevel)
115+
.def("addGAspect", &GElement::__addGAspect_4py,
116+
py::keep_alive<1, 2>())
109117
.def("addDependGElements", &GElement::addDependGElements,
110118
py::arg("elements"))
111119
.def("setLoop", &GElement::setLoop);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: MyTimerAspect
5+
@Time: 2025/3/5 23:38
6+
@Desc:
7+
"""
8+
9+
import time
10+
11+
from PyCGraph import GAspect, CStatus
12+
13+
class MyTimerAspect(GAspect):
14+
_start_time = None
15+
16+
def beginRun(self):
17+
self._start_time = time.time()
18+
return CStatus()
19+
20+
def finishRun(self, curStatus: CStatus):
21+
span = time.time() - self._start_time
22+
print('----> [MyTimerAspect] {0} time cost is : {1}s'.format(self.getName(), round(span, 2)))
23+
return
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: __init__.py
5+
@Time: 2025/3/5 23:38
6+
@Desc:
7+
"""

python/tutorial/T09-Aspect.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: T09-Aspect
5+
@Time: 2025/3/5 23:31
6+
@Desc:
7+
"""
8+
9+
10+
from PyCGraph import GPipeline, GAspect, GCluster
11+
12+
from MyGNode.MyNode1 import MyNode1
13+
from MyGNode.MyNode2 import MyNode2
14+
from MyGAspect.MyTimerAspect import MyTimerAspect
15+
16+
17+
def tutorial_aspect():
18+
a, b1, b2, c = MyNode1(), MyNode2('nodeB1'), MyNode1('nodeB2'), MyNode2()
19+
b_cluster = GCluster([b1, b2])
20+
21+
a.addGAspect(MyTimerAspect())
22+
b_cluster.addGAspect(MyTimerAspect())
23+
24+
pipeline = GPipeline()
25+
pipeline.registerGElement(a, set(), 'nodeA')
26+
pipeline.registerGElement(b_cluster, {a}, 'regionB')
27+
pipeline.registerGElement(c, {b_cluster}, 'nodeC')
28+
pipeline.process()
29+
30+
31+
if __name__ == '__main__':
32+
tutorial_aspect()

python/wrapper/PyWrapperInclude.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
#include "PywGCondition.h"
1818
#include "PyGMultiCondition.h"
1919
#include "PywGParam.h"
20+
#include "PywGAspect.h"
2021

2122
#endif //CGRAPH_PYWRAPPERINCLUDE_H

python/wrapper/PywGAspect.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/***************************
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: PywGAspect.h
5+
@Time: 2025/3/5 23:06
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_PYWGASPECT_H
10+
#define CGRAPH_PYWGASPECT_H
11+
12+
#include <pybind11/pybind11.h>
13+
14+
#include "CGraph.h"
15+
16+
namespace py = pybind11;
17+
18+
class PywGAspect : public CGraph::GAspect {
19+
public:
20+
explicit PywGAspect() {};
21+
~PywGAspect() override {};
22+
23+
CStatus beginInit() override {
24+
PYBIND11_OVERLOAD(CStatus, GAspect, beginInit);
25+
}
26+
27+
CVoid finishInit(const CStatus& curStatus) override {
28+
PYBIND11_OVERLOAD(CVoid, GAspect, finishInit, curStatus);
29+
}
30+
31+
CStatus beginRun() override {
32+
PYBIND11_OVERLOAD(CStatus, GAspect, beginRun);
33+
}
34+
35+
CVoid finishRun(const CStatus& curStatus) override {
36+
PYBIND11_OVERLOAD(CVoid, GAspect, finishRun, curStatus);
37+
}
38+
39+
CStatus beginDestroy() override {
40+
PYBIND11_OVERLOAD(CStatus, GAspect, beginDestroy);
41+
}
42+
43+
CVoid finishDestroy(const CStatus& curStatus) override {
44+
PYBIND11_OVERLOAD(CVoid, GAspect, finishDestroy, curStatus);
45+
}
46+
47+
CVoid enterCrashed() override {
48+
PYBIND11_OVERLOAD(CVoid, GAspect, enterCrashed);
49+
}
50+
};
51+
52+
#endif //CGRAPH_PYWGASPECT_H

src/GraphCtrl/GraphAspect/GAspectManager.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ CStatus GAspectManager::popLast() {
9999

100100

101101
CVoidPtr GAspectManager::setGParamManager(GParamManagerPtr pm) {
102+
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(pm)
102103
for (auto *cur: aspect_arr_) {
103104
cur->setGParamManager(pm);
104105
}
@@ -107,10 +108,19 @@ CVoidPtr GAspectManager::setGParamManager(GParamManagerPtr pm) {
107108

108109

109110
CVoidPtr GAspectManager::setGEventManager(GEventManagerPtr em) {
111+
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(em)
110112
for (auto *cur : aspect_arr_) {
111113
cur->setGEventManager(em);
112114
}
113115
return this;
114116
}
115117

118+
CVoidPtr GAspectManager::setBelong(GElement* belong) {
119+
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(belong)
120+
for (auto *cur : aspect_arr_) {
121+
cur->belong_ = belong;
122+
}
123+
return this;
124+
}
125+
116126
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphAspect/GAspectManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ class GAspectManager : public GAspectObject,
4747

4848
CVoidPtr setGEventManager(GEventManagerPtr em) final;
4949

50+
CVoidPtr setBelong(GElement* belong);
51+
5052
CGRAPH_NO_ALLOWED_COPY(GAspectManager)
5153

5254
private:
5355
GAspectPtrArr aspect_arr_; // 存储aspect的容器
5456

5557
friend class GElement;
5658
friend class CAllocator;
59+
friend class GElementManager;
5760
};
5861

5962
using GAspectManagerPtr = GAspectManager *;

src/GraphCtrl/GraphAspect/GAspectObject.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,4 @@ GAspectObject::~GAspectObject() {
2626
CGRAPH_DELETE_PTR(param_)
2727
}
2828

29-
30-
GAspectObjectPtr GAspectObject::setBelong(GElementPtr belong) {
31-
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(belong)
32-
CGRAPH_THROW_EXCEPTION_BY_CONDITION(!belong->isRegistered(), \
33-
"[" + belong->getName() + "] can not add aspect for the reason of no register");
34-
belong_ = belong;
35-
this->setGParamManager(belong->param_manager_);
36-
this->setGEventManager(belong->event_manager_);
37-
return this;
38-
}
39-
4029
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphAspect/GAspectObject.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ class GAspectObject : public GraphObject,
4343
c_enable_if_t<std::is_base_of<GAspectParam, T>::value, int> = 0>
4444
GAspectObject* setAParam(T* param);
4545

46-
/**
47-
* 设置从属的 element信息
48-
* @param belong
49-
* @return
50-
*/
51-
GAspectObject* setBelong(GElement* belong);
52-
5346
const std::string& getName() const override;
5447

5548
CGRAPH_NO_ALLOWED_COPY(GAspectObject)
@@ -76,6 +69,11 @@ class GAspectObject : public GraphObject,
7669
friend class GAspectManager;
7770
friend class GAspect;
7871
friend class GElement;
72+
73+
public:
74+
const std::string& __getName_4py() const {
75+
return getName();
76+
}
7977
};
8078

8179
using GAspectObjectPtr = GAspectObject *;

0 commit comments

Comments
 (0)