forked from go-qamel/qamel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
63 lines (51 loc) · 1.06 KB
/
object.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
package qamel
import (
"sync"
"unsafe"
)
var (
mutex = sync.Mutex{}
mapObject = map[unsafe.Pointer]interface{}{}
)
// QmlObject is the base of QML object
type QmlObject struct {
Ptr unsafe.Pointer
}
// RegisterObject registers the specified pointer to specified object
func RegisterObject(ptr unsafe.Pointer, obj interface{}) {
if ptr == nil || obj == nil {
return
}
mutex.Lock()
mapObject[ptr] = obj
mutex.Unlock()
}
// BorrowObject fetch object for the specified pointer
func BorrowObject(ptr unsafe.Pointer) interface{} {
if ptr == nil {
return nil
}
mutex.Lock()
return mapObject[ptr]
}
// ReturnObject returns pointer and lock map again
func ReturnObject(ptr unsafe.Pointer) {
if ptr == nil {
return
}
mutex.Unlock()
}
// ObjectExists checks if object exists in map
func ObjectExists(ptr unsafe.Pointer) bool {
obj, ok := mapObject[ptr]
return ok && obj != nil
}
// DeleteObject remove object for the specified pointer
func DeleteObject(ptr unsafe.Pointer) {
if ptr == nil {
return
}
mutex.Lock()
delete(mapObject, ptr)
mutex.Unlock()
}