即使对于const节点,yaml-cpp也会修改底层容器?

时间:2017-04-24 20:34:24

标签: c++ c++17 yaml-cpp

我有一个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++17执行g++ (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()函数到const-ify YAML::Node的另一个答案相关。但是当我试图做同样的事情时,它似乎没有帮助(正如constify()函数所证明的那样,在这个最小的例子中没有使用过。)

1 个答案:

答案 0 :(得分:1)

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];
}
相关问题