lua简单包装
发布时间:2020-12-14 21:52:01 所属栏目:大数据 来源:网络整理
导读:#ifndef _LUA_WRAPPER_#define _LUA_WRAPPER_extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}#include vector#include string#include assert.hstatic size_t Lua_GetSize(lua_State* L,int pos){ if (lua_istable(L,pos)) { return l
#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: %sn",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:%sn",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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |