lua简单包装

lua简单包装,第1张

概述#ifndef _LUA_WRAPPER_#define _LUA_WRAPPER_extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}#include <vector>#include <string>#include <assert.h>static siz
#ifndef _LUA_WRAPPER_#define _LUA_WRAPPER_extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}#include <vector>#include <string>#include <assert.h>static size_t Lua_GetSize(lua_State* L,int pos){    if (lua_istable(L,pos))    {        return lua_rawlen(L,pos);    }    return 0;}static voID Lua_Remove(lua_State* L,int n){    if (n > 0)    {        lua_pop(L,n);    }    else if (n < 0)    {        int top = lua_gettop(L);        lua_pop(L,top);    }}static int Lua_CreateRef(lua_State* L){    if (lua_isfunction(L,-1))    {        return luaL_ref(L,LUA_REGISTRYINDEX);    }    return 0;}static voID Lua_DeleteRef(lua_State* L,int ref){    lua_rawgeti(L,LUA_REGISTRYINDEX,ref);    luaL_unref(L,ref);}/*****************************************************************/static voID Lua_Pack(lua_State * L,int value){    lua_pushinteger(L,value);}static voID Lua_Pack(lua_State* L,size_t value){    lua_pushinteger(L,float value){    lua_pushnumber(L,double value){    lua_pushnumber(L,const char* value){    lua_pushstring(L,const std::string& value){    lua_pushlstring(L,value.c_str(),value.length());}template<class T>static voID Lua_Pack(lua_State* L,const std::vector<T>& value){    lua_newtable(L);    for (size_t i = 0; i < value.size(); ++i)    {        Lua_Pack(L,value[i]);        lua_rawseti(L,-2,i+1);    }}template<typename T1,typename T2,typename... Args>static voID Lua_Pack(lua_State* L,const T1& value1,const T2& value2,Args&... args){    Lua_Pack(L,value1);    Lua_Pack(L,value2,args...);}/*******************************************************/    static int Lua_Unpack(lua_State* L,int& value){    if (lua_isnumber(L,-1))    {        value = (int)lua_tointeger(L,-1);        lua_pop(L,1);        return 1;    }    return 0;}static int Lua_Unpack(lua_State* L,size_t& value){    if (lua_isnumber(L,-1))    {        value = (size_t)lua_tointeger(L,float& value){    if (lua_isnumber(L,-1))    {        value = (float)lua_tonumber(L,1);        return 1;    }    return 0;}static int Lua_Unpack(lua_State* L,double& value){    if (lua_isnumber(L,-1))    {        value = lua_tonumber(L,const char* value){    if (lua_isstring(L,-1))    {        value = lua_tostring(L,std::string& value){    if (lua_isstring(L,-1))    {        const char* str = lua_tostring(L,-1);        value.append(str);        lua_pop(L,1);        return 1;    }    return 0;}template<class T>static voID Lua_Unpack(lua_State* L,std::vector<T>& value){    T t;    if (!lua_istable(L,-1))        return 0;    size_t len = Lua_GetSize(L,-1);    for (size_t i = 1; i <= len; ++i)    {        lua_rawgeti(L,-1,i);        if (Lua_Unpack(L,t))            value.push_back(t);        else            lua_pop(L,1);    }    lua_pop(L,1);    return 1;}template<typename T1,typename... Args>static voID Lua_Unpack(lua_State* L,T1& value1,T2& value2,Args&... args){    Lua_Unpack(L,args...);    Lua_Unpack(L,value1);}/*******************************************/static bool Lua_Loadfile(lua_State* L,const char* filename){    if (luaL_dofile(L,filename))    {        const char* err = lua_tostring(L,-1);        printf("loadl file %s Failed,err: %s\n",filename,err);        lua_pop(L,1);        return false;    }    return true;}static voID Lua_Split(const char* str,char c,std::vector<std::string>& result){    const char* pre = str;    while (*str != 0)    {        while (*str && *str != c) ++str;        result.push_back(std::string(pre,str));        pre = *str ? ++str : str;    }}/*****************************************************************/static int Lua_GetFIEld(lua_State* L,int index,const char* key) {    if (lua_istable(L,index))    {        lua_pushstring(L,key);        lua_rawget(L,index);        return 1;    }    return 0;}static int Lua_GetFIEld(lua_State* L,int i){    if (lua_istable(L,index))    {        lua_rawgeti(L,index,i);        return 1;    }    return 0;}template<typename T1,typename... Args>static int Lua_GetFIEld(lua_State* L,T1& t1,T2& t2,Args& ... args){    int ret = Lua_GetFIEld(L,t1) + Lua_GetFIEld(L,t2,args...);    return ret;}/**********************************************************/static int Lua_topFIEld(lua_State* L,const char* key){    if (lua_istable(L,-1))    {        lua_pushstring(L,-1);        return 1;    }    return 0;}static int Lua_topFIEld(lua_State* L,-1))    {        lua_rawgeti(L,typename... Args>static int Lua_topFIEld(lua_State* L,Args... args){    int ret = Lua_topFIEld(L,t1) + Lua_topFIEld(L,args...);    return ret;}static bool Lua_CallFunc(lua_State* L,int rt){    if (!lua_isfunction(L,-1))        return false;    if (lua_pcall(L,rt,0) != 0)    {        const char* err = lua_tostring(L,-1);        printf("call lua func Failed!!!,err:%s\n",1);        return false;    }    return true;}template<typename T,typename... Args>static bool Lua_CallFunc(lua_State* L,int rt,T& t,Args&... args){    const int len = sizeof...(args) + 1;    if (!lua_isfunction(L,-len))        return false;        Lua_Pack(L,t,args...) ;    if (lua_pcall(L,len,0) != 0 )    {        const char* err = lua_tostring(L,1);        return false;    }    return true;}#endif
总结

以上是内存溢出为你收集整理的lua简单包装全部内容,希望文章能够帮你解决lua简单包装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234777.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存