gpt4 book ai didi

c++ - yaml-cpp 甚至为 const 节点修改底层容器?

转载 作者:行者123 更新时间:2023-11-30 05:14:04 25 4
gpt4 key购买 nike

我有一个 test.yml 文件,

test1:
test1_file: 'test.yml'

我想用 C++ 代码加载这个 yaml 文件并从中检索数据。

对于我的用例,还有一些额外的文件必须合并到数据中。我找到了 here 的答案(我认为...)。所以,yaml-cpp这看起来很漂亮。坦率地说,它的界面看起来有点奇怪,但我真的不想去重新发明轮子。那const YAML::Node & cnode(const YAML::Node & node) { return node;} 相当代码味道。

好的,所以我有一些尝试导航到给定节点的代码...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <yaml-cpp/yaml.h>

using node_name = std::string;
using node_path = std::vector<node_name>;

const YAML::Node & constify(const YAML::Node & node) {
return node;
}

YAML::Node navigate(const YAML::Node & root_node, const node_path & path) {

// no path elements?
if ( path.empty() ) {
return root_node;
}

// any elements are empty?
if ( std::any_of(path.begin(), path.end(), [](const auto & part){return part.empty();}) ) {
throw std::invalid_argument{"navigate to node_path with empty elements"};
}

// set up initial root node info
YAML::Node current = root_node;
const node_name * parent_node_name = nullptr;

auto throw_path_not_found = [&](const node_name & element_name) {
node_path not_found_node_path;
if ( parent_node_name ) {
// parent_node_name points to the last processed parent
// if we pass it as-is as an end-iterator, then it will
// not be included in the container. So increment it.
//
// Then, we're at the current node name (which wasn't found)
// so increment it once more to have the full path.
parent_node_name += 2;

not_found_node_path = {&*path.begin(), parent_node_name};
} else {
not_found_node_path = {path.begin(), path.begin() + 1};
}

// throw yaml_path_not_found{not_found_node_path, current, element_name};
std::string err_msg{"path not found: "};
std::for_each(not_found_node_path.begin(), not_found_node_path.end(), [&](const node_name & n){err_msg += n + ".";});
throw std::runtime_error{std::move(err_msg)};
};

// query node to see if we can continue
auto query_node_type = [&](const node_name & element_name){
switch (current.Type()) {
case YAML::NodeType::Scalar:
// Reached end of node chain before reaching end of desired node path?
if ( &element_name != &path.back() ) {
throw_path_not_found(element_name);
}
return;
case YAML::NodeType::Sequence: // aka array
// this can be fine if the next node element is an integer to access the array
// otherwise we'll get an Undefined node on the next iteration.
return;
case YAML::NodeType::Map:
// this can be fine if the next node element is a key into the map
// otherwise we'll get an Undefined node on the next iteration.
return;
case YAML::NodeType::Null:
// the node path exists but contains no value ???
// more like a std::set, I think?
// if this causes issues, then fix it.
return;
case YAML::NodeType::Undefined:
throw_path_not_found(element_name);

// no-default:
// allow compiler to warn on changes to enum
}

throw std::logic_error{std::string{"unexpected node type "} + std::to_string(current.Type()) + " returned from yaml-cpp"};
};

// loop through path elements querying to see if we've prematurely stopped
for ( const auto & element : path ) {
current = current[element];
query_node_type(element);
parent_node_name = &element;
}

return current;
}

node_path split_node_path(const std::string & path) {
node_path result;

result.emplace_back();

// prod code just uses boost::algorithm::string::split
for ( char c : path ) {
if ( '.' == c ) {
result.emplace_back();
continue;
}
result.back().push_back(c);
}

return result;
}

我的想法是我应该能够提供一个节点路径,例如"test1.test1_file"它应该为此检索节点。但是,我注意到第一次这样做效果很好,但第二次最终会抛出错误,因为找不到节点。等等,什么?

是的,好的:

void dump(const YAML::Node & node) {
std::cout << "...DUMP...\n" << YAML::Dump(node) << std::endl;
}

int main(int argc, char **argv) {
if ( 3 != argc ) {
std::cerr << "Usage: ./a.out test.yml test1.test1.file\n";
return EXIT_FAILURE;
}

try {
YAML::Node root_node = YAML::LoadFile(argv[1]);

dump(root_node);

navigate(root_node, split_node_path(argv[2]));

dump(root_node);

navigate(root_node, split_node_path(argv[2]));

} catch (const std::exception & e) {
std::cerr << "exception: " << e.what() << '\n';
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

构建并执行它 g++ test.cpp -lyaml-cpp -std=c++17g++ (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901成功。然而,调用它会产生意想不到的输出:


$ ./a.out test.yml test1.test1_file
...DUMP...
test1:
test1_file: test.yml
...DUMP...
test1_file: test.yml
exception: path not found: test1.

我完全希望转储是相同的(并且不会抛出异常):navigate()接受 const YAML::Node & .这告诉我它不应该修改根节点。那么具体是在哪里修改的呢?更重要的是,我做错了什么?

我怀疑这与需要 cnode() 的其他答案有关构造函数 YAML::Node秒。但是当我尝试做同样的事情时,它似乎没有帮助(正如这个最小示例中未使用的 constify() 函数所证明的那样)。

最佳答案

YAML::Node是引用类型,不是值类型。这意味着 const YAML::Node&有点误导;这就像在说 const unique_ptr<T>& .可以修改底层值T .

此外,有一些 YAML API 在像这样的循环中有点困惑。

YAML::Node current = ...;
for ( const auto & element : path ) {
// this actually is a mutating call; it identifies the root node
// with the sub-node
current = current[element];
}

关于c++ - yaml-cpp 甚至为 const 节点修改底层容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com