本文是最后一篇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的時候釋放這塊內存
接下來是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;
}
從這里我們可以看到,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;
}
現在,我們把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);
}
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)
最后,輸出的結果為:
復制代碼 代碼如下:
zilongshanren
20
My name is: zilongshanren, and my age is 20
提供Lua面向對象操作API
現在我們已經可以在Lua里面創建C++類的對象了,但是,我們最好是希望可以用Lua里面的面向對象的方式來訪問。
復制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()
而我們知道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);
接下來,我們要利用這3個C API來為我們的student userdata關聯一個metatable.
修改綁定代碼
首先,我們需要創建一個新的metatable,并把setName/getName/getAge/setAge/print函數設置進去。 下面是一個新的函數列表,一會兒我們要把這些函數全部設置到metatable里面去。
復制代碼 代碼如下:
static const struct luaL_Reg studentlib_m [] = {
{"setName",l_setName},
{"setAge", l_setAge},
{"print", l_print},
{"getName",l_getName},
{"getAge", l_getAge},
{NULL, NULL}
};
接下來,我們創建一個metatable,并且設置metatable.__index = matatable.注意這個cc.Student的元表會被存放到Registry里面。
復制代碼 代碼如下:
int luaopen_student (lua_State *L) {
luaL_newmetatable(L, "cc.Student");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, studentlib_m, 0);
luaL_newlib(L, stuentlib_f);
return 1;
}
最后,我們記得在創建Student的時候把此元表與該userdata關聯起來,代碼如下:
復制代碼 代碼如下:
int newStudent(lua_State * L)
{
Student **s = (Student**)lua_newuserdata(L, sizeof(Student*)); // lua will manage Student** pointer
*s = new Student;
luaL_getmetatable(L, "cc.Student");
lua_setmetatable(L, -2);
return 1;
}
另外,我們在從Lua棧里面取出Student對象的時候,使用的是下面的函數
復制代碼 代碼如下:
Student **s = (Student**)luaL_checkudata(L,1,"cc.Student");
這個luaL_checkudata除了可以把index為1的棧上的元素轉換為userdata外,還可以檢測它是否包含“cc.Student”元表,這樣代碼更加健壯。 例如,我們之前的setName函數可以實現為:
復制代碼 代碼如下:
int l_setName(lua_State * L)
{
Student **s = (Student**)luaL_checkudata(L,1,"cc.Student");
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);
}
這里有Student類的完整的新的綁定代碼.
Lua訪問C++類
現在,我們可以用Lua里面的面向對象方法來訪問C++對象啦。
復制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
print(s:getName())
s:setAge(20)
print(s:getAge())
s:print()
這里輸出的結果為:
復制代碼 代碼如下:
zilongshanren
20
My name is: zilongshanren, and my age is 20
管理C++內存
當Lua對象被gc的時候,會調用一個__gc方法。因此,我們需要給綁定的C++類再添加一個__gc方法。
首先是C++端的實現:
然后,添加注冊函數:
復制代碼 代碼如下:
static const struct luaL_Reg studentlib_m [] = {
{"__tostring",student2string},
{"setName",l_setName},
{"setAge", l_setAge},
{"print", l_print},
{"getName",l_getName},
{"getAge", l_getAge},
{"__gc", auto_gc},
{NULL, NULL}
};
最后,我們在Stendent的構造函數和析構函數里面添加輸出:
復制代碼 代碼如下:
Student::Student()
:name("default")
{
cout<<"Student Contructor called"<<endl;
}
Student::~Student()
{
cout<<"Student Destructor called"<<endl;
}
接下來是Lua代碼:
復制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()
--當一個對象設置為nil,說明沒有其它對應引擎之前cc.create創建出來的對象了,此時lua返回到c程序的時候會調用gc
s = nil
--如果想在Lua里面直接手動gc,可以調用下列函數
--collectgarbage
最后,程序輸出結果如下:
復制代碼 代碼如下:
Student Contructor called
My name is: zilongshanren, and my age is 20
Student Destructor called
總結
本文主要介紹如何使用UserData來綁定C/C++自定義類型到Lua,同時通過引入MetaTable,讓我們可以在Lua里面采用更加簡潔的面向對象寫法來訪問導出來的類。下一篇文章,我們將介紹Cococs2D-X里面的tolua++及其基本使用方法。 PS:附上本文源代碼,注意在LuaCocos2D-X工程里面。