传Lua对象到Cpp
2016-07-19 16:04
453 查看
传Lua对象到Cpp
(金庆的专栏)
摘自:http://raycast.net/lua-intf
以下代码演示了Lua函数和表传入Cpp进行处理:
(金庆的专栏)
摘自:http://raycast.net/lua-intf
以下代码演示了Lua函数和表传入Cpp进行处理:
std::string acceptStuff(LuaRef luaObj, const std::vector<std::string>& stringVector, std::map<std::string, int>& dict) { // Assume that this function expects Lua object (table) as first argument auto func = luaObj.get<std::function<std::string(int)>>("func"); auto stringField = luaObj.get<std::string>("str"); std::ostringstream s; s << "func() result: " << func(10) << ", string field value: " << stringField << "\n"; s << "Vector size: " << stringVector.size() << ", first element: " << stringVector[0] << "\n"; s << "Dictionary size: " << dict.size() << ", first element: (" << dict.begin()->first << ", " << dict.begin()->second << ")"; return s.str(); } LuaBinding(lua).beginModule("test") .addFunction("acceptStuff", &acceptStuff) .endModule();
// Lua local obj = { func = function(i) return "You passed number " .. i end, str = "Hello, world" } local v = { 1, 2, 3 } local dict = { first = 1, second = 2 } print(test.acceptStuff(obj, v, dict))
// Output func() result: You passed number 10, string field value: Hello, world Vector size: 3, first element: 1 Dictionary size: 2, first element: (first, 1)
相关文章推荐
- 传Lua对象到Cpp
- 《Lua程序设计》第6章 深入函数 学习笔记
- Lua 正确的尾调用(proper tail call)
- Lua中的closure(闭合函数)
- premake构造工具- 使用premake工具生成工程文件
- Lua中强大的元方法__index详解
- 一个lua 与c交互的小demo
- 如何让EditPlus支持LUA
- 高效调用lua函数
- Lua学习笔记-table转为字符串
- 余额重估(Revaluation Balances)相关设置和操作
- Lua调用C/C++函数
- Lua实现angle角
- Lua5.3——C API函数
- 《Lua程序设计》第5章 函数 学习笔记
- 《Lua程序设计》第4章 语句 学习笔记
- Lua中的控制结构
- LUA基础
- 《Lua程序设计》第3章 表达式 学习笔记
- Lua中的table构造式(table constructor)