#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的更多相关文章

  1. <转> lua: userdata的metatable使用

    1 如何封装c++的指针 对于c++对象的lua包装,我们可以使用 template<typename T> struct luaUserdataWrapper {  luaUserdat ...

  2. Lua C++交互 应用实例步骤(UserData使用)

    一.配置Lua C++交互环境 1.下载Lua 包环境 地址: https://www.lua.org/download.html ,我们这里用的是5.4.2版本. 2.新建C++ 控制台应用程序 3 ...

  3. 用好lua+unity,让性能飞起来——lua与c#交互篇

    前言 在看了uwa之前发布的<Unity项目常见Lua解决方案性能比较>,决定动手写一篇关于lua+unity方案的性能优化文. 整合lua是目前最强大的unity热更新方案,毕竟这是唯一 ...

  4. c++对象导出到lua

    转自:http://www.cnblogs.com/ringofthec/archive/2010/10/26/luabindobj.html 虽然有tolua++, luabind等等, 不过自己手 ...

  5. 20140102-lua binder另一只轮子的雏形

    书接上一回,说到要继续丰富对类型的处理.那么如何才能做到呢,应该是要支持自定义的,所以这一回要讲的就是在前面的基础上,增加支持自定义部分,其中包含以下几个部分 函数的默认参数设置,包括有几个默认参数和 ...

  6. Lua 之 userdata

    Lua 之 userdata 在Lua中可以通过自定义类型(user data)与C语言代码更高效.更灵活的交互,从而扩展Lua能够表达的类型. full userdata full userdata ...

  7. Lua中的userdata

    [话从这里说起] 在我发表<Lua中的类型与值>这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型.现在是时候总结了.对于functio ...

  8. Lua 与 C 交互之UserData(4)

    lua作为脚本于要能够使用宿主语言的类型,不管是宿主基本的或者扩展的类型结构,所以Lua提供的UserData来满足扩展的需求.在Lua中使用宿主语言的类型至少要考虑到几个方面: 数据内存 生命周期 ...

  9. lua学习笔记之userdata

    这一段时间看了<programming in lua>中的第28章,看一遍并不是很难,但是只是朦胧的感觉,雾里看花,水中望月.最终还是决定敲出来自己看看,练练手,结果受益不少,也遇到了一些 ...

随机推荐

  1. Nehe Opengl

    http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ Nehe Opengl,据说从基础到高端进阶都是很好的教程,准备学习这个序列,顺便记录下随 ...

  2. 通过JDBK操作数据库

    一.配置程序--让我们程序能找到数据库的驱动jar包1.把.jar文件复制到项目中去,整合的时候方便.2.在eclipse项目右击"构建路径"--"配置构建路径" ...

  3. Pycharm快捷方式

    PYCHARM的快捷方式 PyCharm3.0默认快捷键(翻译的)1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意 ...

  4. html5之canvas画图基础

    HTML5+CSS3的好处是,你可以编写一个页面分别用于不同的平台,只需要设置不同的css样式就可以了,现在基本主流浏览器都支持全新的HTML5和CSS3,因为它的跨平台开发.因为是原生代码所以它的页 ...

  5. app上传到App Store的快捷方法及步骤

    跳过证书的申请及配置概要文件的设置, 现在根据已有的配置概要文件及发布证书开始: 1.先在Xcode上的PROJECT和TARGETS->Build Setting->Code Signi ...

  6. 不是SELECTed表达式

    sql语句如下: select distinct(p.project_name) name,p.pkid pkid, p.report_year year, q.cor_name cor_name,g ...

  7. 5、 Android 之Fragment

    上:http://blog.csdn.net/lmj623565791/article/details/37970961 下:http://blog.csdn.net/lmj623565791/art ...

  8. JavaWeb--Servlet部分笔记

    1.集群:数万个服务器协同工作 2.web应用核心组件:jsp和servlet(属于门户),都在web容器中执行 3.web客户端发http请求(大的字符串)给web服务器:web服务器根据头信息来定 ...

  9. 关于jetty项目中的问题.

    在某台虚拟机上部署的项目出现的问题: 我想要更改定义的owl文件,重启服务器,却打不开网页. 1.couldnot found owl ,然后我拷贝一份owl到work/config目录下,继续更改配 ...

  10. LCD相关知识点

    1.LCD即液晶显示器,控制原理是控制其中的电子枪,在n行*n列的屏幕上投射不同颜色从而形成图像 2.编程步骤: ①打开LCD背光将LCD背光对应的GPIO设置为禁止上拉(GPxUP相应位写入1),选 ...