bool GetContent(const std::string &file_name, std::string *content) {
std::ifstream fin(file_name);
if (!fin) {
return false;
}
std::stringstream str_stream;
str_stream << fin.rdbuf();
*content = str_stream.str();
return true;
}
- 创建proto格式配置文件(dag_config.proto)
message DAGConfig {
enum SubnodeType {
SUBNODE_IN = 1;
SUBNODE_OUT = 2;
SUBNODE_NORMAL = 3;
};
message Subnode {
required int32 id = 1;
required string name = 2;
optional string reserve = 3;
optional SubnodeType type = 4 [default = SUBNODE_NORMAL];
};
message SubnodeConfig {
repeated Subnode subnodes = 1;
};
message Event {
required int32 id = 1;
optional string name = 2;
};
message Edge {
required int32 id = 1;
required int32 from_node = 2;
required int32 to_node = 3;
repeated Event events = 4;
};
message EdgeConfig {
repeated Edge edges = 1;
}
message SharedData {
required int32 id = 1;
required string name = 2;
};
message SharedDataConfig {
repeated SharedData datas = 1;
}
required SubnodeConfig subnode_config = 1;
required EdgeConfig edge_config = 2;
required SharedDataConfig data_config = 3;
};
- 将proto文件编译成.cc和.h文件
略
- 使用protobuf的序列化进行配置文件解析
DAGConfig dag_config;
string content;
if (!GetContent(dag_config_path, &content)) {
return false;
}
if (!TextFormat::ParseFromString(content, &dag_config)) {
return false;
}