本实例实现一种很简单的类型------布尔数组。C语言可以实现将每个布尔值存储在一个bit中,从而减少内存用量。
必须的一些宏
Code
Snippet
- #define BITS_PER_WORD (CHAR_BIT * sizeof(unsigned int)) //bit
- #define I_WORD(i) ((unsigned int) i / BITS_PER_WORD) //bitword
- #define I_BIT(i)
1<<((unsigned int) (i) % BITS_PER_WORD) //word
bit
lua函数 lua_newuserdata
该函数会根据指定大小分配一块内存,并将对应的userdata压入栈中最后返回这个内存块的地址。
以下函数用lua_newuserdata创建一个新的布尔数组
以下函数用lua_newuserdata创建了一个新的布尔数组
Code
Snippet
- typedef struct NumArray{
- int size;
- unsigned int values[1];
- }NumArray;
-
- static int newarray(lua_State * L)
- {
-
- size_t nBytes;
- NumArray * a;
- int n = luaL_checkint(L,1); //
- luaL_argcheck(L,n>=1,1,"invalid
size"); //>1
- nBytes = sizeof(NumArray) + I_WORD(n-1)*sizeof(unsigned int);
- a = (NumArray * ) lua_newuserdata(L,nBytes);
- a->size = n;
- for(int i = 0;i<I_WORD(n-1);i++)
- a->values[i] =
0;//
- return 1;
- }
只要lua注册好newarray就可以通过 a = array.new(1000)
来创建一个数组。array.set(array,index,value)在数组中存储元素。
Code
Snippet
- static int setarray(lua_State * L)
- {
- NumArray * a = (NumArray * ) lua_touserdata(L,1);
- int index = luaL_checkint(L,2);
- luaL_checkany(L,3);
-
- if(lua_toboolean(L,3))
- a->values[I_WORD(index)] |=
I_BIT(index);
- else
- a->values[I_WORD(index)] |=
I_BIT(index);
- return 0;
- }
对应的有getarray
Code
Snippet
- static int getarray(lua_State * L)
- {
- NumArray * a= (NumArray * ) lua_touserdata(L,1);
- int index = luaL_checkint(L,2);
- lua_pushboolean(L,a->values[I_WORD(index)] &
I_BIT(index));
- return 1;
- }
下面还定义了一个函数用于检索一个数组大小
Code
Snippet
- static int getsize(lua_State * L)
- {
- NumArray * a = (NumArray*) lua_touserdata(L,1);
- lua_pushinteger(L,a->size);
- return 1;
- }
最后需要代码来初始化这个库
Code
Snippet
- static const struct luaL_Reg arraylib[] =
- {
- {"new",newarray},
- {"set",setarray},
- {"get",getarray},
- {"size",getsize},
- {NULL,NULL}
- };
-
- int luaopen_array(lua_State * L)
- {
- luaL_register(L,"array",arraylib);
- return 1;
- }
28.2 元表
一种辨别不
同类型的userdata的方法是,为每种类型创建一个唯一的过元表,每当创建一个userdata后,就用相应的元表来标记它。每当和到一个userdata之后,就检查它是否拥有正确元表。由于lua代码不能改变usedata的元表,因此也无法欺骗代码。
通常辅助库提供了一些函数来实现这些内容,可以使用的辅助库函数有
int luaL_newmetatable(lua_State * L,const char * name)
创建一个新的table用作元表,并将其压栈,然后将这个table与注册表中的指定名称关联起来
int luaL_getmetatable(lua_State * L ,const char * name)
可以在注册表中检测与name关联的元表
void * luaL_checkudata(lua_State * L ,int index,const char * name)
可以检查栈中指定位置上是否有一个userdata,或者它不具有正确名称,就会引发一个错误。否则就返回这个usrdata的地址。
下面开始修改代码 :
1.必须创建一元表
Code
Snippet
- int luaopen_array(lua_State * L)
- {
- luaL_newmetatable(L,"LuaBook.array");
- luaL_register(L,"array",arraylib);
- return 1;
- }
2.修改newarray 使其为所有新建的数组设置这个元表
Code
Snippet
- static int newarray(lua_State * L)
- {
-
- size_t nBytes;
- NumArray * a;
- int n = luaL_checkint(L,1); //
- luaL_argcheck(L,n>=1,1,"invalid
size"); //>1
- nBytes = sizeof(NumArray) + I_WORD(n-1)*sizeof(unsigned int);
- a = (NumArray * ) lua_newuserdata(L,nBytes);
- a->size = n;
- for(int i = 0;i<I_WORD(n-1);i++)
- a->values[i] =
0;//
-
- luaL_getmetatable(L,"LuaBook.array");
- lua_setmetatable(L,-2);
- return 1;
- }
3.最后,setarray getarray getzie 必有检其第一个参数是不回为一个合法的数组。所以定义了如下宏
Code
Snippet
- #define checkarray(L) \\par
(NumArray*) luaL_checkudata(L,1,"LuaBook.array")
28.3 面向对象的访问
下一步就是这种新类型变换为一个对象,然后就可以用普面向对象的语法来使用它们了如
a = array.new(1000)
print(a:size()) --1000
a:set(10,true)
print(a:get(10)) –true
注意:a: size() 等价于a.size(a)
因此,必须使用表达式a.size返回前面定义的函数getsize。实现这一点的关键是使用__index元方法。对于table而言Lua会在找不到指定key时调用
这个,对于userdata,则会在每次访问都调用它。假设运行如下代码
local metaarray = getmetatable(array.new(1)) --创建一个数组
metaarray.__index = metaarray
metaarray.set = array.set
metaarray.get = array.get
metaarray.size = array.size
第一行创建了数组,并将它的元表赋予metaarray。然后将metaarray.__index设置为metaarray。当对size求值时,lua会尝试通过a的元表__index字段来查这个值最后就找到了array.size()
其实,在c中也可以达到相同的效果,甚至还可以做的更好现在数组是一种具有操作的对象,可以无须在table
array中保存这些操作。程序库只要导出一个用于创建新数组函数new就可以了。所有其它操作都可以作为对象的方法。c代码同样可以直接注册这些方法。操作getsize,getarray和setarray无须作任何改变
唯一改变的是注册它们的方式 。首先需要设置两个独立的函数列表,一个用于常规函数,一个用于方法
Code
Snippet
- static const struct luaL_Reg arraylib_f[] =
- {
- {"new",newarray},
- {NULL,NULL}
- };
-
-
- static const struct luaL_Reg arraylib_m[] =
- {
- {"set",setarray},
- {"get",getarray},
- {"size",getsize},
- {NULL,NULL}
- };
新的打开函数luaopen_array必须创建过元表,并将它赋予__index字段,然后在元表中注册所有方法,最后创建并填充array
table。
Code
Snippet
- int luaopen_array(lua_State * L)
- {
- luaL_newmetatable(L,"LuaBook.array");
- /*.__index =
*/
- lua_pushvalue(L,-1);
//
- lua_setfield(L,-2,"__index");
-
- luaL_register(L,NULL,arraylib_m);
- luaL_register(L,"array",arraylib_f);
-
- return 1;
- }
其中用到了
luaL_register的另一个特性。在第一次调用中,以NULL作为库名,luaL_register不会创建任何用于存储函数的table,而是以栈顶的table作用存储函数的table。在本本例中栈顶table就是元表本身,因此lua_register会将所有方法放入其中,第二次调用luaL-register则提供了一个表名,它就根据此名倒地
一个新的table,并将指定的函数注册在这个table中。
28.4 数组的访问
另一种写法是常规的数组写法 a[i]
。由于函数setarray和getarray所接受的参数次序暗剑相应元方法的次序。因此在lua代码中可以快速的将这些元方法定义为。
local metaarray = getmetatable(array.new(1))
metaarray.__index =
array.get
metaarray.__newindex=array.set
metaarray.__len=array.size
如果还要更完美,可以在c代码中注册这些方法,为此需要再次修改初始化函数
Code
Snippet
- static const struct luaL_Reg arraylib_f[] =
- {
- {"new",newarray},
- {NULL,NULL}
- };
-
-
- static const struct luaL_Reg arraylib_m[] =
- {
- {"__newindex",setarray},
- {"__index",getarray},
- {"__len",getsize},
- {NULL,NULL}
- };
Code
Snippet
- int luaopen_array(lua_State * L)
- {
- luaL_newmetatable(L,"LuaBook.array");
-
- luaL_register(L,NULL,arraylib_m);
- luaL_register(L,"array",arraylib_f);
-
- return 1;
- }
28.5 轻量级userdata (light userdata)
之前介绍的是full userdata,Lua还提供了另一种轻量级userdata(light
userdata)。事实上,轻量级userdata仅仅表示的是C指针的值,即(void*)。由于它只是一个值,所以不用创建。如果需要将一个轻量级userdata放入栈中,调用lua_pushlightuserdata即可。full
userdata和light userdata之间最大的区别来自于相等性判断,对于一个full userdata,它只是与自身相等,而light
userdata则表示为一个C指针,因此,它与所有表示同一指针的light userdata相等。再有就是light
userdata不会受到垃圾收集器的管理,使用时就像一个普通的整型数字一样
用户自定义类型《lua程序设计》 28章 笔记,布布扣,bubuko.com
用户自定义类型《lua程序设计》 28章 笔记
原文:http://www.cnblogs.com/zhangdongsheng/p/3657787.html