majintao0131 / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

There should be a way to reset a Node "reference" in the new API. #189

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If you're iterating down a node path, you might want to reset a reference. E.g.:

YAML::Node node = /* something */;
for_each(path.begin(), path.end(), [&node](const std::string& key) {
   node.reset(node[key]);
});

or maybe node.set? I was thinking node.reset to mirror a smart pointer, but 
maybe set would be more natural.

Original issue reported on code.google.com by jbe...@gmail.com on 31 Jan 2013 at 6:44

GoogleCodeExporter commented 9 years ago
Hi, I have encountered the same problem today. I am using your yaml-cpp 0.5 new 
API, and want to iterate down in a map node to get existing nodes or insert new 
nodes, but the referencing will cause it to change the root node itself.
Are there any workarounds of this? Or where I can modify a bit in your source 
code(even some kind of 'dirty fix') to add this feature?
Thanks in advance!

Original comment by chk0...@gmail.com on 26 Mar 2013 at 5:01

GoogleCodeExporter commented 9 years ago
It shouldn't be hard - just reset the shared memory and node delegate in 
YAML::Node.

Original comment by jbe...@gmail.com on 28 Mar 2013 at 3:41

GoogleCodeExporter commented 9 years ago
Thanks for your reply, but how can I do that? Currently I am using something 
like:

template<typename T>
void setVal(const vector<string>& path, YAML::Node& root_node, T val)
{
YAML::Node node = root_node;
// suppose all child nodes exists
for(itr = path.begin(); itr != path.end() ++itr)
    node = root_node[path];
node = val;
}

But this will destroy root_node, after this function call it will be changed to 
the leaf node and I cannot recover the reference to the original root node.

Original comment by chk0...@gmail.com on 28 Mar 2013 at 9:25

GoogleCodeExporter commented 9 years ago
Sorry, I meant, you would add a reset() method to YAML::Node.

Original comment by jbe...@gmail.com on 28 Mar 2013 at 2:10

GoogleCodeExporter commented 9 years ago
Fixed, 
https://code.google.com/p/yaml-cpp/source/detail?r=476cbbebe57bb1808b947c03337bd
643dcbe2db2&repo=new-api

(Implemented Node::reset)

Original comment by jbe...@gmail.com on 3 Apr 2013 at 1:50

GoogleCodeExporter commented 9 years ago
Great news for me! I am trying it out! Thanks very much!

Original comment by chk0...@gmail.com on 3 Apr 2013 at 2:08