首页 技术 正文
技术 2022年11月21日
0 收藏 571 点赞 4,955 浏览 2114 个字
#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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781