move eventable to util and rename common to util
This commit is contained in:
37
lib/util/eventable.rb
Normal file
37
lib/util/eventable.rb
Normal file
@ -0,0 +1,37 @@
|
||||
# A simple event registering/triggering module to mix into classes.
|
||||
# Events are stored in the `@events` ivar.
|
||||
module Eventable
|
||||
|
||||
# Risc a handler for the given event name.
|
||||
# The event name is the method name called on the handler object
|
||||
#
|
||||
# obj.on(:foo , some_object_that_implements foo( whateverargs)
|
||||
#
|
||||
# @param [String, Symbol] name event name
|
||||
# @param [Object] object handling the event, ie implement the function name
|
||||
# @return handler
|
||||
def register_event(name, handler)
|
||||
event_table[name] << handler
|
||||
handler
|
||||
end
|
||||
|
||||
def unregister_event(name, handler)
|
||||
event_table[name].delete handler
|
||||
end
|
||||
|
||||
def event_table
|
||||
return @event_table if @event_table
|
||||
@event_table = Hash.new { |hash, key| hash[key] = [] }
|
||||
end
|
||||
|
||||
# Trigger the given event name and passes all args to each handler
|
||||
# for this event.
|
||||
#
|
||||
# obj.trigger(:foo)
|
||||
# obj.trigger(:foo, 1, 2, 3)
|
||||
#
|
||||
# @param [String, Symbol] name event name to trigger
|
||||
def trigger(name, *args)
|
||||
event_table[name].each { |handler| handler.send( name.to_sym , *args) }
|
||||
end
|
||||
end
|
65
lib/util/list.rb
Normal file
65
lib/util/list.rb
Normal file
@ -0,0 +1,65 @@
|
||||
module Common
|
||||
module List
|
||||
|
||||
# set the next instruction (also aliased as <<)
|
||||
# throw an error if that is set, use insert for that use case
|
||||
# return the instruction, so chaining works as one wants (not backwards)
|
||||
def set_next( nekst )
|
||||
raise "Next already set #{@next}" if @next
|
||||
@next = nekst
|
||||
nekst
|
||||
end
|
||||
|
||||
# during translation we replace one by one
|
||||
# TODO avoid this by building a new list
|
||||
# Make arm instruction not derive from risc (which is weird anyway)
|
||||
# and include the List into it, translate in same way we go from mom->risc
|
||||
def replace_next( nekst )
|
||||
old = @next
|
||||
@next = nekst
|
||||
@next.append old.next if old
|
||||
end
|
||||
|
||||
# get the next instruction (without arg given )
|
||||
# when given an interger, advance along the line that many time and return.
|
||||
def next( amount = 1)
|
||||
(amount == 1) ? @next : @next.next(amount-1)
|
||||
end
|
||||
|
||||
# set the give instruction as the next, while moving any existing
|
||||
# instruction along to the given ones's next.
|
||||
# ie insert into the linked list that the instructions form
|
||||
# but allowing the instruction to be a list too (ie more than one)
|
||||
def insert( instruction )
|
||||
instruction.last.set_next @next
|
||||
@next = instruction
|
||||
end
|
||||
|
||||
# return last set instruction. ie follow the linked list until it stops
|
||||
def last
|
||||
code = self
|
||||
code = code.next while( code.next )
|
||||
return code
|
||||
end
|
||||
|
||||
# set next for the last (see last)
|
||||
# so append the given code to the linked list at the end
|
||||
def append( code )
|
||||
last.set_next code
|
||||
self
|
||||
end
|
||||
alias :<< :append
|
||||
|
||||
def length
|
||||
ret = 0
|
||||
self.each { ret += 1}
|
||||
ret
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
block.call(self)
|
||||
@next.each(&block) if @next
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user