#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, LUA_REGISTRYINDEX, 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, value);
}
static void Lua_Pack(lua_State* L, float value)
{
lua_pushnumber(L, value);
}
static void Lua_Pack(lua_State* L, double value)
{
lua_pushnumber(L, value);
}
static void Lua_Pack(lua_State* L, const char* value)
{
lua_pushstring(L, value);
}
static void Lua_Pack(lua_State* 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, -1);
lua_pop(L, 1);
return 1;
}
return 0;
} static int Lua_Unpack(lua_State* L, float& value)
{
if (lua_isnumber(L, -1))
{
value = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
return 1;
}
return 0;
}
static int Lua_Unpack(lua_State* L, double& value)
{
if (lua_isnumber(L, -1))
{
value = lua_tonumber(L, -1);
lua_pop(L, 1);
return 1;
}
return 0;
} static int Lua_Unpack(lua_State* L, const char* value)
{
if (lua_isstring(L, -1))
{
value = lua_tostring(L, -1);
lua_pop(L,1);
return 1;
}
return 0;
} static int Lua_Unpack(lua_State* 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 T2, typename... Args>
static void Lua_Unpack(lua_State* L, T1& value1, T2& value2, Args&... args)
{
Lua_Unpack(L, value2, 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 index, int i)
{
if (lua_istable(L, index))
{
lua_rawgeti(L, index, i);
return 1;
}
return 0;
} template<typename T1,typename T2,typename... Args>
static int Lua_GetField(lua_State* L, int index, T1& t1, T2& t2, Args& ... args)
{
int ret = Lua_GetField(L, index, t1) + Lua_GetField(L, index, t2, args...);
return ret;
} /**********************************************************/
static int Lua_TopField(lua_State* L, const char* key)
{
if (lua_istable(L, -1))
{
lua_pushstring(L, key);
lua_rawget(L, -1);
return 1;
}
return 0;
} static int Lua_TopField(lua_State* L, int i)
{
if (lua_istable(L, -1))
{
lua_rawgeti(L, -1, i);
return 1;
}
return 0;
} template<typename T1,typename T2,typename... Args>
static int Lua_TopField(lua_State* L, T1& t1, T2& t2, Args... args)
{
int ret = Lua_TopField(L, t1) + Lua_TopField(L, t2, args...);
return ret;
} static bool Lua_CallFunc(lua_State* L, int rt)
{
if (!lua_isfunction(L, -1))
return false;
if (lua_pcall(L, 0, rt, 0) != 0)
{
const char* err = lua_tostring(L, -1);
printf("call lua func failed!!!, err:%s\n", err);
lua_pop(L, 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, rt, 0) != 0 )
{
const char* err = lua_tostring(L, -1);
printf("call lua func failed!!!, err:%s\n",err);
lua_pop(L, 1);
return false;
}
return true;
} #endif

  

lua简单包装的更多相关文章

  1. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  2. lua简单入门

    一.安装windows下的lua环境,luaforwindows 下载最新的5.3版本,下载地址: https://sourceforge.net/projects/luabinaries/files ...

  3. SpringBoot+RestTemplate 简单包装

        RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类.     在SpringBoot中对这个类进行 ...

  4. Lua刚開始学习的人(一)--Lua 简单教学

    近期因为工作原因.临时木有<Oracle起步学习>续集.领导知道学习下Lua脚本语言.看了一周了.趁热打铁,留下点实用的东西吧. 本系列会主要针对宿主语言为 Delphi,原理都是一样的, ...

  5. [转]通过AngularJS directive对bootstrap日期控件的的简单包装

    本文转自:http://www.cnblogs.com/Benoly/p/4109460.html 最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的 ...

  6. [整理]通过AngularJS directive对bootstrap日期控件的的简单包装

    最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形 ...

  7. 基于nginx+lua简单的灰度发布系统

    upstream.conf upstream grey_1 { keepalive 1000; server localhost:8020; } upstream grey_2 { keepalive ...

  8. 正确lua简单的扩展,可以加速相关C++数据。

    很早的时候,我有一件事纠结.如果,我在这里C++打开界面脚本.使用C++其中一个目标,和.我的程序有很多不同的lua虚拟机.每个虚拟机与一个关联C++对象,它是多线程,那么如何快速应利用这个好时机lu ...

  9. lua学习笔记10:lua简单的命令行

    前面反复使用的命令行,好学喜欢命令行: 一 格公式 lua [options][script][args] 两 详细命令 -e 直接命令传递一个lua -l 加载文件 -i 进入交互模式 比例如.端子 ...

随机推荐

  1. iis6 , URL重写HTM文件名后,出现真实的HTM文件不能访问的解决

    服务器环境是windows 2003 IIS6 在web.config文件中加入 1.在<compilation debug="true"> 节点加入 <buil ...

  2. CSS 中文字体的英文名称 (simhei, simsun) 宋体 微软雅黑等

    Mac OS的一些:   华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 俪黑 P ...

  3. ZedGraph控件的使用 --归类(转帖)

    在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGrap ...

  4. CentOS 6.5 下搭建NTP服务器

    参考网站: http://www.iyunv.com/thread-64847-1-1.html http://acooly.iteye.com/blog/1993484 1         检查系统 ...

  5. Android wm指令用法详解

    wm 是查看和设置显示信息的指令,此指令只能临时调试使用. wm:查看 wm 指令信息 $ adb shell root@rk3288:/ # wm wm usage: wm [subcommand] ...

  6. Renesas PPP Mode

    不同厂家的NB/4G/2G,配置PPP模式略有差异,回调配置在如下路径: “synergy\ssp_supplemental\src\framework\sf_cellular_cat3\sf_cel ...

  7. postman 前置 和 后置 处理器 用法

    基本用法 赋予变量 var  body="我是变量的值" ;   -----给body赋值 postman.setEnvironmentVariable("sign&qu ...

  8. 2018-2019-1 20165226《信息安全系统设计基础》 pwd命令的实现

    2018-2019-1 20165226<信息安全系统设计基础> pwd命令的实现 一.学习pwd 查看pwd 得知一个嫩过去文件路径的函数--getcwd i节点值 通过ls -i -a ...

  9. [saiku] schema文件分析

    上一篇讲到了如何在管理台配置数据源 [ http://www.cnblogs.com/avivaye/p/4877767.html ] 这次来说明下shema文件里面是怎样配置Cube和角色权限的 通 ...

  10. [Python] map Method

    Map applies a function to all the items in an input_list Blueprint map(function, list_of_inputs) Mos ...