简体   繁体   中英

Getting boost property_tree parent node

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

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, eg with something like in C++: boost ptree relative key .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM