forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT29-Storage.cpp
More file actions
71 lines (58 loc) · 2.05 KB
/
T29-Storage.cpp
File metadata and controls
71 lines (58 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: Storage.cpp
@Time: 2025/10/4 13:43
@Desc: 本例子主要展示保存pipeline 到本地,并且重新加载的功能
***************************/
#include <iostream>
#include <cstdio>
#include "MyGNode/MyNode1.h"
#include "MyGNode/MyNode2.h"
using namespace CGraph;
#if __cplusplus >= 201703L
void tutorial_storage_save(const std::string& path) {
GPipelinePtr pipeline = GPipelineFactory::create();
GElementPtr a, b, c, d = nullptr;
CStatus status = pipeline->registerGElement<MyNode1>(&a, {}, "nodeA");
status += pipeline->registerGElement<MyNode2>(&b, {a}, "nodeB");
status += pipeline->registerGElement<MyNode1>(&c, {a}, "nodeC");
status += pipeline->registerGElement<MyNode2>(&d, {b, c}, "nodeD");
status += pipeline->save(path);
if (status.isOK()) {
std::cout << "pipline have been save to " << path << std::endl;
} else {
std::cout << " save pipeline failed ...\n";
}
GPipelineFactory::remove(pipeline); // 析构当前pipeline即可
}
void tutorial_storage_load(const std::string& path) {
// 需要通过如下方式,提前注册所有的使用到的 meta 信息
CGRAPH_REGISTER_META_TYPE(MyNode1);
CGRAPH_REGISTER_META_TYPE(MyNode2);
GPipelinePtr pipeline = GPipelineFactory::create(); // 创建一个新的pipeline,用于加载
std::cout << "create another pipeline, and load pipeline for path : " << path << std::endl;
CStatus status = pipeline->load(path);
if (status.isErr()) {
std::cout << status.getInfo() << std::endl;
return;
}
pipeline->process();
GPipelineFactory::remove(pipeline);
// 删除存储文件
std::remove(path.c_str());
}
void tutorial_storage() {
const std::string& path = "storage.cgraph";
tutorial_storage_save(path);
tutorial_storage_load(path);
}
#else
void tutorial_storage() {
std::cout << "CGraph storage support cpp17 and upper only \n";
}
#endif
int main () {
tutorial_storage();
return 0;
}