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章,看一遍并不是很难,但是只是朦胧的感觉,雾里看花,水中望月.最终还是决定敲出来自己看看,练练手,结果受益不少,也遇到了一些 ...
随机推荐
- LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...
- Adapter 启动时报错——2
在安装tibco adr3 7.00以前的版本,在designer中启动adr3 会报出“无法加载adr3.dll”文件的错误,这是因为在designer中的palettes默认是指向adapter ...
- 移动端flex布局 微信和UC的兼容性
请查看以下两个链接 http://www.tuicool.com/articles/Afq6Bzq http://www.sheng00.com/2148.html
- OC基础--结构体 枚举做类成员属性
结构体 枚举作类的成员属性: 定义一个学生类 性别 -- 枚举 生日 入学日期 毕业日期 -- 结构体 代码示例: 声明文件 Student.h: #import <Foundation ...
- powershell读写磁盘变量(对象序列化)
---[第一章,开篇引子]--- 很久很久以前,我在网上闲逛,看到一头powershell菜鸟,写了一篇感慨道“挖哈哈,我学会powershell啦,我以前很笨,各种不懂,还想把变量保存起来.” 实际 ...
- LLBLGen Pro v4.2_Patch+Keygen
将dll文件覆盖安装目录下的文件,之后用算号器算出license文件,将license文件放在安装目录下即可. 算号器是在http://www.dxper.net/thread-408-1-1.htm ...
- Makefile 一点一滴(三)—— 尝试简单的变量替换
上一版的 makefile: TestCpp : ./debug/TestCpp.o g++ -o TestCpp ./debug/TestCpp.o ./debug/TestCpp.o : ./sr ...
- Java泛型学习笔记 - (六)泛型的继承
在学习继承的时候, 我们已经知道可以将一个子类的对象赋值给其父类的对象, 也就是父类引用指向子类对象, 如: Object obj = new Integer(10); 这其实就是面向对象编程中的is ...
- mac 下配置tomcat
下面就是一些简单的步骤,帮你把Tomcat7安装在你的Mac上. 下载一个 二进制包: apache-tomcat-7.0.27.tar.gz ,可以在Apache的官方网站找到. 双击解压在你的下载 ...
- 音频软件消除人声的一点体会(cood edit ,goldwav)
音频软件消除人声的一点体会(cood edit ,goldwav) 使用方法: 1.打开文件 2.命令处理(红色位置可以调整到你认为合适的数据或效果) 3.效果:两个软件均处理后的效果均可以接受.不 ...