lua - c++封装 dxlua

 自己封装了一套lua的c++封装库(5.1),优点是上手极容易,只有头文件,封装了大部分的API,而且效率损失很小。可在vc8下编译通过。
以下为摘要:

加载脚本:

Dx::Lua::Domain d;
d.DoFile("Test.lua");

索引:
使用[]
d["print"];
d["table"]["foreach"];

提取变量:

string s;
d["the_string"]>>s;

设置变量:
string s;
d["the_string"] = s;
创建一个全局表t,并插入3个值:
d["t"] = TABLE;//优化形式为d["t"] = NewTable(3);
d["t"][1] = 1,d["t"][2] = 2,d["t"][3] = 3;

引用:
Reference print(d["print"]);
d["tbl"] = TABLE;
Reference tbl(d["tbl"]);

调用函数:
print("hello");
print("number ", 3.14);
调用函数返回值:
string res;
fun(arg1,arg2)>>res;

获取多返回值:

string res1,res2;
lvals res = {res1,res2};
fun(arg1,arg2)>>lvals;

C++到Lua的类型映射:

在任意.cpp中进行映射,不需要修改被映射的类:
C.h

class C
{
//...
void f();
};

C2lua.cpp
BEGIN_CLASS2LUA(C)
LUA_METHOD(f),
END_CLASS2LUA

main.cpp
...
Domain d;
C c;
d["inst"] = c;
Reference inst(d["inst"]);
(d->*"f")();// call c.f();

遍历表:

    for(Range rg(d["_G"]);rg;++rg)
      if((*rg).Is(TABLE))//使用*获得值的引用
      {
        string k;
        rg>>k;
        cout<<"found a table : "<<k<<endl;
      }

错误处理:
定义了若干异常如RangeError,LoadError,LuaBadCast等

download

http://free5.ys168.com/?qslash

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值