lua userdata
#define metatablename "studentlib.06-11-11"
/**
* utility functions
*/
static int pusherror(lua_State *L, const char *info = NULL)
{
lua_pushnil(L);
if(info)
{
lua_pushfstring(L, "%s :%s", info, strerror(errno));
}
else
{
lua_pushstring(L, strerror(errno));
}
lua_pushinteger(L, errno);
return 3;
}
/**
* the struct in c
*/
struct studentTag
{
char *name;
int no;
int sex;
int age;
};
static int student_gc(lua_State *L)
{
studentTag *pStudent = (studentTag *)lua_touserdata(L, 1);
if(pStudent->name)
{
free(pStudent->name);
//printf("student_gc()\n");
}
return 0;
}
static int newStudent(lua_State *L)
{
size_t nBytes = sizeof(studentTag);
studentTag *pStudent = (studentTag *)lua_newuserdata(L, nBytes);
luaL_setmetatable(L, metatablename);
return 1;
}
static int setName(lua_State *L)
{
studentTag *pStudent = (studentTag *)luaL_checkudata(L, 1, metatablename);
const char *name = luaL_checkstring(L, 2);
luaL_argcheck(L, name != NULL && name != "", 2, "expect a name");
pStudent->name = (char *)malloc(luaL_len(L, 2) + 1);
if(pStudent->name == NULL)
{
pusherror(L);
}
strcpy(pStudent->name, name);
return 0;
}
static int getName(lua_State *L)
{
studentTag *pStudent = (studentTag *)luaL_checkudata(L, 1, metatablename);
lua_pushstring(L, pStudent->name);
return 1;
}
static int setNo(lua_State *L)
{
studentTag *pStudent = (studentTag *)luaL_checkudata(L, 1, metatablename);
int no = luaL_checkinteger(L, 2);
luaL_argcheck(L, no > 0, 2, "invalid number");
pStudent->no = no;
return 0;
}
static int getNo(lua_State *L)
{
studentTag *pStudent = (studentTag *)luaL_checkudata(L, 1, metatablename);
lua_pushinteger(L, pStudent->no);
return 1;
}
struct luaL_Reg lib_m[] =
{
{"setName", setName},
{"name", getName},
{"setNo", setNo},
{"no", getNo},
{NULL, NULL}
};
struct luaL_Reg lib_f[] =
{
{"new", newStudent},
{NULL, NULL}
};
extern "C" __declspec(dllexport) int luaopen_student(lua_State *L)
{
luaL_newmetatable(L, metatablename);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, student_gc); //
lua_setfield(L, -2, "__gc");
luaL_setfuncs(L, lib_m, 0);
lua_newtable(L); // 相当于 newlib
luaL_setfuncs(L, lib_f, 0);
lua_pushliteral(L, "Copyright (C) 2016-2018 Kepler Project");
lua_setfield(L, -2, "_COPYRIGHT");
lua_pushliteral(L, "a lua library for a student struct");
lua_setfield(L, -2, "_DESCRIPTION");
lua_pushliteral(L, "1.0");
lua_setfield(L, -2, "_VERSION");
return 1;
}
lua userdata的更多相关文章
- <转> lua: userdata的metatable使用
1 如何封装c++的指针 对于c++对象的lua包装,我们可以使用 template<typename T> struct luaUserdataWrapper { luaUserdat ...
- Lua C++交互 应用实例步骤(UserData使用)
一.配置Lua C++交互环境 1.下载Lua 包环境 地址: https://www.lua.org/download.html ,我们这里用的是5.4.2版本. 2.新建C++ 控制台应用程序 3 ...
- 用好lua+unity,让性能飞起来——lua与c#交互篇
前言 在看了uwa之前发布的<Unity项目常见Lua解决方案性能比较>,决定动手写一篇关于lua+unity方案的性能优化文. 整合lua是目前最强大的unity热更新方案,毕竟这是唯一 ...
- c++对象导出到lua
转自:http://www.cnblogs.com/ringofthec/archive/2010/10/26/luabindobj.html 虽然有tolua++, luabind等等, 不过自己手 ...
- 20140102-lua binder另一只轮子的雏形
书接上一回,说到要继续丰富对类型的处理.那么如何才能做到呢,应该是要支持自定义的,所以这一回要讲的就是在前面的基础上,增加支持自定义部分,其中包含以下几个部分 函数的默认参数设置,包括有几个默认参数和 ...
- Lua 之 userdata
Lua 之 userdata 在Lua中可以通过自定义类型(user data)与C语言代码更高效.更灵活的交互,从而扩展Lua能够表达的类型. full userdata full userdata ...
- Lua中的userdata
[话从这里说起] 在我发表<Lua中的类型与值>这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型.现在是时候总结了.对于functio ...
- Lua 与 C 交互之UserData(4)
lua作为脚本于要能够使用宿主语言的类型,不管是宿主基本的或者扩展的类型结构,所以Lua提供的UserData来满足扩展的需求.在Lua中使用宿主语言的类型至少要考虑到几个方面: 数据内存 生命周期 ...
- lua学习笔记之userdata
这一段时间看了<programming in lua>中的第28章,看一遍并不是很难,但是只是朦胧的感觉,雾里看花,水中望月.最终还是决定敲出来自己看看,练练手,结果受益不少,也遇到了一些 ...
随机推荐
- UIPIckerView现实城市选择
实现城市选择,选中省时,后来自动显示相对应的城市,并且下面会打印出来对应的省和城市 . 因为plist里面是一个一个的字典. 1.字典转模型 HMCities.h #import <Founda ...
- socketserver服务器
''' 网络编程 Socket(TCP,IP)套接字 服务端 运行起来, 客户端 客户端 客户端 客户端 服务端: import socket sk = socket.socket() #绑定端口号 ...
- Android6.0中的权限
Android6.0相比之前的Android版本有一个很大的不同点,就是动态的获取权限.之前我们需要什么权限只需要在Manifest文件中声明即可,在6.0中,又新增了运行时权限的动态检测. Andr ...
- apache和nginx
虽然nginx使用较少 还是写写文章,记录下 nginx是异步非阻塞,apache是阻塞的. apache动态页面比nginx好. 由于nginx的高并发性(使用epoll模型),所以出来静态页面性能 ...
- 第三篇:Retrofit SDK的设计思路
2016-05-08 15:24:03 Retreofit毫无疑问是一个优美的开源框架,有轻量级.耦合性低.扩展性好.灵活性高的特点,那么Retrofit的设计者们到底是怎么样做到这些的呢?我希望能够 ...
- Bootstrap<基础二十四> 缩略图
Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...
- OpenLDAP安装
参考: http://54im.com/openldap/centos-6-yum-install-openldap-phpldapadmin-tls-%E5%8F%8C%E4%B8%BB%E9%85 ...
- 运行impala tpch
1.安装git和下载tpc-h-impala脚步 [root@ip-172-31-34-31 ~]# yum install git [root@ip-172-31-34-31 ~]# git clo ...
- uboot环境变量实现分析
u-boot的环境变量用来存储一些经常使用的参数变量,uboot希望将环境变量存储在静态存储器中(如nand nor eeprom mmc). 其中有一些也是大家经常使用,有一些是使用人员自己定义的, ...
- Text Justification
在一段时间没刷题之后,我发现脑子严重地滞涩了.这题AC花了好大力气,出现了Memory Limit Exceed,Core Dump,以及各种普通的编译.运行错误.MLE 和 CD错误是比较难检查的, ...