lua简单包装
#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简单包装的更多相关文章
- 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 ...
- lua简单入门
一.安装windows下的lua环境,luaforwindows 下载最新的5.3版本,下载地址: https://sourceforge.net/projects/luabinaries/files ...
- SpringBoot+RestTemplate 简单包装
RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类. 在SpringBoot中对这个类进行 ...
- Lua刚開始学习的人(一)--Lua 简单教学
近期因为工作原因.临时木有<Oracle起步学习>续集.领导知道学习下Lua脚本语言.看了一周了.趁热打铁,留下点实用的东西吧. 本系列会主要针对宿主语言为 Delphi,原理都是一样的, ...
- [转]通过AngularJS directive对bootstrap日期控件的的简单包装
本文转自:http://www.cnblogs.com/Benoly/p/4109460.html 最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的 ...
- [整理]通过AngularJS directive对bootstrap日期控件的的简单包装
最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形 ...
- 基于nginx+lua简单的灰度发布系统
upstream.conf upstream grey_1 { keepalive 1000; server localhost:8020; } upstream grey_2 { keepalive ...
- 正确lua简单的扩展,可以加速相关C++数据。
很早的时候,我有一件事纠结.如果,我在这里C++打开界面脚本.使用C++其中一个目标,和.我的程序有很多不同的lua虚拟机.每个虚拟机与一个关联C++对象,它是多线程,那么如何快速应利用这个好时机lu ...
- lua学习笔记10:lua简单的命令行
前面反复使用的命令行,好学喜欢命令行: 一 格公式 lua [options][script][args] 两 详细命令 -e 直接命令传递一个lua -l 加载文件 -i 进入交互模式 比例如.端子 ...
随机推荐
- HDU2888 Check Corners(二维RMQ)
有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...
- 关于Android应用开发的一些安全注意事项
原文地址: http://www.javacodegeeks.com/2014/05/simple-tips-to-secure-android-app.html ...
- SublimeText设置在浏览器打开 快捷键
这里插入一下安装"view in browser"官方版的说明:(前提是得先安装package control插件) 1.通过"ctrl+shift+p"打开命 ...
- socket编程---SCTP
sctp_sndrcvinfo结构体 sctp_event_subscribe结构体 更多的关于SCTP的结构体http://aisxyz.iteye.com/blog/2408978 SCTP套接字 ...
- pthread线程初始化(pthread_once)
pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...
- hibernate enum映射详解
hibernate enum映射详解 在这里介绍注解的形式,如果想要了解XML配置的方式,可以自行查找相关资料. 例如以下Entity @Entity @Table(name = "t_us ...
- Oracle.DataAccess.dll 部署安装
Oracle.DataAccess.dll 要拷贝到项目发布目录 项目发布的时候,还必须要拷贝以下几个文件在运行目录1.oci.dll 2.oraociicus11.dll 3.OraOps11w.d ...
- NHibernate ConfORM Mapping
前言 昨天写了一篇fluent nhibernate通过约定的代码映射方式,NH在3.0版本以后已经集成了conform的代码映射方式,一直没注意也没使用过,今天试试怎么样. 步骤 1.通过confo ...
- log4net 极简配置
log4net的配置详解 分类: C#2013-10-01 22:45 5335人阅读 评论(4) 收藏 举报 log4net日志框架 前言:没买到1号回家的票,所以在祖国64岁生日之 ...
- Splunk安装部署基础篇
Splunk安装(以4.3.4版本为例) 下载splunk软件包,并解压,直接启动即可. --Linux-x86_64.tgz -C /optcd /opt/splunk/bin./splunk st ...