lua-users home
lua-l archive

RE: Re[2]: Lua Event driven programming

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


Yes matter of taste. I must admit I use the Lua API as a kind of toolkit in
my C code and I do use a Lua table internally in the same way as you have
from Lua. Of course, the object-oriented version of 'Delegate_List' could be
implemented in pure Lua. As you might guess 'Delegate_List' inherits
generalised list handling methods (like add) from a parent class 'List'.
Providing a __call metamethod for Delegate_List is rather neat, I think,
whether done in C or in Lua and it means from C you simply pcall the
delegate object itself (or treat it like a function in Lua).
-----Original Message-----
From: Bulat Ziganshin [mailto:bulat.ziganshin@gmail.com] 
Sent: 01 June 2009 13:48
To: John Hind
Cc: 'Lua list'
Subject: Re[2]: Lua Event driven programming
Hello John,
Monday, June 1, 2009, 4:22:05 PM, you wrote:
> I created a Lua class implemented in C called "Delegate_List" which
> maintains a list of functions (the event handlers) and a metatable
override
> for the Lua call operation makes a call on objects of this class call all
> the functions in the list in turn
just my two pennies: i hate to do any data manipulation in C
so for handling multiple event handlers i does the same in Lua:
handlers = {}
function add_handler(event,handler)
 handlers[event] = append (handlers[event] or {}, handler)
end
add_handler("event1",
 function(obj)
 -- event1 handler
 end)
-- Called from C in case of event
function event1(obj)
 for _,v in ipairs(handlers.event1 or {})
 v(obj)
 end
end
-- 
Best regards,
 Bulat mailto:Bulat.Ziganshin@gmail.com

AltStyle によって変換されたページ (->オリジナル) /