我在程序中使用 boost property_tree 。我已经设置了使用自定义路径类型的树。我正在寻找的是获取特定节点的父节点 ID。
这是一个例子:
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>
结果正如预期的那样:
{0}: 1
{0}: 2
{0}: 1
{1}: 2
{0}: 2
{1}: 1
我需要的是一种获取节点完整 ID 而不永久存储它的方法。我正在考虑的是一种简单地获取其父节点 id 并将其推到路径前面并依此类推到顶层的方法。但我似乎无法找到在 property_tree 中执行此操作的方法。这可能吗?如果没有,还有其他方法可以计算这种情况下的完整路径吗?
例如对于路径为 {0, 1, 0} 的节点:
- ID == 0 => 路径 = {0}
- 父级 != NULL => 父级.id == 1 => 路径 = {1, 0}
- 父级 != NULL => 父级.id == 0 => 路径 = {0, 1, 0}
- 父 == NULL => 结束