Lua教程(六):綁定一個簡單的C++類_Lua

來源:互聯網
上載者:User

本文是最後一篇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工程裡面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.