有没有用于C++的Lua表迭代器包装器?

我希望能够从 Lua 代码中调用一些 C++ 函数,同时在参数中传递 Lua 表,就像这样:

call_cpp_function_from_lua({ x = 10, y = 20 })

call_cpp_function_from_lua 中,我希望能够获取并使用 C++ 表示 Lua 表的迭代器,就像这样:

std::map<boost::variant<LuaTypesList>, boost::variant<LuaTypesList>>::iterator it = getLuaTableBegin();

我可以使用 C API 来完成这个过程,但是这样很繁琐,容易出错,参见 Iterating through a Lua table from C++?

点赞
用户72178
用户72178

QtLua库实现了Lua表格的C ++迭代器。 它有Value :: iteratorValue :: const_iterator类,允许迭代Lua表格。 这是一个简短的示例,介绍如何使用它们:

// 来源于examples/cpp/value/iterate.cc:32的代码

QtLua :: State状态;

// 新的lua表格值
state.exec_statements(“table = {a = 1,b = 2,c = 3}”);

QtLua :: Value表= state [“table”];

// 从C ++代码迭代lua表
for(QtLua :: Value :: const_iterator i = table.begin(); i!= table.end(); i ++)
  qDebug()<< i.key().to_string_p()
           << i.value().to_string_p();
2013-03-10 10:57:33