c++ - Does this script result in many, many closures, and if yes, whats an alternative? -
i want implement gui message handling system in lua, works this:
in c++ code, windows have window procedures, have in windows api, , trying lean on in lua well.
so windows carry luabind::object
points table like
local action = { [on_uimsg.mousemove] = function (ele, a,b,c)return on_mousemove(b,c) end, [on_uimsg.mousedown] = function (ele, a,b,c) return on_mousedown(ele,b) end, [on_uimsg.leave] = function (ele, a,b,c) return on_mouseleave(b,c) end, }
the key table gui message. ele handle window, message again, b , c parameters.
local function on_mousemove(b,c) consoleout2("mousemove %i %i",b,c); return 0; end; local function on_command(b,c) consoleout2("mousecmd %i %i",b,c); return 1; end;
are example functions.
they bound object this:
parms.pos_x = 620; parms.pos_y = 300; parms.width = 100; parms.height = 100; parms.parent = desktop; parms.name = "test"; parms.skin = "default_outline"; parms.class = "basicstatictext"; local window = hud:addwindow(parms,action);
this in same script file, gets (to knowledge) executed once, when load it. @ first table gets constructed, later on table gets bound luabind:::object
. object gets called in c++ this:
if (luabind::type(o)==lua_tfunction) { luabind::call_function<int>(o,handle,a,b,c); } else if (luabind::type(o)==lua_ttable) { luabind::object call = o[a]; if (luabind::type(call)==lua_tfunction) { luabind::call_function<int>(call,handle,a,b,c); } }
so whenever message fired, table gets called , returns function every time, guess. assuming this, because though load script file once, when debug script , put breakpoint there, breakpoint gets hit when action object called.
is way of handling things?
i assume question whether allocate new function every time access table. answer no.
remember: normal function definitions in lua special-cases of unnamed function creation. following identical:
function somename() end somename = function() end
the function created when lua source file executed. table use created once: when lua source file executed. if executed lua source file multiple times, table , contents created multiple times.
yes, code have lot of functions in it. what's wrong that? unless creating functions or doing equally abusive, there's no such thing "too many". lua doesn't have switch
statement precisely because lua code typically things way.
in short: entirely normal lua.
Comments
Post a Comment