本文是最后一篇C/C++與Lua交互的教程,在此之后,我們會結(jié)合Cocos2D-X來介紹Lua綁定。本文主要介紹如何綁定一個簡單的C++類到Lua里面,并且提供Lua的面向?qū)ο笤L問方式。
綁定C++類
定義C++類
首先,我們定義一個Student類,它擁有名字(字符串類型)和年齡(整型),并且提供一些getter和setter,最后還提供了一個print方法.這里有Student類的定義和實現(xiàn):Student.h和Student.cpp
編寫綁定代碼
首先,讓我們編寫在Lua里面創(chuàng)建Student對象的方法:
復(fù)制代碼 代碼如下:
Student **s = (Student**)lua_newuserdata(L, sizeof(Student*)); // lua will manage Student** pointer
*s = new Student; //這里我們分配了內(nèi)存,后面我們會介紹怎么讓Lua在gc的時候釋放這塊內(nèi)存
接下來是getName,setName,setAge,getAge和print方法的定義:
復(fù)制代碼 代碼如下:
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充當(dāng)了C++類和Lua的一個橋梁,另外,我們在從Lua棧里面取出數(shù)據(jù)的時候,一定要記得檢查數(shù)據(jù)類型是否合法。
注冊C API到Lua里面
最后,我們需要把剛剛編寫的這些函數(shù)注冊到Lua虛擬機里面去。
復(fù)制代碼 代碼如下:
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;
}
現(xiàn)在,我們把luaopen_student函數(shù)添加到之前的注冊函數(shù)里面去:
復(fù)制代碼 代碼如下:
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里面調(diào)用require "模塊名"
luaL_requiref(L, lib->name, lib->func, 1);
lua_settop(L, 0);
}
Lua訪問C++類
現(xiàn)在,我們在Lua里面操作這個Student類。注意,我們綁定的每一個函數(shù)都需要一個student對象作為參數(shù),這樣使用有一點不太方便。
復(fù)制代碼 代碼如下:
local s = cc.create()
cc.setName(s,"zilongshanren")
print(cc.getName(s))
cc.setAge(s,20)
print(cc.getAge(s))
cc.print(s)
最后,輸出的結(jié)果為:
復(fù)制代碼 代碼如下:
zilongshanren
20
My name is: zilongshanren, and my age is 20
提供Lua面向?qū)ο蟛僮鰽PI
現(xiàn)在我們已經(jīng)可以在Lua里面創(chuàng)建C++類的對象了,但是,我們最好是希望可以用Lua里面的面向?qū)ο蟮姆绞絹碓L問。
復(fù)制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()
而我們知道s:setName(xx)就等價于s.setName(s,xx),此時我們只需要給s提供一個metatable,并且給這個metatable設(shè)置一個key為”__index”,value等于它本身的metatable。最后,只需要把之前Student類的一些方法添加到這個metatable里面就可以了。
MetaTable
我們可以在Registry里面創(chuàng)建這個metatable,然后給它取個名字做為索引,注意,為了避免名字沖突,所以這個名字一定要是獨一無二的。
復(fù)制代碼 代碼如下:
//創(chuàng)建名字為tname的metatable并放在當(dāng)前棧頂,同時把它與Registry的一個key為tname的項關(guān)聯(lián)到一起
int luaL_newmetatable (lua_State *L, const char *tname);
//從當(dāng)前棧頂獲取名字為tname的metatable
void luaL_getmetatable (lua_State *L, const char *tname);
//把當(dāng)前棧index處的userdata取出來,同時檢查此userdata是否包含名字為tname的metatable
void *luaL_checkudata (lua_State *L, int index,const char *tname);
接下來,我們要利用這3個C API來為我們的student userdata關(guān)聯(lián)一個metatable.
修改綁定代碼
首先,我們需要創(chuàng)建一個新的metatable,并把setName/getName/getAge/setAge/print函數(shù)設(shè)置進去。 下面是一個新的函數(shù)列表,一會兒我們要把這些函數(shù)全部設(shè)置到metatable里面去。
復(fù)制代碼 代碼如下:
static const struct luaL_Reg studentlib_m [] = {
{"setName",l_setName},
{"setAge", l_setAge},
{"print", l_print},
{"getName",l_getName},
{"getAge", l_getAge},
{NULL, NULL}
};
接下來,我們創(chuàng)建一個metatable,并且設(shè)置metatable.__index = matatable.注意這個cc.Student的元表會被存放到Registry里面。
復(fù)制代碼 代碼如下:
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;
}
最后,我們記得在創(chuàng)建Student的時候把此元表與該userdata關(guān)聯(lián)起來,代碼如下:
復(fù)制代碼 代碼如下:
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對象的時候,使用的是下面的函數(shù)
復(fù)制代碼 代碼如下:
Student **s = (Student**)luaL_checkudata(L,1,"cc.Student");
這個luaL_checkudata除了可以把index為1的棧上的元素轉(zhuǎn)換為userdata外,還可以檢測它是否包含“cc.Student”元表,這樣代碼更加健壯。 例如,我們之前的setName函數(shù)可以實現(xiàn)為:
復(fù)制代碼 代碼如下:
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++類
現(xiàn)在,我們可以用Lua里面的面向?qū)ο蠓椒▉碓L問C++對象啦。
復(fù)制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
print(s:getName())
s:setAge(20)
print(s:getAge())
s:print()
這里輸出的結(jié)果為:
復(fù)制代碼 代碼如下:
zilongshanren
20
My name is: zilongshanren, and my age is 20
管理C++內(nèi)存
當(dāng)Lua對象被gc的時候,會調(diào)用一個__gc方法。因此,我們需要給綁定的C++類再添加一個__gc方法。
首先是C++端的實現(xiàn):
然后,添加注冊函數(shù):
復(fù)制代碼 代碼如下:
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的構(gòu)造函數(shù)和析構(gòu)函數(shù)里面添加輸出:
復(fù)制代碼 代碼如下:
Student::Student()
:name("default")
{
cout"Student Contructor called"endl;
}
Student::~Student()
{
cout"Student Destructor called"endl;
}
接下來是Lua代碼:
復(fù)制代碼 代碼如下:
local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()
--當(dāng)一個對象設(shè)置為nil,說明沒有其它對應(yīng)引擎之前cc.create創(chuàng)建出來的對象了,此時lua返回到c程序的時候會調(diào)用gc
s = nil
--如果想在Lua里面直接手動gc,可以調(diào)用下列函數(shù)
--collectgarbage
最后,程序輸出結(jié)果如下:
復(fù)制代碼 代碼如下:
Student Contructor called
My name is: zilongshanren, and my age is 20
Student Destructor called
總結(jié)
本文主要介紹如何使用UserData來綁定C/C++自定義類型到Lua,同時通過引入MetaTable,讓我們可以在Lua里面采用更加簡潔的面向?qū)ο髮懛▉碓L問導(dǎo)出來的類。下一篇文章,我們將介紹Cococs2D-X里面的tolua++及其基本使用方法。 PS:附上本文源代碼,注意在LuaCocos2D-X工程里面。
您可能感興趣的文章:- Lua教程(五):C/C++操作Lua數(shù)組和字符串示例
- Lua教程(四):在Lua中調(diào)用C語言、C++的函數(shù)
- Lua教程(三):C語言、C++中調(diào)用Lua的Table示例
- Lua教程(二):C++和Lua相互傳遞數(shù)據(jù)示例
- Lua教程(一):在C++中嵌入Lua腳本