天天看點

[tolua++]官方例子1 tarray

這個文章不算是教程,隻是我學習tolua++的學習筆記,有什麼不對的請多多指教

我所使用的tolua++是1.0.92,lua使用的是5.1.5.之是以不用最新版的lua,是因為無論是tolua還是tolua++已經好久沒有更新了,而無奈自己能力有限,是以就這麼用着把。

tarray講解的是數組,結構體有關的内容。主要就是*.pkg和lua的寫法。

struct Point
{
	float x;
	float y;
};
extern int a[10];
//這個為什麼加個const
extern const Point p[10];
extern Point*pp[10]
           

tarray.pkg的部分節選

官方說明:tolua binds such declarations to Lua global variables. Thus, in Lua, we can access the C/C++ variable naturally. If the variable is non constant, we can also assign the variable a new value from Lua. Global variables that represent arrays of value can also be bound to Lua. Arrays can be of any type. The corresponding Lua objects for arrays are Lua tables indexed with numeric values; however, be aware that index 1 in Lua is mapped to index 0 in an C/C++ array. Arrays must be pre dimensioned.

翻譯過來就是:

tolua可以綁定這樣的聲明到lua的全局變量。如果變量不是常量的話,也可以在lua中配置設定給它一個新的值,全局數組同樣也可以被綁定給lua。數組的類型可以是任何類型。lua中的從1開始的數組(lua中是table,也可以簡單認為數組)會映射到c/c++中的從0開始的數組(經我測試,c/c++的數組映射到lua中的table中,是從0開始的)。數組一定得預先确定好大小

很正常的編寫,唯一讓我在意的是,tarray.h中的p[10]是沒有const的,而pkg中是存在的。。。

module M
{
	extern int ma[10]@a;
	extern const Point mp[10]@p;
	extern Point*mpp[10]@pp;
}
           

對應的.h

extern int ma[10];
extern Point mp[10];
extern Point*mpp[10];
           

官方說明

tolua allows us to group constants, variables, and functions in a module. The module itself is mapped to a table in Lua, and its constants, variables, and functions are mapped to fields in that table.

tolua允許我們把常量,變量,和函數分組在一個模式塊中。這個模式塊本身被映射帶lua中的一個table表中。

在lua中使用的話就是 eg:M.ma[0]就是c/c++數組的ma[0].

關于這句

extern int ma[10]@a;
	extern const Point mp[10]@p;
	extern Point*mpp[10]@pp;
           

Renaming constants, variables and functions

When exporting constants, variable, and functions (members of a class or not), we can rename them, such that they will be bound with a different name from their C/C++ counterparts. To do that, we write the name they will be referenced in Lua after the character  @ . 

常量,變量,函數的重命名

嗯~,就我個人感覺,這就是指派

extern Point*mpp[10]@pp;
           

在lua使用這個是eg:M.mpp[0].x的。這點需注意

總結結束

注:

我下載下傳的tolua++的tarray.lua都是for i=1,10 do ...end的,但是我在實際運用時發現得這樣for i=0,9 do ...end

不知道什麼原因。

繼續閱讀