lua-intf的小技巧

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值