本文是最后一篇C/C++与Lua交互的教程,在此之后,我们会结合Cocos2D-X来介绍Lua绑定。本文主要介绍如何绑定一个简单的C++类到Lua里面,并且提供Lua的面向对象访问方式。
绑定C++类
定义C++类
首先,我们定义一个Student类,它拥有名字(字符串类型)和年龄(整型),并且提供一些getter和setter,最后还提供了一个print方法.这里有Student类的定义和实现:Student.h和Student.cpp
编写绑定代码
首先,让我们编写在Lua里面创建Student对象的方法:
Student **s = (Student**)lua_newuserdata(L, sizeof(Student*)); // lua will manage Student** pointer
*s = new Student; //这里我们分配了内存,后面我们会介绍怎么让Lua在gc的时候释放这块内存
</div>
接下来是getName,setName,setAge,getAge和print方法的定义:
int l_setName(lua_State* L)
{
Student **s = (Student**)lua_touserdata(L, 1);
luaL_argcheck(L, s != NULL, 1, "invalid user data");
luaL_checktype(L, -1, LUA_TSTRING);
std::string name = lua_tostring(L, -1);
(*s)->setName(name);
return 0;
}
int l_setAge(lua_State* L)
{
Student **s = (Student**)lua_touserdata(L,1);
luaL_argcheck(L, s != NULL, 1, "invalid user data");
luaL_checktype(L, -1, LUA_TNUMBER);
int age = lua_tonumber(L, -1);
(*s)->setAge(age);
return 0;
}
int l_getName(lua_State* L)
{
Student **s = (Student**)lua_touserdata(L,1);
luaL_argcheck(L, s != NULL, 1, "invalid user data");
lua_settop(L, 0);
lua_pushstring(L, (*s)->getName().c_str());
return 1;
}
int l_getAge(lua_State* L)
{
Student **s = (Student**)lua_touserdata(L,1);
luaL_argcheck(L, s != NULL, 1, "invalid user data");
lua_settop(L, 0);
lua_pushnumber(L, (*s)->getAge());
return 1;
}
int l_print(lua_State* L)
{
Student **s = (Student**)lua_touserdata(L,1);
luaL_argcheck(L, s != NULL, 1, "invalid user data");
(*s)->print();
return 0;
}
</div>
从这里我们可以看到,userdata充当了C++类和Lua的一个桥梁,另外,我们在从Lua栈里面取出数据的时候,一定要记得检查数据类型是否合法。
注册C API到Lua里面
最后,我们需要把刚刚编写的这些函数注册到Lua虚拟机里面去。
static const struct luaL_Reg stuentlib_f [] = {
{"create", newStudent},
{"setName",l_setName},
{"setAge", l_setAge},
{"print", l_print},
{"getName",l_getName},
{"getAge", l_getAge},
{NULL, NULL}
};
int luaopen_student (lua_State *L) {
luaL_newlib(L, stuentlib_f);
return 1;
}
</div>
现在,我们把luaopen_student函数添加到之前的注册函数里面去:
static const luaL_Reg lualibs[] =
{
{"base", luaopen_base},
{"io", luaopen_io},
{"cc",luaopen_student},
{NULL, NULL}
};
const luaL_Reg *lib = lualibs;
for(; lib->func != NULL; lib++)
{
//注意这里如果使用的不是requiref,则需要手动在Lua里面调用require "模块名"
luaL_requiref(L, lib->name, lib->func, 1);
lua_settop(L, 0);
}</div>
Lua访问C++类
现在,我们在Lua里面操作这个Student类。注意,我们绑定的每一个函数都需要一个student对象作为参数,这样使用有一点不太方便。
local s = cc.create()
cc.setName(s,"zilongshanren")
print(cc.getName(s))
cc.setAge(s,20)
print(cc.getAge(s))
cc.print(s)
</div>
最后,输出的结果为:
zilongshanren
20
My name is: zilongshanren, and my age is 20
</div>
提供Lua面向对象操作API
现在我们已经可以在Lua里面创建C++类的对象了,但是,我们最好是希望可以用Lua里面的面向对象的方式来访问。
local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()</div>
而我们知道s:setName(xx)就等价于s.setName(s,xx),此时我们只需要给s提供一个metatable,并且给这个metatable设置一个key为”__index”,value等于它本身的metatable。最后,只需要把之前Student类的一些方法添加到这个metatable里面就可以了。
MetaTable
我们可以在Registry里面创建这个metatable,然后给它取个名字做为索引,注意,为了避免名字冲突,所以这个名字一定要是独一无二的。
//创建名字为tname的metatable并放在当前栈顶,同时把它与Registry的一个key为tname的项关联到一起
int luaL_newmetatable (lua_State *L, const char *tname);
//从当前栈顶获取名字为tname的metatable
void luaL_getmetatable (lua_State *L, const char *tname);
//把当前栈index处的userdata取出来,同时检查此userdata是否包含名字为tname的metatable
void *luaL_checkudata (lua_State *L, int index,const char *tname);
</div>
接下来,我们要利用这3个C API来为我们的student userdata关联一个metatable.
修改绑定代码
首先,我们需要创建一个新的metata

