C++ yaml cpp甚至为常量节点修改基础容器?

C++ yaml cpp甚至为常量节点修改基础容器?,c++,c++17,yaml-cpp,C++,C++17,Yaml Cpp,我有一个test.yml文件 test1: test1_file: 'test.yml' 我想用C++代码加载这个YAML文件并从中检索数据。 对于我的用例,有一些附加文件必须合并到数据中。我找到了答案(我想…)。所以,这看起来很漂亮。坦白地说,它的界面看起来有点奇怪,但我真的不想去重新发明轮子。那constyaml::Node&cnode(constyaml::Node&Node){return Node;}真是一种代码味道 好的,我有一些代码试图导航到一个给定的节点 #include

我有一个test.yml文件

test1:
  test1_file: 'test.yml'
我想用C++代码加载这个YAML文件并从中检索数据。 对于我的用例,有一些附加文件必须合并到数据中。我找到了答案(我想…)。所以,这看起来很漂亮。坦白地说,它的界面看起来有点奇怪,但我真的不想去重新发明轮子。那
constyaml::Node&cnode(constyaml::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;
}
g++(Ubuntu 6.2.0-3ubuntu11~16.04)6.2.0 20160901构建并执行它成功。调用它会产生意外的输出,但是:


$./a.out test.yml test1.test1\u文件
倾倒
测试1:
test1_文件:test.yml
倾倒
test1_文件:test.yml
异常:未找到路径:test1。

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


我怀疑这与另一个答案有关,它需要
cnode()
函数来构造
YAML::Node
s。但是,当我尝试做同样的事情时,它似乎没有什么帮助(在这个最小的示例中没有使用的
constify()
函数就证明了这一点)。

YAML::Node
是引用类型,而不是值类型。这意味着
constyaml::Node&
有点误导;这就像说
const unique\u ptr&
。您可以修改基础值
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];
}

那么,如何在不使用递归或破坏树的情况下进行遍历呢?必须使用递归。。。或者添加一层间接寻址,例如,带有空删除器的unique_ptr。
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];
}