我需要在C++中解析一些YAML (对YAML来说有点新)。我想使用yaml-cpp。我的目标是:
如果我以YAML为例,它可能看起来如下:
...
comms:
messageBus:
remoteHost: "message-bus"
remotePort: 5672
甚至:
...
comms:
不过,我希望能够更新反映默认值的YAML。因此,要阅读上述YAML,代码如下所示:
auto &nodeComms = get_yaml_node_field_or_insert(node, "comms");
auto &nodeMessageBus = get_yaml_node_field_or_insert(nodeComms , "comms");
auto strRemoteHost = get_yaml_string_field_or_default_and_update(nodeMessageBus, "remoteHost", "127.0.0.1");
auto nRemotePort = get_yaml_uint16_field_or_default_and_update(nodeMessageBus, "remotePort", uint16_t{5672});
因此,如果在第二个示例上运行上述代码,更新的YAML现在将是:
...
comms:
messageBus:
remoteHost: "127.0.0.1"
remotePort: 5672
对于get_yaml_string_field_or_default_and_update
和get_yaml_uint16_field_or_default_and_update
来说,创建一个模板化函数来处理读取不同值类型并在必要时插入它们是非常简单的:
///////////////////////////////////////////////////////////////////////////
template <typename TYPE_IN, typename TYPE_RETURN>
TYPE_RETURN get_type_from_yaml_node_or_default_and_update(YAML::Node &node,
const char *pchFieldName,
TYPE_IN nValueDefault) noexcept
{
if (!node)
{
assert(false);
throw std::runtime_error("Invalid node");
}
if (!node[pchFieldName])
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
try
{
return node[pchFieldName].as<TYPE_RETURN>();
}
catch (const std::exception &e)
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
catch (...)
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
}
我正在挣扎的代码是get_yaml_node_field_or_insert
///////////////////////////////////////////////////////////////////////////
YAML::Node &get_yaml_node_field_or_insert(YAML::Node &node, const char *pchFieldName) noexcept
{
if (!node)
{
assert(false);
throw std::runtime_error("Invalid node");
}
// TODO: Either 1) Return reference if it exists or 2) insert if it does not and return reference.
return node[pchFieldName];
}
看起来,operator[]似乎没有根据API返回引用。
// indexing
template <typename Key>
const Node operator[](const Key& key) const;
template <typename Key>
Node operator[](const Key& key);
如有任何建议或建议,将不胜感激。
发布于 2020-01-31 23:04:24
节点已经是一个引用类型了,所以从函数中返回它不会产生很深的副本。它也是可变的,所以您只需编辑它,更改就会更新根节点。
https://stackoverflow.com/questions/60012219
复制相似问题