[tolua++]官方例子1 tarray

来源:互联网 发布:淘宝手机端图片不清楚 编辑:程序博客网 时间:2024/05/17 04:58

这个帖子不算是教程,只是我学习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];//这个为什么加个constextern 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

不知道什么原因。



0 0
原创粉丝点击