#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. Ubuntu下的计算器

    今天计算乘法时居然给算错了,好囧. 于是开始使用ubuntu自带的计算器:gcalctool 使用方法: 在终端直接输入命令gcalctool即可.

  2. office 2013母版保存并调用

    如果觉得某个ppt的母版不错,想保存下来以后使用的话,那么执行 开始->另存为->  选择位置和格式,注意格式选择potx. 之后如果想要使用这组母版,怎么办呢? 浏览主题,打开之前保存的 ...

  3. 微信跳一跳,Python辅助自动跳程序

    一.说明 此代码借鉴一位大神提供在gitHub上的源码,已经做了简化合并处理,成功连上手机并运行后,可实现自动玩微信跳一跳游戏,刷个1000+的分数轻轻松松 github源码地址 https://gi ...

  4. Linux中epoll+线程池实现高并发

    服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下,每个线程 ...

  5. windows默认文件浏览器大小

    windows打开之后,发现默认窗口很小.

  6. C#使用WebService

    一.新建webservice 新建项目→asp.net Web服务应用程序 或者在现有项目中 点击右键 新建web服务程序asmx 只要在webservice类里面 的方法 标注为[WebMethod ...

  7. Log4j(3)--rootLogger根配置和appender输出类型配置

    参考博客:http://blog.java1234.com/blog/articles/270.html 一.rootLogger根配置: Log4j 根配置语法 log4j.rootLogger = ...

  8. Linux下搭建企业共享目录方案之------samba

    Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...

  9. 编译安装php-5.4.44

    编译安装php-5.4.44 1. 首先,安装必要的库文件,一面编译被打断: yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-de ...

  10. 安装face_recognition

    Ubuntu安装face_recognition需要先安装dlib 1.安装dlib的依赖 sudo apt-get install build-essential cmake sudo apt-ge ...