我有一个配置(“/home/work/father.config”)看起来像:
father_config = {
child_config = "/home/work/child.config";
father_int = 1;
};
在 /home/work/child.config 中:
child_config = {
child_int =2;
};
我想要的是从中father_int
得到child_cfg
。
请看代码:
#include <iostream>
#include <libconfig.h++>
using namespace std;
int main() {
libconfig::Config father_cfg;
father_cfg.readFile("./father.cfg");
const libconfig::Setting& father_setting = father_cfg.lookup("father_setting");
const std::string& child_path = father_setting.lookup("child_path");
libconfig::Config child_cfg;
child_cfg.readFile(child_path);
libconfig::Setting& child_setting = child_cfg.lookup("child_setting");
auto & s = child_cfg.getRoot().add("father_int", libconfig::Setting::TypeGroup);// father_setting.lookup("father_int");
s = father_setting; // !!!!!!! this doesn't work
int father_int = child_setting.lookup("father_int"); // the ulimate purpose is to make father int can be found in child_cfg
cout << father_int;
}
我知道如果father_int 是int 类型并且key-father_int 不会改变,我可以使用:
int father_int = father_setting.lookup("father_int");
child_cfg.GetRoot().add("father_int", libconfig::Setting::TypeInt) = father_int;
但我希望这可以更灵活,需要添加libconfig::Setting::TypeGroup
而不是libconfig::Setting::TypeInt
,
你能帮忙吗?