MCPcopy Create free account
hub / github.com/ChunelFeng/CGraph / tutorial_function

Function tutorial_function

tutorial/T12-Function.cpp:14–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12using namespace CGraph;
13
14void tutorial_function() {
15 GPipelinePtr pipeline = GPipelineFactory::create();
16 GElementPtr a, b = nullptr;
17 GFunctionPtr c_function, d_function = nullptr; // 申明两个 GFunction 类型的变量
18
19 /** v1.8.3 版本之后,推荐使用+=的形式,对status赋值 */
20 CStatus status = pipeline->registerGElement<MyNode1>(&a, {}, "nodeA", 1);
21 status += pipeline->registerGElement<MyWriteParamNode>(&b, {a}, "nodeB", 1);
22 status += pipeline->registerGElement<GFunction>(&c_function, {b}, "functionC", 1); // 注册GFunction类型的节点c_function
23 status += pipeline->registerGElement<GFunction>(&d_function, {c_function}, "functionD", 3);
24 if (!status.isOK()) {
25 CGRAPH_ECHO("pipeline register failed for the reason : ", status.getInfo().c_str());
26 return;
27 }
28
29 int num = 10;
30 const std::string& info = "Hello, CGraph";
31 c_function->setFunction(CFunctionType::RUN, [num, info] {
32 CGRAPH_ECHO("input num i = [%d], info = [%s]", num, info.c_str()); // 传递pipeline外部参数,并在pipeline内部节点执行时使用
33 return CStatus();
34 });
35
36 /** 通过链式调用的方式,注册多个执行函数 */
37 d_function->setFunction(CFunctionType::INIT, [d_function] {
38 CGRAPH_ECHO("[%s] do init function ...", d_function->getName().c_str());
39 return CStatus();
40 })->setFunction(CFunctionType::RUN, [d_function, num] {
41 auto param = d_function->getGParamWithNoEmpty<MyParam>("param1");
42 param->iCount += num;
43 CGRAPH_ECHO("[%s] do run function, iCount = [%d], iValue = [%d] ...",
44 d_function->getName().c_str(), param->iCount, ++param->iValue);
45 return CStatus();
46 });
47
48 pipeline->process();
49 GPipelineFactory::remove(pipeline);
50}
51
52
53int main() {

Callers 1

mainFunction · 0.70

Calls 6

createFunction · 0.85
CGRAPH_ECHOFunction · 0.85
removeFunction · 0.85
isOKMethod · 0.80
setFunctionMethod · 0.80
processMethod · 0.45

Tested by

no test coverage detected