zoukankan      html  css  js  c++  java
  • 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()
    ");
        }
    
        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;
    }
    
  • 相关阅读:
    用 Timer Applet 做 GTD 料理
    Envy-便当的显卡驱动布置剧本
    DB2 9 运用开发(733 测验)认证指南,第 1 部分: 数据库工具与编程办法(1)
    Sabayon:经管 GNOME 用户的设置
    应用 KScope 阅读并编纂你的源代码
    Cheese-从摄像头捕捉照片和视频
    Kaffeine Player:功用富厚的媒体播放器
    SpeedCrunch:很酷的桌面较量争论器
    DB2 9 根蒂根基(730 测验)认证指南,第 7 部分: XQuery 简介(6)
    Conduit 0.3.2 颁布
  • 原文地址:https://www.cnblogs.com/ShawSpring/p/6088825.html
Copyright © 2011-2022 走看看