Binding C++ methods and variables to a table, side by side -


i've been attempting create "host" application exposes c++ api lua code, , have been successful far, have run snags when attempting bind "variables" in methods.

the pattern have developed bind "classes" lua involves each class having _new , _gc function, static lual_reg regdata[] each class. can assign desired functions regdata array, , call helper method bind them lua. following code illustrating method:

int host::_new(lua_state * ls) {     host ** udata = (host **)lua_newuserdata(ls, sizeof(host *);     *udata = new host();      lual_getmetatable(ls, "lual_host);     lua_setmetatable(ls, -2);      return 1; }  int host::_gc(lua_state * ls) {     host * host = *(host **)lual_checkudata(ls, 1, "lual_host");     delete host;     return 0; }  const lual_reg host::registrationdata[] = {     { "new"  , host::_new },     { "__gc" , host::_gc  },     { 0      , 0          } }; 

and elsewhere:

void luastate::registerobject(const char * name, const lual_reg data[]) {     int len = strlen(name) + 6;     char * lualname = new char[len];     snprintf(lualname, len, "lual_%s", name);      // create metatable proper functions     lual_newmetatable(_state, lualname);     lual_setfuncs(_state, data, 0);      // copy metatable on stack     lua_pushvalue(_state, -1);     // assign index copy     lua_setfield(_state, -1, "__index");      // expose table global "host"     lua_setglobal(_state, name);      delete lualname; } 

lets want lua code able view keyboard state well, , lua code access like:

host = host.new() pressed = host.keyboard.getkeypressed(1) 

it becomes easy enough copy exact same pattern keyboard class setup keyboard table, can't seem come way add keyboard table host table. there simple way without messing pattern? there better pattern should using?

i've found plenty of resources on how create various different aspects of c-lua apis, haven't found in terms of patterns/best practices use while doing so. also, know there libraries, such luabind, can this, prefer make own (at least first time).

i've decided take more oo/inheritance approach problem, , plan on taking @ how moai sdk implements lua api, looks promising far.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -