导出一个简单C++类到lua full userdata

  东西不用,很快就忘,lua其实我很认真的研究过,但是不用,转眼就忘了。

就像导出c/c++对象到lua,这次作个记录,以便日后查阅,lua5.2

 

test.cpp:

 

#include <stdio.h>
#include <iostream>
#include <lua.hpp>
#include <math.h>
#include <string>
#include <assert.h>
#include <sstream>
#include <string.h>

using namespace std;

class Person{
public:
    std::string    *     mName;
    std::string *     mId;
    std::string *     mPhone;
    short    mAge;
    short     mGender;    
};



static Person * checkPerson(lua_State *L){
    void *ud = luaL_checkudata(L, 1, "LuaBook.Person");
    luaL_argcheck(L, ud != NULL, 1, "`Person` expected");
    return (Person*)ud;
}

static int newPerson(lua_State *L){
    size_t nbytes = sizeof(Person);
    Person *p = (Person*)lua_newuserdata(L, nbytes);
    
    luaL_getmetatable(L, "LuaBook.Person");
    lua_setmetatable(L, -2);
    
    p->mName = new std::string("wangxiaoyun");
    p->mId = new std::string("f232-fadf-1231-dfafd");
    p->mPhone = new std::string("0472-6165500");
    
    p->mAge = 28;
    p->mGender = 1;
    
    return 1;
}

static int Person_setName(lua_State *L){
    Person *p = checkPerson(L);
    size_t len = 0;
    const char * newName = luaL_checklstring(L,2,&len);
    luaL_argcheck(L, len <= 256, 2, "name is too long");
    *(p->mName) = newName;
    return 0;
}

static int deletePerson(lua_State *L){
    Person *p = checkPerson(L);
    
    delete p->mName;
    delete p->mId;
    delete p->mPhone;
    
    p->mName = NULL;
    p->mId = NULL;
    p->mPhone = NULL;
}

static int tostringPerson(lua_State *L){
    Person *p = checkPerson(L);
    /*
    std::cout << "name:" << *p->mName << endl
                <<"id:" << *p->mId << endl
                <<"mPhone:" << *p->mPhone <<endl
                <<"mAge:" << p->mAge <<endl
                <<"mGender:" << p->mGender
                << endl;
    */
    stringstream ss;
    ss << "name:" << *p->mName << endl
                <<"id:" << *p->mId << endl
                <<"mPhone:" << *p->mPhone <<endl
                <<"mAge:" << p->mAge <<endl
                <<"mGender:" << p->mGender
                << endl;
    
    lua_pushstring(L, ss.str().c_str());
    
    return 1;
}

static int l_sin(lua_State *L){
    double d = lua_tonumber(L, 1);
    lua_pushnumber(L, sin(d));
    return 1;
}

static struct luaL_Reg mylib[] = {
    {"sin", l_sin},
    {NULL, NULL}
};

static struct luaL_Reg person_lib_f[] = {
    {"new", newPerson},
    {NULL, NULL}
};

static struct luaL_Reg person_lib_m[] = {
    {"delete", deletePerson},
    {"__tostring", tostringPerson},
    {"setName", Person_setName},
    {NULL, NULL}
};

void luaopen_Person(lua_State *L){
    luaL_newmetatable(L, "LuaBook.Person");
    
    lua_pushstring(L, "__index");
    lua_pushvalue(L, -2); //pushes the metatable
    lua_settable(L, -3);    //metatable.__index = metatable
    luaL_setfuncs(L, person_lib_m, 0);
    
    luaL_newlib(L, person_lib_f);
    lua_setglobal(L, "Person");
}

void luaopen_mylib(lua_State *L){
    luaL_newlib(L, mylib);
    lua_setglobal(L, "mylib");
}


void run_lua_file(lua_State *L, char * filename){
    assert(filename != NULL);
    if(luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0)){
        std::cout << ("cannot run lua file: %s", lua_tostring(L, -1));
    }
}


void test_mylib(){
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_mylib(L);
    run_lua_file(L,"test.lua");
}

void test_Person(){
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_Person(L);
    run_lua_file(L,"test.lua");
}

int main(){
    //test_mylib();
    test_Person();
}

 

test.lua:

 

print("test person")

person = Person.new()
person:setName("newName")
print(person)
person:delete();

  

 

转载于:https://www.cnblogs.com/SillyFlame/p/5139273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值