-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pybind11 demo #468
Labels
good first issue
Good for newcomers
Comments
from datetime import datetime
import time
from PyCGraph import GNode, GPipeline, CStatus
class MyGNode(GNode):
def __init__(self):
super().__init__()
print("new")
def init(self):
print("init....")
return CStatus()
def run(self):
print("[{0}] run {1}".format(datetime.now(), self.getName()))
time.sleep(1)
return CStatus()
def __del__(self):
print("delete")
if __name__ == '__main__':
ppln = GPipeline()
a = MyGNode()
b = MyGNode()
ppln.registerGElement(a, set(), "nodeA", 1)
ppln.registerGElement(b, {a}, "nodeB", 1)
ppln.process() |
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "CGraph.h"
#include "wrapper/PyWrapperInclude.h"
using namespace CGraph;
namespace py = pybind11;
class MyGParamCpp : public GParam {
public:
explicit MyGParamCpp() : GParam() {};
~MyGParamCpp() override {};
CStatus setup() override {
printf("enter setup .. \n");
return CStatus();
}
CVoid reset(const CStatus& curSts) override {
printf("enter reset .. \n");
return;
}
int prm1 {1};
int prm2 {2};
};
PYBIND11_MODULE(PyCGraph, m) {
py::class_<CStatus>(m, "CStatus")
.def(py::init<>())
.def(py::init<int, const std::string&>())
.def("getCode", &CStatus::getCode)
.def("getInfo", &CStatus::getInfo)
.def("isOK", &CStatus::isOK);
py::enum_<GMultiConditionType>(m, "GMultiConditionType")
.value("SERIAL", GMultiConditionType::SERIAL)
.value("PARALLEL", GMultiConditionType::PARALLEL)
.export_values();
py::class_<GParam, PywGParam, std::unique_ptr<GParam, py::nodelete> >(m, "GParam")
.def(py::init<>());
py::class_<MyGParamCpp, GParam, std::unique_ptr<MyGParamCpp, py::nodelete> >(m, "GParamCpp")
.def(py::init<>())
.def_readwrite("prm1", &MyGParamCpp::prm1)
.def_readwrite("prm2", &MyGParamCpp::prm2)
;
py::class_<PyGPipeline, std::unique_ptr<PyGPipeline, py::nodelete> >(m, "GPipeline")
.def(py::init<>())
.def("init", &PyGPipeline::init)
.def("run", &PyGPipeline::run, py::call_guard<py::gil_scoped_release>())
.def("process", &PyGPipeline::process, py::call_guard<py::gil_scoped_release>(),
py::arg("runTimes") = 1)
.def("destroy", &PyGPipeline::destroy)
.def("registerGElement", &PyGPipeline::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::class_<GElement, PywGElement, std::unique_ptr<GElement, py::nodelete> >(m, "GElement")
.def(py::init<>())
.def("createGParam", &GElement::__createGParam_4py)
.def("getGParam", &GElement::__getGParam_4py)
.def("getName", &GElement::getName)
.def("setName", &GElement::setName)
.def("addDependGElements", &GElement::addDependGElements,
py::arg("elements"))
.def("setLoop", &GElement::setLoop);
py::class_<GNode, PywGNode, GElement, std::unique_ptr<GNode, py::nodelete> >(m, "GNode")
.def(py::init<>());
py::class_<PyGCluster, GElement, std::unique_ptr<PyGCluster, py::nodelete> >(m, "GCluster")
.def(py::init<>())
.def("addGElements", &PyGCluster::addGElements,
py::arg("elements"));
py::class_<PyGRegion, GElement, std::unique_ptr<PyGRegion, py::nodelete> >(m, "GRegion")
.def(py::init<>())
.def("addGElements", &PyGRegion::addGElements,
py::arg("elements"));
py::class_<PywGCondition, GElement, std::unique_ptr<PywGCondition, py::nodelete> >(m, "GCondition")
.def(py::init<>())
.def("addGElements", &PywGCondition::addGElements,
py::arg("elements"));
py::class_<PyGMultiCondition<CGraph::GMultiConditionType::SERIAL>, GElement>(m, "GSerialMultiCondition")
.def(py::init<>())
.def("addGElements", &PyGMultiCondition<CGraph::GMultiConditionType::SERIAL>::addGElements,
py::arg("elements"));
py::class_<PyGMultiCondition<CGraph::GMultiConditionType::PARALLEL>, GElement>(m, "GParallelMultiCondition")
.def(py::init<>())
.def("addGElements", &PyGMultiCondition<CGraph::GMultiConditionType::PARALLEL>::addGElements,
py::arg("elements"));
}
|
搞一个包含纯虚的中间基类试试看 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: