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) Select
12
library TimerUtils requires Alloc, MicroTable
13
14
private struct data extends array
15
    static key K // used to connect the data struct with the variable
16
    static key D // used to link other data struct to the timer
17
    timer t
18
    
19
    implement Alloc
20
    
21
    method destroy takes nothing returns nothing
22
        call PauseTimer(.t)
23
        call ClearData(.t, thistype.K)
24
        call .deallocate()
25
    endmethod
26
    
27
    static method create takes nothing returns thistype
28
        local thistype this = thistype.allocate()
29
        if this.t == null then
30
            set this.t = CreateTimer()
31
        endif
32
        call StoreData(this.t, thistype.K, this) 
33
        return this
34
    endmethod
35
    
36
endstruct
37
38
function NewTimer takes nothing returns timer
39
    return data(data.create()).t
40
endfunction
41
42
function ReleaseTimer takes timer t returns nothing
43
    call data(GetData(t, data.K)).destroy()
44
endfunction
45
46
function SetTimerData takes timer t, integer d returns nothing
47
    call StoreData(t, data.D, d)
48
endfunction
49
50
function GetTimerData takes timer t returns integer
51
    return GetData(t, data.D)
52
endfunction
53
54
endlibrary