質問

Starting with a string representing a path to a node that I want to retrieve data from, such as "a.b.c". Currently, the code that I am using to transverse the node hierarchy to reach that node looks something like this (may not compile, but you get the idea):

string keyPath("a.b.c");
vector<string> keyPieces(split(keyPath, "."));

const YAML::Node *currNode = &rootNode;
for_each(begin(keyPieces), end(keyPieces), [&](const string &key)
{
    // for simplicity, ignore indexing on a possible scalar node
    // and using a string index on a sequence node
    if ((*currNode)[key])
    {
        currNode = &((*currNode)[key]);
    }
});

// assuming a valid path, currNode should be pointing at the node
// described in the path (i.e. currNode is pointing at the "c" node)

The above code seems to work correctly, but I am wondering if there is a better way to do the node transversal assignment. Using direct node assignment (currNode = currNode[key]), instead of a pointer/address of (currNode = &((*currNode)[key])), seems to end up creating references between nodes instead of transversing from one node to the next.

Is there a 'cleaner' or more idiomatic way to accomplish this?

役に立ちましたか?

解決

There's no way to do that now (this is a use case I hadn't thought of), but it's a good idea. I've filed a bug (http://code.google.com/p/yaml-cpp/issues/detail?id=189).

他のヒント

I was doing a similar thing, and found out that this capability was added sometime ago, called Node::reset()

So,

string keyPath("a.b.c");
vector<string> keyPieces(split(keyPath, "."));

YAML::Node currNode = rootNode;
for_each(begin(keyPieces), end(keyPieces), [&](const string &key)
{
    if (currNode[key])
    {
        currNode.reset(currNode[key]);
    }
});

should work as intended.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top