NVIDIA DeepStream SDK API Reference

6.4 Release
yaml_config.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 
14 #ifndef DS3D_COMMON_HPP_YAML_CONFIG_HPP
15 #define DS3D_COMMON_HPP_YAML_CONFIG_HPP
16 
17 #include <ds3d/common/common.h>
18 #include <ds3d/common/idatatype.h>
19 #include <ds3d/common/type_trait.h>
20 #include <ds3d/common/config.h>
21 #include <yaml-cpp/yaml.h>
22 
23 #include <stdexcept>
24 #include <string>
25 
26 namespace ds3d { namespace config {
27 
28 ErrCode inline CatchYamlCall(std::function<ErrCode()> f)
29 {
30  ErrCode code = ErrCode::kGood;
31  DS3D_TRY { code = f(); }
32  DS3D_CATCH_ERROR(Exception, ErrCode::kConfig, "parse config error")
33  DS3D_CATCH_ERROR(std::runtime_error, ErrCode::kConfig, "parse config failed")
34  DS3D_CATCH_ERROR(std::exception, ErrCode::kConfig, "parse config failed")
35  DS3D_CATCH_ANY(ErrCode::kConfig, "parse config failed")
36  return code;
37 }
38 
39 template <class F, typename... Args>
40 ErrCode
41 CatchConfigCall(F f, Args&&... args)
42 {
43  ErrCode code = ErrCode::kGood;
44  DS3D_TRY { code = f(std::forward<Args>(args)...); }
45  DS3D_CATCH_ERROR(Exception, ErrCode::kConfig, "parse config error")
46  DS3D_CATCH_ERROR(std::runtime_error, ErrCode::kConfig, "parse config failed")
47  DS3D_CATCH_ERROR(std::exception, ErrCode::kConfig, "parse config failed")
48  DS3D_CATCH_ANY(ErrCode::kConfig, "parse config failed")
49  return code;
50 }
51 
52 inline ErrCode
53 parseComponentConfig(const std::string& yamlComp, const std::string& path, ComponentConfig& config)
54 {
55  config.filePath = path;
56 
57  YAML::Node node = YAML::Load(yamlComp);
58  config.name = node["name"].as<std::string>();
59  DS3D_FAILED_RETURN(!config.name.empty(), ErrCode::kConfig, "name not found in config");
60  std::string type = node["type"].as<std::string>();
61  DS3D_FAILED_RETURN(!type.empty(), ErrCode::kConfig, "component type must be specified");
62  config.type = componentType(type);
64  config.type != ComponentType::kNone, ErrCode::kConfig, "type not found in config");
65  auto inCapsNode = node["in_caps"];
66  if (inCapsNode) {
67  config.gstInCaps = inCapsNode.as<std::string>();
68  }
69  auto outCapsNode = node["out_caps"];
70  if (outCapsNode) {
71  config.gstOutCaps = outCapsNode.as<std::string>();
72  }
73  auto linkToNode = node["link_to"];
74  if (linkToNode) {
75  config.linkTo = linkToNode.as<std::string>();
76  }
77  auto linkFromNode = node["link_from"];
78  if (linkFromNode) {
79  config.linkFrom = linkFromNode.as<std::string>();
80  }
81 
82  auto withQueueNode = node["with_queue"];
83  if (withQueueNode) {
84  config.withQueue = withQueueNode.as<std::string>();
85  }
86 
87  if (node["custom_lib_path"]) {
88  config.customLibPath = node["custom_lib_path"].as<std::string>();
90  !config.customLibPath.empty(), ErrCode::kConfig, "custom_lib_path not found in config");
91  }
92  if (node["custom_create_function"]) {
93  config.customCreateFunction = node["custom_create_function"].as<std::string>();
95  !config.customCreateFunction.empty(), ErrCode::kConfig,
96  "custom_create_function not found in config");
97  }
98  //
99  auto bodyNode = node["config_body"];
100  if (bodyNode) {
101  // some components(e.g. lidarinfer) need how to figure out file path from config_path
102  if (!bodyNode["config_path"] && !path.empty()) {
103  bodyNode["config_path"] = path;
104  }
105  YAML::Emitter body;
106  body << bodyNode;
108  body.good(), ErrCode::kConfig,
109  "config_body error in config, yaml error: " + body.GetLastError());
110  config.configBody = body.c_str();
111  }
112 
113  YAML::Emitter yRawContent;
114  yRawContent << node;
115  config.rawContent = yRawContent.c_str();
116  return ErrCode::kGood;
117 }
118 
119 inline ErrCode
121  const std::string& yamlDoc, const std::string& path, std::vector<ComponentConfig>& all)
122 {
123  auto nodes = YAML::LoadAll(yamlDoc);
124  for (const auto& doc : nodes) {
125  if (!doc) {
126  continue;
127  }
128  YAML::Emitter content;
129  content << doc;
131  content.good(), ErrCode::kConfig,
132  "component error in config, yaml error: " + content.GetLastError());
133  ComponentConfig config;
135  parseComponentConfig(content.c_str(), path, config),
136  "parsing a component failed in config");
137  all.push_back(config);
138  }
139  return ErrCode::kGood;
140 }
141 
142 }} // namespace ds3d::config
143 
144 #endif // DS3D_COMMON_HPP_YAML_CONFIG_HPP
ds3d::config::ComponentConfig::type
ComponentType type
Definition: includes/ds3d/common/config.h:36
ds3d::config::ComponentType::kNone
@ kNone
DS3D_CATCH_ANY
#define DS3D_CATCH_ANY(errcode, fmt,...)
Definition: defines.h:117
type_trait.h
ds3d::Exception
Definition: common.h:138
ds3d::ErrCode::kGood
@ kGood
ds3d::config::ComponentConfig::gstInCaps
std::string gstInCaps
Definition: includes/ds3d/common/config.h:37
ds3d::config::parseComponentConfig
ErrCode parseComponentConfig(const std::string &yamlComp, const std::string &path, ComponentConfig &config)
Definition: yaml_config.hpp:53
ds3d::ErrCode
ErrCode
Definition: common.h:43
ds3d::config::ComponentConfig::withQueue
std::string withQueue
Definition: includes/ds3d/common/config.h:41
DS3D_CATCH_ERROR
#define DS3D_CATCH_ERROR(type, errcode, fmt,...)
Definition: defines.h:110
config.h
DS3D_ERROR_RETURN
#define DS3D_ERROR_RETURN(code, fmt,...)
Definition: defines.h:72
ds3d::config::ComponentConfig::linkFrom
std::string linkFrom
Definition: includes/ds3d/common/config.h:40
ds3d::config::CatchYamlCall
ErrCode CatchYamlCall(std::function< ErrCode()> f)
Definition: yaml_config.hpp:28
ds3d::ErrCode::kConfig
@ kConfig
DS3D_THROW_ERROR
#define DS3D_THROW_ERROR(statement, code, msg)
Definition: defines.h:78
ds3d::config::ComponentConfig::configBody
std::string configBody
Definition: includes/ds3d/common/config.h:44
common.h
ds3d::config::CatchConfigCall
ErrCode CatchConfigCall(F f, Args &&... args)
Definition: yaml_config.hpp:41
ds3d::config::ComponentConfig::filePath
std::string filePath
Definition: includes/ds3d/common/config.h:48
ds3d::config::ComponentConfig::customCreateFunction
std::string customCreateFunction
Definition: includes/ds3d/common/config.h:43
ds3d::config::ComponentConfig::name
std::string name
Definition: includes/ds3d/common/config.h:35
ds3d::config::ComponentConfig::rawContent
std::string rawContent
Definition: includes/ds3d/common/config.h:47
idatatype.h
ds3d::config::ComponentConfig
Definition: includes/ds3d/common/config.h:34
ds3d::config::ComponentConfig::gstOutCaps
std::string gstOutCaps
Definition: includes/ds3d/common/config.h:38
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
ds3d::config::componentType
ComponentType componentType(const std::string &strType)
Definition: includes/ds3d/common/config.h:52
ds3d
Definition: lidar_3d_datatype.h:33
ds3d::config::parseFullConfig
ErrCode parseFullConfig(const std::string &yamlDoc, const std::string &path, std::vector< ComponentConfig > &all)
Definition: yaml_config.hpp:120
DS3D_TRY
#define DS3D_TRY
Definition: defines.h:108
ds3d::config::ComponentConfig::linkTo
std::string linkTo
Definition: includes/ds3d/common/config.h:39
ds3d::config::ComponentConfig::customLibPath
std::string customLibPath
Definition: includes/ds3d/common/config.h:42