lua-intf的小技巧

来源:互联网 发布:深圳市一号仓佳速网络 编辑:程序博客网 时间:2024/06/16 11:39
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();// Lualocal 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))// Outputfunc() result: You passed number 10, string field value: Hello, worldVector size: 3, first element: 1Dictionary size: 2, first element: (first, 1)
0 0