forked from vova616/GarageEngine
/
FuncList.go
49 lines (43 loc) · 1.01 KB
/
FuncList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package engine
type FuncKey *func()
type FuncList struct {
Functions []FuncKey
}
func NewFuncList(f func()) *FuncList {
fa := &FuncList{nil}
fa.Add(f)
return fa
}
func (this *FuncList) Run() {
for i, fnc := range this.Functions {
if fnc != nil && *fnc != nil {
(*fnc)()
} else {
this.Functions[len(this.Functions)-1], this.Functions[i], this.Functions = nil, this.Functions[len(this.Functions)-1], this.Functions[:len(this.Functions)-1]
if fnc != nil {
*fnc = nil
}
}
}
}
func (this *FuncList) Add(fnc func()) (key FuncKey) {
if fnc == nil {
return
}
c := &fnc
this.Functions = append(this.Functions, c)
return c
}
func (this *FuncList) Remove(key FuncKey) (deleted bool) {
if key == nil || *key == nil {
return false
}
for i, c := range this.Functions {
if c == key {
this.Functions[len(this.Functions)-1], this.Functions[i], this.Functions = nil, this.Functions[len(this.Functions)-1], this.Functions[:len(this.Functions)-1]
*key = nil
return true
}
}
return false
}