使用lua访问STL容器
2010-07-31 14:52
330 查看
最近在做项目时,需要在lua脚本中,对c++的一个容器进行遍历。查了半天的资料,没有一劳永逸的办法。只好使用了较鹾的办法。源码列出来了,等着板砖。
c++代码:
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "luaxlib.h"
}
#pragma comment(lib,"lua51.lib")
#pragma comment(lib,"lua5.1.lib")
#pragma comment(lib,"tolua++.lib")
#define maxnum 10
std::vector<int> globalvec;
static int getmaxnum(Lua_state*L)
{
size_t size = globalvec.size();
lua_pushnumber(L,size);
return 1;
}
static int getvalue(lua_State*L)
{
size_t index= lua_tonumber(L,-1);
if(index > globalvec.size()
{
return 0;
}
lua_pushnumber(L,globalvec[index]);
return 1;
}
static int getvalue(unsigned int index)
{
if(index >= globalvec.size())
{
return 0;
}
return globalvec[index];
}
static int testc()
{
unsigned int index =0;
while(index < globalvec.size())
{
getval(index++);
}
}
main()
{
for(unsigned int i =0;i < maxnum;i ++)
{
globalvec.push_back(i);
}
L= lua_open();
luaopen_base(L):
luaL_ openlibs(L);
luaL_dofile(L,"test.lua");
lua_register(L,"getmaxindex",getmaxindex);
lua_register(L,"getvalue",getvalue);
LARGE_INTER startime1,starttime2,endtime1,endtime2;
LARGE_INTER frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&starttime1);
lua_getglobal(L,testfunc);
lua_call(L,0,0);
QueryPerformanceCounter(&endtime1);
QueryPerformanceCounter(&starttime2);
testc();
QueryPerformanceCounter(&endtime2);
float interval1 = (1000 * endtime1.QuardPart - 1000 *starttime1.QuardPart)/frequency.QuardPart;
float interval2 = (1000 * endtime2.QuardPart - 1000 * starttime.QuardPart)/frequencey.QuardPart;
lua_close(L):
return 1;
}
lua脚本:
function testfunc()
local index =0
local maxindex = getmaxindex()
local value =0
while index < maxindex do
value = getvalue(index)
index = index+1
print(value)
end
最后测下来lua效果还不错,放心大胆的使用了
c++代码:
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "luaxlib.h"
}
#pragma comment(lib,"lua51.lib")
#pragma comment(lib,"lua5.1.lib")
#pragma comment(lib,"tolua++.lib")
#define maxnum 10
std::vector<int> globalvec;
static int getmaxnum(Lua_state*L)
{
size_t size = globalvec.size();
lua_pushnumber(L,size);
return 1;
}
static int getvalue(lua_State*L)
{
size_t index= lua_tonumber(L,-1);
if(index > globalvec.size()
{
return 0;
}
lua_pushnumber(L,globalvec[index]);
return 1;
}
static int getvalue(unsigned int index)
{
if(index >= globalvec.size())
{
return 0;
}
return globalvec[index];
}
static int testc()
{
unsigned int index =0;
while(index < globalvec.size())
{
getval(index++);
}
}
main()
{
for(unsigned int i =0;i < maxnum;i ++)
{
globalvec.push_back(i);
}
L= lua_open();
luaopen_base(L):
luaL_ openlibs(L);
luaL_dofile(L,"test.lua");
lua_register(L,"getmaxindex",getmaxindex);
lua_register(L,"getvalue",getvalue);
LARGE_INTER startime1,starttime2,endtime1,endtime2;
LARGE_INTER frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&starttime1);
lua_getglobal(L,testfunc);
lua_call(L,0,0);
QueryPerformanceCounter(&endtime1);
QueryPerformanceCounter(&starttime2);
testc();
QueryPerformanceCounter(&endtime2);
float interval1 = (1000 * endtime1.QuardPart - 1000 *starttime1.QuardPart)/frequency.QuardPart;
float interval2 = (1000 * endtime2.QuardPart - 1000 * starttime.QuardPart)/frequencey.QuardPart;
lua_close(L):
return 1;
}
lua脚本:
function testfunc()
local index =0
local maxindex = getmaxindex()
local value =0
while index < maxindex do
value = getvalue(index)
index = index+1
print(value)
end
最后测下来lua效果还不错,放心大胆的使用了
相关文章推荐
- 多线程中使用迭代器 访问 STL 容器元素的越界问题
- Lua使用C++中的类——调用类方法并访问类变量
- C++ STL基本容器的使用
- STL中一些容器的使用
- STL库中几种容器及迭代器的使用
- C++STL容器使用经验总结
- 使用stl标准容器,遍历容器时出现 iterators incompatible 错误的其中两种原因
- STL学习笔记之使用“交换技巧”来修整顺序容器的过剩容量
- C++ STL基本容器的使用
- 例程讲解关于图像处理程序中文件操作时对于STL容器的使用
- OpenResty的安装和在nginx中使用lua直接访问mysql达到数据接口的统一
- 使用STL容器构成一棵树
- STL:STL各种容器的使用时机详解
- 逆序存储小文件(一)——使用STL容器
- STL-list容器使用
- stl string 容器的使用
- **c++ stl 顺序容器: begin(),end()和front(),back()区别和使用**
- docker使用——容器不能访问外网
- C++ 之高效使用STL (STL 容器的选择)
- 使用GDB调试STL容器