lua-users home
lua-l archive

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




On 2 April 2011 23:12, C++ RTMP Server <crtmpserver@gmail.com> wrote:
Hi,

I have a special situation in which I have to create an association between my app-specific state and the lua_State. For this I have 3 solutions:

1. create pair of maps: map <MyStateStruct*, lua_State *> and the other way around map <lua_State *, MyStateStruct*>
2. create an unique ID for each MyStateStruct and store that ID into GLOBALINDEX inside lua_State
3. Same as 2, but store MyStateStruct directly into lua_State GLOBALINDEX as user-data

Honestly, I don't like any of them:

1st one introduces a lot of management code and is prone to errors
2nd and 3rd introduces few calls for extracting my MyStateStruct structure

I hope that there is a way to associate a void * pointer with a lua_State so when lua calls C/C++ code I can get my hands on the MyStateStruct without calling additional stuff (get the global index) or indexing through those 2 maps. I was thinking more like simple dereferencing a void * inside lua_State

int luaapi_application_getConfig(lua_State *L) {
       MyStateStruct *pMyState=(MyStateStruct *)L->pMagicUserDataField;
       //do stuff with pMyState
       ....
}

Thank you
------
Eugen-Andrei Gavriloaie
Web: http://www.rtmpd.com



If there is a one to one mapping of application state to lua_State, you can register the luaCfunctions with the upvalue stored as a void pointer. See the section [1] in Programming in Lua which talks about this situation using the registry or upvalues. 

[1] http://www.lua.org/pil/27.3.html

Liam