#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. ubuntu 下安装mysql,以及配置远程登录

    安装MysQL 在Ubuntu14.04下安装MySQL比较简单,只需下面这条命令就行了: 1.输入 sudo apt-get install mysql-server 2.继续执行后,需要设定MyS ...

  2. 通过淘宝IP地址库获取IP位置

    地址:http://ip.taobao.com/ 提供的服务包括: 1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商. 2. 用户可以根据自己 ...

  3. Emoji表情符号录入MySQL数据库失败解决

    让MySQL支持Emoji表情,涉及无线相关的 MySQL 数据库建议都提前采用 utf8mb4 字符集. utf8mb4和utf8到底有什么区别呢?原来以往的mysql的utf8一个字符最多3字节, ...

  4. ssh 整合

    1. 加入 Spring 1). 加入 jar 包 2). 配置 web.xml 文件 3). 加入 Spring 的配置文件. 2. 加入 Hibernate 1). 同时建立持久化类, 和其对应的 ...

  5. 白皮 Chapter 1

    6.29 今天主要做了一些1.5中的小结和练习,果然换语言思路也要跟着变么…各种不爽啊不爽… scanf各种忘记&,还有各种忘记return 0… average temperature su ...

  6. 使用git删除远程仓库文件

    git rm -r -f --cached 文件或文件夹 git commit -m "移除文件或文件夹" git push origin master 注意:要删除的文件或文件夹 ...

  7. 学员报名WDP培训之前必须阅读

    Oracle WDP核心概念:Oracle WDP的全称为Oracle Workforce Development Program,主要面向学生.个人市场,这是Oracle公司针对职业教育市场在全球推 ...

  8. CGContext 解释

    Managing Graphics Contexts:管理图形上下文 CGContextFlush // 强制立即渲染未执行在一个窗口上下文上的绘图操作到目标设备.系统会在合适的时机自动调用此函数,手 ...

  9. C#3.0 扩展方法

    扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.对于用 C# 和 Visual ...

  10. 修改(table的section与上一个section的间距)section header背景颜色

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView * ...