问题
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}:
- id == 0 => path = {0}
- parent != NULL => parent.id == 1 => path = {1, 0}
- parent != NULL => parent.id == 0 => path = {0, 1, 0}
- 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