class FileResolver 读lua参数文件程序接口
参考cartographer_common_configuration_file_resolver.md
**class LuaParameterDictionary 获取lua参数表 **
说明: 读取 lua参数 相关文件,形成lua 参数表,实现过程用到cpp调用lua 知识,比较繁杂,有兴趣的读者可以参考: cpp_call_lua.md 文件。 或参考链接:https://www.cnblogs.com/chevin/p/5884657.html 系统学习。
当然,也可以忽略该部分具体lua相关知识,仅仅了解LuaParameterDictionary 类中各函数用法。为达到此目标,可以查看LuaParameterDictionary test文件或以下简单说明。
构造函数:
LuaParameterDictionary(const std::string& code,std::unique_ptr<FileResolver> file_resolver);
code: 需要包含的文件夹:
FileResolver: 参数文件路径+文件名
举例: code = "text(
include "map_builder.lua"
MAP_BUILDER.use_trajectory_builder_2d = true
return MAP_BUILDER)text";
file_resolver =
::absl::make_unique< ::cartographer::common::ConfigurationFileResolver>(
std::vector<std::string>{
std::string(::cartographer::common::kSourceDirectory) +
"/configuration_files"});
LuaParameterDictionary 主要函数用法如下:
code="return {
a= 100,
b= "john",
c= 3.14,
d= true,
e= { f= 100, g = 200},
h= 300
}"
LuaParameterDictionary dict(code,make_unique<FileResolver>())
LuaParameterDictionary sub_dict = dict.GetDictionary("e")
auto a = dict.GetInt("a"); //a = 100
auto b = dict.GetString("b"); //b = "john"
auto c = dict.GetDouble("c"); //c = 3.14
auto d = dict.GetBool("d"); //d = true
std::vector<std::string> keys = dict.GetKeys(); //keys = {"f","g"}
string str = dict.ToString();
/*str = "{ a= 100, b= "john", c= 3.14, d= true, e= { f= 100, g = 200}, h= 300 }" */
code = "return { 1., 2., 3. }"
LuaParameterDictionary dict(code,make_unique<FileResolver>())
vector<double> values = dict.GetArrayValuesAsDoubles();
// values = { 1., 2., 3. }
code = "return { { a = 1 }, { b= choose(false, 1, 0) } }"
vector<std::unique_ptr<LuaParameterDictionary>> values =
dict.GetArrayValuesAsDictionaries();
values[0]->GetInt("a")// == 1
values[0]->GetInt("b")// == 1