forked from therecipe/qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt.go
107 lines (90 loc) · 2.09 KB
/
qt.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package qt
import (
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
)
var (
Logger = log.New(os.Stderr, "", log.Ltime)
signals = make(map[string]interface{})
signalsMutex = new(sync.Mutex)
ids int
)
func init() { runtime.LockOSThread() }
func ExistsSignal(name, signal string) bool {
signalsMutex.Lock()
_, exists := signals[fmt.Sprintf("%v:%v", name, signal)]
signalsMutex.Unlock()
return exists
}
func LendSignal(name, signal string) interface{} {
var s interface{}
signalsMutex.Lock()
s = signals[fmt.Sprintf("%v:%v", name, signal)]
signalsMutex.Unlock()
return s
}
func GetSignal(name, signal string) interface{} {
if signal == "destroyed" || strings.HasPrefix(signal, "~") {
defer DisconnectAllSignals(name, signal)
}
return LendSignal(name, signal)
}
func ConnectSignal(name, signal string, function interface{}) {
signalsMutex.Lock()
signals[fmt.Sprintf("%v:%v", name, signal)] = function
signalsMutex.Unlock()
}
func DisconnectSignal(name, signal string) {
signalsMutex.Lock()
delete(signals, fmt.Sprintf("%v:%v", name, signal))
signalsMutex.Unlock()
}
func DisconnectAllSignals(name, signal string) {
signalsMutex.Lock()
for entry := range signals {
if (signal == "destroyed" || !strings.HasSuffix(entry, fmt.Sprintf(":%v", "destroyed"))) && strings.HasPrefix(entry, fmt.Sprintf("%v:", name)) {
delete(signals, entry)
}
}
signalsMutex.Unlock()
}
func Identifier() string {
ids++
return fmt.Sprint(ids)
}
func DumpSignals() {
Debug("##############################\tSIGNALSTABLE_START\t##############################")
signalsMutex.Lock()
for entry := range signals {
Debug(entry)
}
signalsMutex.Unlock()
Debug("##############################\tSIGNALSTABLE_END\t##############################")
}
func CountSignals() int {
var c int
signalsMutex.Lock()
c = len(signals)
signalsMutex.Unlock()
return c
}
func GoBoolToInt(b bool) int {
if b {
return 1
}
return 0
}
func Recover(fn string) {
if recover() != nil {
Debug("RECOVERED:", fn)
}
}
func Debug(fn ...interface{}) {
if strings.ToLower(os.Getenv("QT_DEBUG")) == "true" {
Logger.Println(fn...)
}
}