Pergunta

I want to use insensively yaml-cpp in a C++ project, as it fits my needs perfectly. But I want to update one node from another node, i.e. properly add non-existant nodes from one mode to another, or replace existing values of existing ones. I can't find how to do this simply with the current interface...

So I try to do this with a simple loop on an iterator. I figure out that the following thing does not work while traversing a map node:

if (node_1[it->first]) /*...*/

It does not find any node! So, for map nodes with scalars as keys, the test if (node_1[it->first.Scalar()]) /*...*/ works well. My problem is to do the same with sequence keys. How can I do that?

EDIT

Here is an example of YAML document:

---
#Fake entry
Time:   0.1.1.2
ID: 25814
Emitter: Me
Line : {
    orig: 314,
    st: 512
  }
Message : |
    This is a fake error

#More difficult
[0,1,2,3] : my_address
[5, 6, 7, 8] : an_address
...

This document is loaded without any problem into a Node, say doc1; I want now modify some entries with respect to another YAML document, such as:

---
Comment: what a dummy file!
Emitter: You
[0,1,2,3] : address changed
...

So I load this second document into a Node doc2, and I want to update doc1 with the nodes of doc 2. The first key of doc 2 is not present in doc 1 and is a scalar, so I can do doc1[it->first.Scalar()] = it->second. The second key is present, so the same instruction will update doc1, replacing the value linked with key Emitter. My problem is that I cannot succeed in finding the 3rd key inside doc1, as it is a sequence.

Foi útil?

Solução

yaml-cpp doesn't offer generic equality testing for nodes, which is why your initial solution (which would have the best bet of working) didn't work.

Instead, yaml-cpp relies on typed equality testing. E.g., node[5] will convert all keys to integers to check key equality; it won't convert 5 to a node and then check equality that way. This is why your other solution will usually work - most of your keys are simple scalars, so they can match using std::string equality.

It appears you really want to "merge" two nodes; this has been on the yaml-cpp issues list for a while: https://code.google.com/p/yaml-cpp/issues/detail?id=41, and there's some discussion there which explains why it's a hard problem.

As a possible workaround, if you know the type of each node, you can explicitly cast before doing your comparison, e.g.:

doc1[it->first.as<T>()] = it->second;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top