Я думаю, что вы должны рекурсивно пройти по дереву property_tree.
Вы можете определить функцию, которая рекурсивно повторяется на каждом узле и вызывает метод для каждого узла:
template<typename T>
void traverse_recursive(const boost::property_tree::ptree &parent, const boost::property_tree::ptree::path_type &childPath, const boost::property_tree::ptree &child, T &method)
{
using boost::property_tree::ptree;
method(parent, childPath, child);
for(ptree::const_iterator it=child.begin();it!=child.end();++it) {
ptree::path_type curPath = childPath / ptree::path_type(it->first);
traverse_recursive(parent, curPath, it->second, method);
}
}
Мы можем определить более простую функцию для вызова предыдущей:
template<typename T>
void traverse(const boost::property_tree::ptree &parent, T &method)
{
traverse_recursive(parent, "", parent, method);
}
Теперь вы можете изменить класс A, чтобы добавить один метод для объединения только одного узла и заполнить метод update_ptree:
#include <boost/bind.hpp>
class A {
ptree pt_;
public:
void set_ptree(const ptree &pt) {
pt_ = pt;
}
void update_ptree(const ptree &pt) {
using namespace boost;
traverse(pt, bind(&A::merge, this, _1, _2, _3));
}
ptree get_ptree() {
return pt_;
}
protected:
void merge(const ptree &parent, const ptree::path_type &childPath, const ptree &child) {
pt_.put(childPath, child.data());
}
};
Единственное ограничение заключается в том, что возможноиметь несколько узлов с одинаковым путем.Будет использован каждый из них, но будет объединен только последний.