C++与lua(五)

5 篇文章 0 订阅
本文介绍了如何使用C++和Lua进行交互,通过定义Student类及其成员函数,并提供了一系列Lua API,如设置年龄、获取年龄、设置姓名和获取姓名。实例演示了如何在Lua环境中创建、操作Student对象及其生命周期管理。
摘要由CSDN通过智能技术生成

main.cpp

#include <stdio.h>
#include "lua.hpp"
#include <string.h>
#include <string>
#include <iostream>
 
using namespace std;

class Student
{
public:
	Student():m_age(0), m_name(""){}
	~Student(){}

	int m_age;
	string m_name;

	void setAge(int age){m_age = age;}
	int  getAge(){return m_age;}

	void setName(const char* name){m_name = name;}
	const char* getName(){return m_name.c_str();}
};

int Lua_CreateStudent(lua_State *L)
{
	Student **s = (Student**)lua_newuserdata(L, sizeof(Student*));
	*s = new Student;

	// 元表
	luaL_getmetatable(L, "Meta_Student");
    lua_setmetatable(L, -2);
    
	return 1;
}

int Lua_SetAge(lua_State *L)
{
	Student **s = (Student**)luaL_checkudata(L, -2, "Meta_Student");
    luaL_argcheck(L, s != NULL, -2, "invalid user data");
    luaL_checktype(L, -1, LUA_TNUMBER);
    int age = lua_tonumber(L, -1);
    (*s)->setAge(age);
    return 0;
}

int Lua_GetAge(lua_State *L)
{
	Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
    luaL_argcheck(L, s != NULL, -1, "invalid user data");
    lua_settop(L, 0);
    lua_pushnumber(L, (*s)->getAge());
    return 1;
}

int Lua_SetName(lua_State *L)
{
	Student **s = (Student**)luaL_checkudata(L, -2, "Meta_Student");
    luaL_argcheck(L, s != NULL, -2, "invalid user data");
    
    luaL_checktype(L, -1, LUA_TSTRING);
    
    const char* name = lua_tostring(L, -1);
    (*s)->setName(name);
    return 0;
}

int Lua_GetName(lua_State *L)
{
	Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
    luaL_argcheck(L, s != NULL, -1, "invalid user data");
    lua_settop(L, 0);
    lua_pushstring(L, (*s)->getName());
    return 1;
}

int Lua_Destory(lua_State *L)
{
	Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
    luaL_argcheck(L, s != NULL, -1, "invalid user data");
    delete (*s);

    lua_settop(L, 0);
    return 0;
}

int LuaOpenLibStudent(lua_State *pLua)
{
	static const struct luaL_Reg studentlib_meta[] = {
	    {"setAge", Lua_SetAge},
	    {"getAge", Lua_GetAge},
	    {"setName",Lua_SetName},
	    {"getName",Lua_GetName},
	    {NULL, NULL}
	};
	// 元表
	luaL_newmetatable(pLua, "Meta_Student");
	lua_pushvalue(pLua, -1);
	lua_setfield(pLua, -2, "__index");
	lua_pushcfunction(pLua, Lua_Destory);
	lua_setfield(pLua, -2, "__gc");
	// // getAge方法
	// lua_pushcfunction(pLua, Lua_GetAge);
	// lua_setfield(pLua, -2, "getAge");
 
	// // setAge方法
	// lua_pushcfunction(pLua, Lua_SetAge);
	// lua_setfield(pLua, -2, "setAge");

	// // getName方法
	// lua_pushcfunction(pLua, Lua_GetName);
	// lua_setfield(pLua, -2, "getName");
 
	// // setName方法
	// lua_pushcfunction(pLua, Lua_SetName);
	// lua_setfield(pLua, -2, "setName");
	/*
	luaL_register,参数二,NULL值表示以栈顶table作为存储函数的table
	等于一组或者多组
	lua_pushcfunction
	lua_setfield
	*/
	luaL_register(pLua, NULL, studentlib_meta);
	
	static const struct luaL_Reg studentlib[] = {
	    {"create", Lua_CreateStudent},
	    {NULL, NULL}
	};

	luaL_register(pLua, "Stu", studentlib);

	// 清除栈
	//int n = lua_gettop(pLua);
	//lua_pop(pLua, n);
	// 清除栈
	lua_settop(pLua, 0);

	return 0;
}

int main()
{
	lua_State* pLua = luaL_newstate();
	luaL_openlibs(pLua);
	
	LuaOpenLibStudent(pLua);
	
	char szTmp[512];
	snprintf(szTmp, sizeof(szTmp), "package.path=package.path..';./?.lua;./?.luac'\0");
	if (luaL_dostring(pLua, szTmp) != 0){}
 
	if (luaL_dostring(pLua, "require 'init'") != 0){}
 
	lua_settop(pLua, 0);
	
 	lua_getglobal(pLua, "test");
	lua_pushnumber(pLua, 20);
	lua_pushstring(pLua, "Hello World~!");
	int nRet = lua_pcall(pLua, 2, 0, 0);
	
	lua_close(pLua);
	return 0;
}

init.lua

require "Stu"

function test(intValue, strValue)
	print(intValue, strValue)

	local s = Stu.create()
	s:setName("Hello")
	print(s:getName())
	s:setAge(200)
	print(s:getAge())

	print ("Hello World!")
end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值