tolua

Using Lua API and tag method facilities, tolua maps C/C++ constants, external variables, functions, classes, and methods to Lua.How tolua works(1)創(chuàng)建a package file (a C/C++ cleaned header file) listing the constants, variables, functions, classes, and methods we want to export to the Lua environment.(2)parses this file and creates a C/C++ file that automatically binds the C/C++ code to Lua. (3) accesed from LuaHow to use toLua由(1)an executable (2)and a library組成the executable represents the parser that reads a package file and output a C/C++ code that implements the binding to access the C/C++ features from Lua.tolua -o myfile.c myfile.pkgtolua -H lua_XXX.h -o lua_XXX.cpp -n pkgXXX XXX.pkgtolua -n pkgname -o myfile.c myfile.pkgwhen using C++, we can opt for automatic initializationtolua -a -n pkgname -o myfile.c myfile.pkgautomatic initialization sometimes does not work也可以調(diào)用tolua_pkgname_close(void);解綁可以使用-H選項(xiàng)將open與close操作導(dǎo)出到頭文件中漆际。void tolua_restorestate (void);Basic Conceptspackage file may include other package file, use $Basic types

char int float double ? ? -> ? ? ? number

char* ? ? ? -> ? ? ? ? ? ? ?string

void* ? ? ? -> ? ? ? ? ? ? ?userdata

tolua ignores const modifier

Functions in C/C++ can also manipulate Lua objects explicitly.

lua_Object也是一種基本類型盗胀。

User defined types

非Basic Types均為用戶自定義類型整份,are mapped to tagged userdata type in Lua.

Lua can only store pointers to 自定義類型唬渗。

NULL and nil

Typedefs

必須使用之前聲明

typedef double real;

Including real header files

$#include "header1.h"

可以使用C/C++支持的注釋方式進(jìn)行注釋。

Binding constants

(1) define's

#define NAME [VALUE]

(2) enum's

enum {

NAME1[=VALUE1],

NAME2[=VALUE2]

};

Binding external variables

[extern] type var;

double v[10];//Lua中下標(biāo)從1開始

Binding functions

無(wú)參數(shù)時(shí)加void

Arrays

change to table

void func (int n, int m, double image[n*m]);

Overloaded functions

接受

但是只會(huì)根據(jù)參數(shù)轉(zhuǎn)換后的Lua類型進(jìn)行判斷

Default parameters values

void func(lua_Object lo = TOLUA_NIL)

void func(int a[5] = 0);

Multiple returned values

tolua uses feature of?Multiple returned values to simulate values passed by references

void swap (double* x, double* y);

or

void swap (double& x, double& y);

x, y = swap (x, y)

Binding struct fields

typdef struct [name] {

int x[10];

int y[10];

} Example;

Binding classes and methods

Specifying inheritance

class classname: public basename

{

/* class definition*/

};

Specifying exported memebers and methods

Constructors are called as static methods, named new,

Destructors are called as a conventional method called delete

virtual keyword has no effect in the package file

example:

class Point {

static int n;

double x;

double y;

static char* className(void);

Point (void);

Point (double px, double py);

~Point(void);

Point add (Point& other);

};

overloaded operators

operator+ - * / < > <= >=

operator[]

example:

double operator[](int index);

Module definition

module name

{

#define N

extern int var;

int func (...);

}

Renaming constants, variables and functions

#define CNAME @ ivar;

void cfunc @ ifunc (...);

class cannot be renamed, because they represent types in C.

Storing additional fields

based on table

Exported utility functions

tolua uses itself to export some utility functions to Lua, including its object-oriented framework.

tolua.using(table)

receives a table and maps all its fields to the global environment. thus we can map an entire module and access its features without the module prefix.

tolua.type(var)

returns a string representing the object type

tolua.tag("type")

returns type corresponding tag number

tolua.foreach(object)

this function filters all fields starting with a dot, not passing them to the provided callback function.

tolua.cast(object, "typename")

Returns the object "casted" to the given type. The object must represent an user type, otherwise return nil.

tolua.takeownership(object)

asks tolua to take the ownership of the given object.

This means the C/C++ object will be freed/destructed when garbage-collected by Lua. The object must represent an user type, otherwise an執(zhí)行錯(cuò)誤產(chǎn)生准浴。

tolua.class(table, base = nil)

the created class can inherit from a base class, previously created.

tolua.instance(table, class)

Sets the given table to be an instance of the given class.

example:

--define a Point class

classPoint = {x=0, y=0}

tolua.class(classPoint)--set as a class

--define print function

function classPoint:print()

print (self.x, self.y)

end

-- define add method

function classPoint:add(p2)

return Point{x=self.x+p2.x, y=self.y+p2.y}

end

-- define a Point constructor

function Point (p)

tolua.instance(p, classPoint) -- set as an instance of classPoint

return p

end

-- define a Color Point class

classColorPoint = {color = 'black' }

tolua.class(classColorPoint, classPoint) -- set as class inheriting from classPoint

-- define class methods

function classColorPoint:print()

print(self.x, self.y, self.color)

end

-- define Color Point constructor

function ColorPoint(p)

tolua.instance(p, classColorPoint) -- set as an instance of classColorPoint

return p

end

-- some valid codes would then be

p = Point{ x= 1}

q = ColorPoint{x=2, y =3, color =2)

r = p:add(q)

r:print()

Embedded Lua code

tolua allows us to embed Lua code in the C/C++ generated code.

$[

embedded Lua code

...

$]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末备禀,一起剝皮案震驚了整個(gè)濱河市趁仙,隨后出現(xiàn)的幾起案子懂傀,更是在濱河造成了極大的恐慌趾诗,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹬蚁,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡郑兴,警方通過(guò)查閱死者的電腦和手機(jī)犀斋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)情连,“玉大人叽粹,你說(shuō)我怎么就攤上這事∪匆ǎ” “怎么了虫几?”我有些...
    開封第一講書人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)挽拔。 經(jīng)常有香客問(wèn)我辆脸,道長(zhǎng),這世上最難降的妖魔是什么螃诅? 我笑而不...
    開封第一講書人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任啡氢,我火速辦了婚禮状囱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘倘是。我一直安慰自己亭枷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開白布搀崭。 她就那樣靜靜地躺著叨粘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瘤睹。 梳的紋絲不亂的頭發(fā)上升敲,一...
    開封第一講書人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音默蚌,去河邊找鬼冻晤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛绸吸,可吹牛的內(nèi)容都是我干的鼻弧。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼锦茁,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼攘轩!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起码俩,我...
    開封第一講書人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤度帮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后稿存,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體笨篷,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年瓣履,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了率翅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袖迎,死狀恐怖冕臭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情燕锥,我是刑警寧澤辜贵,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站归形,受9級(jí)特大地震影響托慨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜连霉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一榴芳、第九天 我趴在偏房一處隱蔽的房頂上張望嗡靡。 院中可真熱鬧,春花似錦窟感、人聲如沸讨彼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)哈误。三九已至,卻和暖如春躏嚎,著一層夾襖步出監(jiān)牢的瞬間蜜自,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工卢佣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留重荠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓虚茶,卻偏偏與公主長(zhǎng)得像戈鲁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嘹叫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容