[英]Is there any Lua table iterator wrapper for C++?
我想从Lua代码中调用一些C ++函数,并在参数中传递Lua表,如下所示:
call_cpp_function_from_lua({ x = 10, y = 20 })
在call_cpp_function_from_lua
我想要为Lua表的C ++表示形式获取并使用迭代器,如下所示:
std::map<boost::variant<LuaTypesList>, boost::variant<LuaTypesList>>::iterator it = getLuaTableBegin();
我可以使用C API来做到这一点,但是它很容易出错,请参阅从C ++遍历Lua表? 。
QtLua库为Lua表实现了C ++迭代器。 它具有Value::iterator
和Value::const_iterator
类,从而允许Lua表的迭代。 这是有关如何使用它们的简短示例:
// code from examples/cpp/value/iterate.cc:32
QtLua::State state;
// New lua table value
state.exec_statements("table = { a = 1, b = 2, c = 3 }");
QtLua::Value table = state["table"];
// Iterate over lua table from C++ code
for (QtLua::Value::const_iterator i = table.begin(); i != table.end(); i++)
qDebug() << i.key().to_string_p()
<< i.value().to_string_p();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.