传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的更多相关文章

  1. intent传值传对象跳转

    intent传值传对象跳转 1.传值 //原activity中存入一个字段 intent = new Intent(From.this, To.class); intent.putExtra(&quo ...

  2. @requestbody---接受前端传json对象并绑定javabean

    @requestbody---接受前端传json对象并绑定javabean----https://blog.csdn.net/ljxbbss/article/details/74452326 最近代码 ...

  3. xlua中lua对象到c#对象的转型

    lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...

  4. IDEA同步上传lua代码,方便开发。

    因项目是Java和lua一起开发的,以前用Notepad++插件连接,每次关掉得重新寻找目录.有点耗时间,所以用idea提供的工具很是便利,再此做个笔记. 点击上面的绿色”+“号,添加 在配置mapp ...

  5. PHP关于传众多参数还是传上下文对象的性能测试

    在开发微信公众平台平台的过程中,有这么几个参数总是需要传来传去,$userOpenId,$message,$time. 在整个程序的运行过程中,为了函数方便的处理,将这三个变量一直放在参数列表里.关于 ...

  6. JQuery Ajax 使用FormData上传文件对象

    FormData部分: 先new FormData对象 :let somedata = new FormData(),然后将数据添加进去,这里我们使用append()进行添加. 这里举一个上传头像的例 ...

  7. JAVA之等号、传类对象参数与c++的区别

    在JAVA中用等号对类对象进行赋值,实际上操作的是对象的地址. eg: package MyText; class ClassA { int value; public void seta(int v ...

  8. vue给input file绑定函数获取当前上传的对象

    HTML <input type="file" @change="tirggerFile($event)"> JS(vue-methods) tir ...

  9. LUA对象

    Rectangle = {width = , height = , area = }; function Rectangle:new(o, width, height) o = o or {}; se ...

随机推荐

  1. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  2. [USACO17FEB]Why Did the Cow Cross the Road I S

    题目描述 Farmer John's cows are trying to learn to cross the road effectively. Remembering the old " ...

  3. 【BZOJ2242】【SDOI2011】计算器

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y ...

  4. 【NOIP2013】华容道 广搜+spfa

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

  5. ●BZOJ 1853 [Scoi2010]幸运数字

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题解: 容斥原理,暴力搜索,剪枝(这剪枝剪得真玄学) 首先容易发现,幸运号码不超过 2 ...

  6. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. bzoj1853[Scoi2010]幸运数字 容斥

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 3027  Solved: 1128[Submit][Status ...

  8. Codeforces 666E Forensic Examination

    题意:给定主串s和m个模式串,每次询问[l,r]的模式串中出现s[pl...pr]次数最多的串和次数. 这题挺简单的,先把所有模式串拿来建广义后缀自动机,询问相当于子树众数,用线段树合并即可. 那我为 ...

  9. 数据权限管理中心 - 基于mybatis拦截器实现

    数据权限管理中心 由于公司大部分项目都是使用mybatis,也是使用mybatis的拦截器进行分页处理,所以技术上也直接选择从拦截器入手 需求场景 第一种场景:行级数据处理 原sql: select ...

  10. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...