Getting boost property_tree parent node

人盡茶涼 提交于 2019-12-02 04:06:57

问题


I am using boost property_tree in my program. I've set up the tree for using custom path type. What I'm looking for is getting a specific node's parent node id.

Here is an example:

MetaStorageTree tree;

typedef boost::property_tree::basic_ptree<Framework::CommonClientServer::InterfacePathChain_t, MetaStorageTreeNode*>
    MetaStorageTreeNode_t;
class MetaStorageTree : public MetaStorageTreeNode_t;

MetaStorageTreeNode* node = new MetaStorageTreeNode(1);
MetaStorageTreeNode* node1 = new MetaStorageTreeNode(2);
tree.put(InterfacePathChain_t{0}, node);
tree.put(InterfacePathChain_t{0, 0}, node1);
tree.put(InterfacePathChain_t{0, 1}, node1);
tree.put(InterfacePathChain_t{0, 0, 0}, node);
tree.put(InterfacePathChain_t{0, 1, 0}, node1);
tree.put(InterfacePathChain_t{0, 1, 1}, node);

//InterfacePathChain_t is basically a vector<int>

And the result goes as expected:

{0}: 1
    {0}: 2
        {0}: 1
    {1}: 2
        {0}: 2
        {1}: 1

What I need is a way of getting full id of a node without permanently storing it. What I am thinking of is a way of simply getting its parent node id and pushing it to the front of the path and so on to the top level. But I didn't seem to be able to find a way to do this in property_tree. Is this possible? If no, are there any other ways of calculating full path for this case?

For example as for node with path {0, 1, 0}:

  1. id == 0 => path = {0}
  2. parent != NULL => parent.id == 1 => path = {1, 0}
  3. parent != NULL => parent.id == 0 => path = {0, 1, 0}
  4. parent == NULL => end

回答1:


You can't.

Boost Ptree nodes are self-contained and do not know about any containing datastructure (it's the the "tree" equivalent of a singly-linked list).

As a best approximation you could look up the child inside the parent, e.g. with something like in C++: boost ptree relative key.

This presumes you always have "a root" available to search in.



来源:https://stackoverflow.com/questions/45366768/getting-boost-property-tree-parent-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!