lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks for the reply. I knew it probably is something like this and thanks for confirming me with that. But it seems a little bit hard, given many of this kind of tables need to be imported to C. I like tolua, which exposes C structures automaticlly to Lua. Is there a equivalent to do the reverse? Or everyone use this direct importing function?
 
Thanks
 
John

Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> What's the best way to fill a userdata with a table?

You can set metamethods for this. But if you're using "config{...}" in Lua,
then it is easier to write a C function exported as "config" that does the job.

It would be something like this (untested):

static CConfig myconfig;

static int do_config(lua_State *L)
{
lua_pushliteral(L,"length"); lua_gettable(L,1);
myconfig.level=lua_tonumber(L,-1);
lua_pushliteral(L,"width"); lua_gettable(L,1);
myconfig.width=lua_tonumber(L,-1);
lua_pushliteral(L,"red"); lua_gettable(L,1);
myconfig.red=strdup(lua_tonumber(L,-1));
return 0;
}

I'm not sure what you need to do to copy the value of "red"; I've just used
strdup, which I'm not sure works for C++ strings.

Also, there is no error handling, but I hope you get the idea.
--lhf



Do You Yahoo!?
150万曲MP3疯狂搜,带您闯入音乐殿堂
美女明星应有尽有,搜遍美图、艳图和酷图
1G就是1000兆,雅虎电邮自助扩容!