使用luac API从表中提取用户数据
Extract userdata from table,using lua c API
我使用luacapi来循环表中的变量,比如这个
lua脚本:
array = {0,1,2,3}
lua c api
lua_getglobal(l, "array");
if(lua_isnil(l, -1)) {
}
lua_pushnil(l);
while(lua_next(l, -2)) {
int value=(int)lua_tonumber(l, -1);
printf("%d n",value);
lua_pop(l, 1);
}
我可以得到的结果
01.2.3
然后我想在表中放入一些用户数据对象然后在c api 中循环它们
lua脚本
foo0 = Foo.new("fred0")
foo1 = Foo.new("fred0")
foo2 = Foo.new("fred0")
foo3 = Foo.new("fred0")
array = {foo0,foo1,foo2,foo3}
lua c api
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <iostream>
#include <sstream>
#include <vector>
class Foo
{
public:
Foo(const std::string & name) : name(name)
{
std::cout << "Foo is born" << std::endl;
}
std::string Add(int a, int b)
{
std::stringstream ss;
ss << name << ": " << a << " + " << b << " = " << (a+b);
return ss.str();
}
~Foo()
{
std::cout << "Foo is gone" << std::endl;
}
std::string name;
};
int l_Foo_constructor(lua_State * l)
{
const char * name = luaL_checkstring(l, 1);
Foo ** udata = (Foo **)lua_newuserdata(l, sizeof(Foo *));
*udata = new Foo(name);
luaL_getmetatable(l, "luaL_Foo");
lua_setmetatable(l, -2);
return 1;
}
Foo * l_CheckFoo(lua_State * l, int n)
{
return *(Foo **)luaL_checkudata(l, n, "luaL_Foo");
}
int l_Foo_add(lua_State * l)
{
return 1;
}
int l_Foo_destructor(lua_State * l)
{
Foo * foo = l_CheckFoo(l, 1);
delete foo;
return 0;
}
void RegisterFoo(lua_State * l)
{
luaL_Reg sFooRegs[] =
{
{ "new", l_Foo_constructor },
{ "add", l_Foo_add },
{ "__gc", l_Foo_destructor },
{ NULL, NULL }
};
luaL_newmetatable(l, "luaL_Foo");
luaL_register(l, NULL, sFooRegs);
lua_pushvalue(l, -1);
lua_setfield(l, -1, "__index");
lua_setglobal(l, "Foo");
}
int main()
{
lua_State * l = luaL_newstate();
luaL_openlibs(l);
RegisterFoo(l);
int erred = luaL_dofile(l,"/Volumes/Work/CODE/Test/testStatic/testStatic/kami.txt");
if(erred)
std::cout << "Lua error: " << luaL_checkstring(l, -1) << std::endl;
lua_getglobal(l, "array");
if(lua_isnil(l, -1)) {
//return std::vector();
}
lua_pushnil(l);
std::vector<Foo *> v;
while(lua_next(l, -2)) {
Foo * foo = l_CheckFoo(l, -1);//this line do not work
//
//
//
//
//I don't know how to do it here.
//
//
//
//
//v.push_back(foo);
lua_pop(l, 1);
}
// for (Foo* theValue:v)
// {
// printf("==>%s",theValue->name.c_str());
// }
lua_close(l);
return 0;
}
如何从表中提取用户数据?请帮帮我,谢谢。
最后我自己解决了
l_CheckFoo(l, -1);
应该是
lua_touserdata(l, -1);
void * hehe=lua_touserdata(l, -1);
Foo ** haha=(Foo **)hehe;
Foo *f=*haha;
printf("%s n",f->name.c_str());
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何从 std::atomic 中提取指针 T<T>?
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 为什么istream不支持右值提取
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 从用户输入中提取整数的枚举开关语句
- 使用luac API从表中提取用户数据
- 如何使用c / c ++在OpenLDAP中提取用户的组成员身份
- 使用getline(),然后从用户输入中提取字符串(使用c++)
- c++有效地从用户定义结构的向量中提取子集