Hibrid TimerUtils

Scripts

11 years
edited 8 years
Description


This small library allows to attach data structs into timers in an easy way, following the structure proposed by Vexorian, just for compatibility.

Requirements:


- Microtable
- Alloc

Actual Code


Code (jass,12,21,32,36,40) Select
library TimerUtils requires Alloc, MicroTable

private struct data extends array
    static key K // used to connect the data struct with the variable
    static key D // used to link other data struct to the timer
    timer t
   
    implement Alloc
   
    method destroy takes nothing returns nothing
        call PauseTimer(.t)
        call ClearData(.t, thistype.K)
        call .deallocate()
    endmethod
   
    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        if this.t == null then
            set this.t = CreateTimer()
        endif
        call StoreData(this.t, thistype.K, this)
        return this
    endmethod
   
endstruct

function NewTimer takes nothing returns timer
    return data(data.create()).t
endfunction

function ReleaseTimer takes timer t returns nothing
    call data(GetData(t, data.K)).destroy()
endfunction

function SetTimerData takes timer t, integer d returns nothing
    call StoreData(t, data.D, d)
endfunction

function GetTimerData takes timer t returns integer
    return GetData(t, data.D)
endfunction

endlibrary