Skip to content

Commit dd2ad6c

Browse files
committed
[feat] suppot GParam in PyCGraph.
1 parent 1bba7d7 commit dd2ad6c

14 files changed

Lines changed: 115 additions & 5 deletions

File tree

python/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ project(PyCGraph)
55
set(CMAKE_CXX_STANDARD 11)
66
set(CMAKE_CXX_STANDARD_REQUIRED True)
77

8-
IF(UNIX)
9-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -pthread -fPIC")
10-
ENDIF()
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -pthread -fPIC")
119

1210
find_package(pybind11 CONFIG REQUIRED)
1311

python/PyCGraph.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: PyCGraph.cpp
5+
@Time: 2025/1/30 21:43
6+
@Desc:
7+
***************************/
8+
19
#include <pybind11/pybind11.h>
210
#include <pybind11/stl.h>
311

@@ -20,6 +28,9 @@ PYBIND11_MODULE(PyCGraph, m) {
2028
.value("PARALLEL", GMultiConditionType::PARALLEL)
2129
.export_values();
2230

31+
py::class_<GParam, PywGParam, std::unique_ptr<GParam, py::nodelete> >(m, "GParam")
32+
.def(py::init<>());
33+
2334
py::class_<PyGPipeline, std::unique_ptr<PyGPipeline, py::nodelete> >(m, "GPipeline")
2435
.def(py::init<>())
2536
.def("init", &PyGPipeline::init)
@@ -36,6 +47,8 @@ PYBIND11_MODULE(PyCGraph, m) {
3647

3748
py::class_<GElement, PywGElement, std::unique_ptr<GElement, py::nodelete> >(m, "GElement")
3849
.def(py::init<>())
50+
.def("createGParam", &GElement::__createGParam_4py)
51+
.def("getGParam", &GElement::__getGParam_4py)
3952
.def("getName", &GElement::getName)
4053
.def("setName", &GElement::setName)
4154
.def("addDependGElements", &GElement::addDependGElements,

python/wrapper/PyGPipeline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PyGPipeline : public CGraph::GPipeline {
2020
const CGraph::GElementPtrSet &depends = CGraph::GElementPtrSet{},
2121
const std::string &name = CGraph::CGRAPH_EMPTY,
2222
CSize loop = CGraph::CGRAPH_DEFAULT_LOOP_TIMES) {
23-
return innerRegister(element, depends, name, loop);
23+
return __interRegister_4py(element, depends, name, loop);
2424
}
2525
};
2626

python/wrapper/PyWrapperInclude.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
#include "PyGRegion.h"
1717
#include "PywGCondition.h"
1818
#include "PyGMultiCondition.h"
19+
#include "PywGParam.h"
1920

2021
#endif //CGRAPH_PYWRAPPERINCLUDE_H

python/wrapper/PywGParam.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: PywGParam.h
5+
@Time: 2025/2/1 14:04
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_PYWGPARAM_H
10+
#define CGRAPH_PYWGPARAM_H
11+
12+
#include <pybind11/pybind11.h>
13+
14+
#include "CGraph.h"
15+
16+
namespace py = pybind11;
17+
18+
class PywGParam : public CGraph::GParam {
19+
public:
20+
explicit PywGParam() : CGraph::GParam() {};
21+
~PywGParam() override {};
22+
23+
protected:
24+
CStatus init() override {
25+
PYBIND11_OVERLOAD(CStatus, CGraph::GParam, init);
26+
}
27+
28+
CStatus destroy() override {
29+
PYBIND11_OVERLOAD(CStatus, CGraph::GParam, destroy);
30+
}
31+
32+
CStatus setup() override {
33+
PYBIND11_OVERLOAD(CStatus, CGraph::GParam, setup);
34+
}
35+
36+
CVoid reset(const CStatus& curStatus) override {
37+
PYBIND11_OVERLOAD(CVoid, CGraph::GParam, reset, curStatus);
38+
}
39+
};
40+
41+
#endif //CGRAPH_PYWGPARAM_H

src/GraphCtrl/GraphElement/GElement.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,15 @@ CBool GElement::isDefaultBinding() const {
585585
return CGRAPH_DEFAULT_BINDING_INDEX == binding_index_;
586586
}
587587

588+
589+
CStatus GElement::__createGParam_4py(GParamPtr param, const std::string& key) {
590+
CGRAPH_ASSERT_NOT_NULL(param_manager_)
591+
return param_manager_->__create_4py(param, key);
592+
}
593+
594+
595+
GParamPtr GElement::__getGParam_4py(const std::string& key) {
596+
return param_manager_ ? param_manager_->__get_4py(key) : nullptr;
597+
}
598+
588599
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphElement/GElement.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ class GElement : public GElementObject,
484484
CGRAPH_DECLARE_GPARAM_MANAGER_WRAPPER_WITH_MEMBER
485485
CGRAPH_DECLARE_GEVENT_MANAGER_WRAPPER_WITH_MEMBER
486486
CGRAPH_DECLARE_STAGE_MANAGER_WRAPPER_WITH_MEMBER
487+
488+
public:
489+
/// 以下函数功能,仅供py版本使用
490+
CStatus __createGParam_4py(GParamPtr param, const std::string& key);
491+
GParamPtr __getGParam_4py(const std::string& key);
487492
};
488493

489494
using GElementRef = GElement &;

src/GraphCtrl/GraphParam/GParamManager.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,27 @@ std::vector<std::string> GParamManager::getKeys() {
9999
return keys;
100100
}
101101

102+
103+
CStatus GParamManager::__create_4py(GParamPtr param, const std::string& key) {
104+
CGRAPH_FUNCTION_BEGIN
105+
CGRAPH_LOCK_GUARD lock(this->mutex_);
106+
auto iter = params_map_.find(key);
107+
if (iter != params_map_.end()) {
108+
return CStatus("py: create [" + key + "] param duplicate");
109+
}
110+
111+
params_map_.insert(std::pair<std::string, GParamPtr>(key, param));
112+
CGRAPH_FUNCTION_END
113+
}
114+
115+
116+
GParamPtr GParamManager::__get_4py(const std::string& key) {
117+
const auto& iter = params_map_.find(key);
118+
if (iter == params_map_.end()) {
119+
return nullptr;
120+
}
121+
122+
return iter->second;
123+
}
124+
102125
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphParam/GParamManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class GParamManager : public GParamObject,
8484

8585
friend class GPipeline;
8686
friend class CAllocator;
87+
88+
public:
89+
/// 为 python 版本设定的函数,cpp 的童鞋不需要使用
90+
CStatus __create_4py(GParamPtr param, const std::string& key);
91+
92+
GParamPtr __get_4py(const std::string& key);
8793
};
8894

8995
using GParamManagerPtr = GParamManager *;

src/GraphCtrl/GraphPipeline/GPipeline.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,10 @@ CStatus GPipeline::innerRegister(GElementPtr element, const GElementPtrSet &depe
359359
CGRAPH_FUNCTION_END
360360
}
361361

362+
363+
CStatus GPipeline::__interRegister_4py(CGraph::GElementPtr element, const CGraph::GElementPtrSet &depends,
364+
const std::string &name, CSize loop) {
365+
return innerRegister(element, depends, name, loop);
366+
}
367+
362368
CGRAPH_NAMESPACE_END

0 commit comments

Comments
 (0)