-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvariant.go
230 lines (194 loc) · 5.83 KB
/
variant.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* GML - Go QML
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Roland Singer <roland.singer[at]desertbit.com>
* Copyright (c) 2019 Sebastian Borchers <sebastian[at]desertbit.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package gml
// #include <gml.h>
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
"github.com/desertbit/gml/internal/json"
)
// TODO: think about adding a ticker routine calling gc...
// Hint: consider freeing a variant if not required anymore
// to release memory as fast as possible.
// The go garbage collector does not know about the C++ data
// and might not trigger a gc cycle to release memory.
// Consider this if you store bigger data values into a variant.
type Variant struct {
freed bool
ptr C.gml_variant
}
func NewVariantFromPointer(ptr unsafe.Pointer) (v *Variant) {
return newVariant((C.gml_variant)(ptr))
}
// TODO: free variant again. also add Free method and free as soon as possible within the gen code.
func ToVariant(i interface{}) *Variant {
var ptr C.gml_variant
switch d := i.(type) {
case nil:
ptr = C.gml_variant_new() // Always create a valid QVariant.
case bool:
var b C.u_int8_t
if d {
b = 1
}
ptr = C.gml_variant_new_from_bool(b)
case float32:
ptr = C.gml_variant_new_from_float(C.float(d))
case float64:
ptr = C.gml_variant_new_from_double(C.double(d))
case int:
ptr = C.gml_variant_new_from_int(C.int(d))
case int8:
ptr = C.gml_variant_new_from_int8(C.int8_t(d))
case uint8: // Alias for byte
ptr = C.gml_variant_new_from_uint8(C.u_int8_t(d))
case int16:
ptr = C.gml_variant_new_from_int16(C.int16_t(d))
case uint16:
ptr = C.gml_variant_new_from_uint16(C.u_int16_t(d))
case int32:
ptr = C.gml_variant_new_from_int32(C.int32_t(d))
case uint32:
ptr = C.gml_variant_new_from_uint32(C.u_int32_t(d))
case int64:
ptr = C.gml_variant_new_from_int64(C.int64_t(d))
case uint64:
ptr = C.gml_variant_new_from_uint64(C.u_int64_t(d))
case string:
cstr := C.CString(d)
defer C.free(unsafe.Pointer(cstr))
ptr = C.gml_variant_new_from_string(cstr) // Makes a deep copy.
case []byte:
ptr = C.gml_variant_new_from_bytes((*C.char)(unsafe.Pointer(&d[0])), C.int(len(d))) // Makes a deep copy.
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(d)
default:
data, err := json.Marshal(i)
if err != nil {
// Don't fail on an error.
fmt.Println("gml: ToVariant: failed to marshal value to json: ", err)
ptr = C.gml_variant_new() // Always create a valid QVariant.
} else {
ptr = C.gml_variant_new_from_bytes((*C.char)(unsafe.Pointer(&data[0])), C.int(len(data))) // Makes a deep copy.
}
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(data)
}
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(i)
return newVariant(ptr)
}
func newVariant(ptr C.gml_variant) (v *Variant) {
// This should never happen. Signalizes a fatal error.
if ptr == nil {
panic(fmt.Errorf("failed to create gml variant: C pointer is nil"))
}
v = &Variant{
ptr: ptr,
}
runtime.SetFinalizer(v, freeVariant) // Always free the C value.
return
}
func freeVariant(v *Variant) {
if v.freed {
return
}
v.freed = true
C.gml_variant_free(v.ptr)
}
func (v *Variant) Free() {
freeVariant(v)
}
func (v *Variant) Release() {
v.freed = true
}
func (v *Variant) Pointer() unsafe.Pointer {
return unsafe.Pointer(v.ptr)
}
// Pass a pointer.
func (v *Variant) Decode(i interface{}) (err error) {
if i == nil {
return errors.New("decode input is nil")
}
switch d := i.(type) {
case *bool:
*d = (C.gml_variant_to_bool(v.ptr) != 0)
case *float32:
*d = float32(C.gml_variant_to_float(v.ptr))
case *float64:
*d = float64(C.gml_variant_to_double(v.ptr))
case *int:
*d = int(C.gml_variant_to_int(v.ptr))
case *int8:
*d = int8(C.gml_variant_to_int8(v.ptr))
case *uint8: // Alias for byte
*d = uint8(C.gml_variant_to_uint8(v.ptr))
case *int16:
*d = int16(C.gml_variant_to_int16(v.ptr))
case *uint16:
*d = uint16(C.gml_variant_to_uint16(v.ptr))
case *int32:
*d = int32(C.gml_variant_to_int32(v.ptr))
case *uint32:
*d = uint32(C.gml_variant_to_uint32(v.ptr))
case *int64:
*d = int64(C.gml_variant_to_int64(v.ptr))
case *uint64:
*d = uint64(C.gml_variant_to_uint64(v.ptr))
case *string:
b := newBytes()
defer b.Free()
C.gml_variant_to_string(v.ptr, b.ptr)
buf := b.String()
if len(buf) == 0 {
*d = ""
} else {
*d = string(buf)
}
case *[]byte:
b := newBytes()
defer b.Free()
C.gml_variant_to_string(v.ptr, b.ptr)
*d = b.Bytes()
default:
b := newBytes()
defer b.Free()
C.gml_variant_to_string(v.ptr, b.ptr)
data := b.String()
if len(data) == 0 {
return fmt.Errorf("failed to decode variant: no data to decode")
}
err = json.Unmarshal(data, i)
if err != nil {
return fmt.Errorf("failed to decode variant: %v", err)
}
}
return
}