- Joined
- Sep 9, 2007
- Messages
- 6,759
Read the header, like always
JASS:
// --------------------------------------------------------------------------------------------------------------------
//
// Event
// =============
//
// Version: 1.0.0
// Author: Anachron
//
// Requirements:
// Stack [by Anachron]
// (New) Table [by Bribe] (v. 3.1) [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//
// Description:
// This is a custom library to define and trigger your self made
// events with dynamically changeable code pieces.
//
// History:
// 1.0.0: Initial Release
//
// API:
// ------------------------------------------------------------------------------------------------
// Methods
// ------------------------------------------------------------------------------------------------
// fire() --> Trigger the event and all attached codes,
// after that it will trigger all childs
//
// attach(code x) --> Add a code block to the current event
// detach(code x) --> Remove a code block from the current event
// clear() --> Clear all code blocks from the event
//
// adopt(Event x) --> Add a child event to this event
// orphane(Event x) --> Remove child event from this event
// abort() --> Clear all child events from this event
//
// --------------------------------------------------------------------------------------------------------------------------
library Event requires Table, Stack
module IsEvent
private Table BoolHandles = 0
private Stack BoolIndex = 0
private Stack ChildIndex = 0
private trigger Invoker = null
public static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set .BoolHandles = Table.create()
set .BoolIndex = Stack.create()
set .ChildIndex = Stack.create()
set .Invoker = CreateTrigger()
return this
endmethod
public method adopt takes thistype theChild returns boolean
if .ChildIndex.has(theChild) then
return false
endif
return .ChildIndex.add(theChild)
endmethod
public method orphane takes thistype theChild returns boolean
if not .ChildIndex.has(theChild) then
return false
endif
return .ChildIndex.delete(theChild)
endmethod
public method abort takes nothing returns nothing
call .ChildIndex.clear()
endmethod
private static method execute takes thistype this returns boolean
local boolean did = TriggerEvaluate(.Invoker)
call .ChildIndex.reset()
loop
exitwhen not .ChildIndex.hasNext()
call thistype(.ChildIndex.getNext()).fire()
endloop
return did
endmethod
public stub method fire takes nothing returns nothing
call thistype.execute(this)
endmethod
public method attach takes code theCode returns boolean
local boolexpr boolExpr = Condition(theCode)
local integer boolExprId = GetHandleId(boolExpr)
if .BoolIndex.has(boolExprId) then
return false
endif
set .BoolHandles.triggercondition[boolExprId] = TriggerAddCondition(.Invoker, boolExpr)
call .BoolIndex.add(boolExprId)
return true
endmethod
public method detach takes code theCode returns boolean
local boolexpr boolExpr = Condition(theCode)
local integer boolExprId = GetHandleId(boolExpr)
if .BoolHandles.has(boolExprId) then
return false
endif
call TriggerRemoveCondition(.Invoker, .BoolHandles.triggercondition[boolExprId])
call .BoolHandles.remove(boolExprId)
call .BoolIndex.delete(boolExprId)
return true
endmethod
public method clear takes nothing returns nothing
local integer boolExprId = 0
loop
exitwhen .BoolIndex.count == 0
set boolExprId = .BoolIndex.get(.BoolIndex.count)
call TriggerRemoveCondition(.Invoker, .BoolHandles.triggercondition[boolExprId])
call .BoolHandles.remove(boolExprId)
call .BoolIndex.delete(boolExprId)
endloop
endmethod
private method onDestroy takes nothing returns nothing
call .abort()
call .clear()
call .BoolHandles.destroy()
call .BoolIndex.destroy()
call .ChildIndex.destroy()
call DestroyTrigger(.Invoker)
endmethod
endmodule
struct Event
implement IsEvent
endstruct
endlibrary