-
Notifications
You must be signed in to change notification settings - Fork 17
/
types.go
573 lines (446 loc) · 11 KB
/
types.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2016 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cgo
//#include <stdint.h>
//#include <stdlib.h>
import "C"
import "unsafe"
type (
Bool C.char
Char C.char
Int C.int
UInt C.uint
Float C.float
Double C.double
SizeT C.size_t
Int8 C.int8_t
Int16 C.int16_t
Int32 C.int32_t
Int64 C.int64_t
IntPtr C.intptr_t
UInt8 C.uint8_t
UInt16 C.uint16_t
UInt32 C.uint32_t
UInt64 C.uint64_t
UIntPtr C.uintptr_t
VoidPointer uintptr
)
// -----------------------------------------------------------------------------
func bool2Byte(b bool) byte {
if b {
return 1
}
return 0
}
func NewBool(firstValue bool, moreValues ...bool) *Bool {
n := len(moreValues) + 1
p := NewBoolN(n)
s := p.Slice(n)
s[0] = bool2Byte(firstValue)
for i, v := range moreValues {
s[i+1] = bool2Byte(v)
}
return p
}
func NewBoolN(n int) *Bool {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Char(0))))
return (*Bool)(p)
}
func (s *Bool) First() bool {
return *s != 0
}
func (s *Bool) Slice(n int) []byte {
return ((*[1 << 31]byte)(unsafe.Pointer(s)))[0:n:n]
}
func (s *Bool) Free() {
C.free(unsafe.Pointer(s))
}
// -----------------------------------------------------------------------------
func NewChar(firstValue int, moreValues ...int) *Char {
n := len(moreValues) + 1
p := NewCharN(n)
s := p.Slice(n)
s[0] = byte(firstValue)
for i, v := range moreValues {
s[i+1] = byte(v)
}
return p
}
func NewCharN(n int) *Char {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Char(0))))
return (*Char)(p)
}
func (s *Char) First() byte {
return byte(*s)
}
func (s *Char) Slice(n int) []byte {
if n == 0 {
n = s.Strlen()
}
return ((*[1 << 31]byte)(unsafe.Pointer(s)))[0:n:n]
}
func (s *Char) Free() {
C.free(unsafe.Pointer(s))
}
// -----------------------------------------------------------------------------
func NewInt(firstValue int, moreValues ...int) *Int {
n := len(moreValues) + 1
p := NewIntN(n)
s := p.Slice(n)
s[0] = Int(firstValue)
for i, v := range moreValues {
s[i+1] = Int(v)
}
return p
}
func NewIntN(n int) *Int {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int(0))))
return (*Int)(p)
}
func (p *Int) First() int {
return int(*p)
}
func (p *Int) Slice(n int) []Int {
return ((*[1 << 29]Int)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Int) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUInt(firstValue uint, moreValues ...uint) *UInt {
n := len(moreValues) + 1
p := NewUIntN(n)
s := p.Slice(n)
s[0] = UInt(firstValue)
for i, v := range moreValues {
s[i+1] = UInt(v)
}
return p
}
func NewUIntN(n int) *UInt {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt(0))))
return (*UInt)(p)
}
func (p *UInt) First() uint {
return uint(*p)
}
func (p *UInt) Slice(n int) []UInt {
return ((*[1 << 29]UInt)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UInt) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewFloat(firstValue float32, moreValues ...float32) *Float {
n := len(moreValues) + 1
p := NewFloatN(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewFloatN(n int) *Float {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Float(0))))
return (*Float)(p)
}
func (p *Float) First() float32 {
return float32(*p)
}
func (p *Float) Slice(n int) []float32 {
return ((*[1 << 29]float32)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Float) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewDouble(firstValue float64, moreValues ...float64) *Double {
n := len(moreValues) + 1
p := NewDoubleN(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewDoubleN(n int) *Double {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Double(0))))
return (*Double)(p)
}
func (p *Double) First() float64 {
return float64(*p)
}
func (p *Double) Slice(n int) []float64 {
return ((*[1 << 28]float64)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Double) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewSizeT(firstValue int, moreValues ...int) *SizeT {
n := len(moreValues) + 1
p := NewSizeTN(n)
s := p.Slice(n)
s[0] = SizeT(firstValue)
for i, v := range moreValues {
s[i+1] = SizeT(v)
}
return p
}
func NewSizeTN(n int) *SizeT {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(SizeT(0))))
return (*SizeT)(p)
}
func (p *SizeT) First() int {
return int(*p)
}
func (p *SizeT) Slice(n int) []SizeT {
return ((*[1 << 28]SizeT)(unsafe.Pointer(p)))[0:n:n]
}
func (p *SizeT) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewInt8(firstValue int8, moreValues ...int8) *Int8 {
n := len(moreValues) + 1
p := NewInt8N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewInt8N(n int) *Int8 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int8(0))))
return (*Int8)(p)
}
func (p *Int8) First() int8 {
return int8(*p)
}
func (p *Int8) Slice(n int) []int8 {
return ((*[1 << 31]int8)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Int8) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUInt8(firstValue uint8, moreValues ...uint8) *UInt8 {
n := len(moreValues) + 1
p := NewUInt8N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewUInt8N(n int) *UInt8 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt8(0))))
return (*UInt8)(p)
}
func (p *UInt8) First() uint8 {
return uint8(*p)
}
func (p *UInt8) Slice(n int) []uint8 {
return ((*[1 << 31]uint8)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UInt8) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewInt16(firstValue int16, moreValues ...int16) *Int16 {
n := len(moreValues) + 1
p := NewInt16N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewInt16N(n int) *Int16 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int16(0))))
return (*Int16)(p)
}
func (p *Int16) First() int16 {
return int16(*p)
}
func (p *Int16) Slice(n int) []int16 {
return ((*[1 << 30]int16)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Int16) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUInt16(firstValue uint16, moreValues ...uint16) *UInt16 {
n := len(moreValues) + 1
p := NewUInt16N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewUInt16N(n int) *UInt16 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt16(0))))
return (*UInt16)(p)
}
func (p *UInt16) First() uint16 {
return uint16(*p)
}
func (p *UInt16) Slice(n int) []uint16 {
return ((*[1 << 30]uint16)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UInt16) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewInt32(firstValue int32, moreValues ...int32) *Int32 {
n := len(moreValues) + 1
p := NewInt32N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewInt32N(n int) *Int32 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int32(0))))
return (*Int32)(p)
}
func (p *Int32) First() int32 {
return int32(*p)
}
func (p *Int32) Slice(n int) []int32 {
return ((*[1 << 29]int32)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Int32) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUInt32(firstValue uint32, moreValues ...uint32) *UInt32 {
n := len(moreValues) + 1
p := NewUInt32N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewUInt32N(n int) *UInt32 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt32(0))))
return (*UInt32)(p)
}
func (p *UInt32) First() uint32 {
return uint32(*p)
}
func (p *UInt32) Slice(n int) []uint32 {
return ((*[1 << 29]uint32)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UInt32) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewInt64(firstValue int64, moreValues ...int64) *Int64 {
n := len(moreValues) + 1
p := NewInt64N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewInt64N(n int) *Int64 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int64(0))))
return (*Int64)(p)
}
func (p *Int64) First() int64 {
return int64(*p)
}
func (p *Int64) Slice(n int) []int64 {
return ((*[1 << 28]int64)(unsafe.Pointer(p)))[0:n:n]
}
func (p *Int64) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUInt64(firstValue uint64, moreValues ...uint64) *UInt64 {
n := len(moreValues) + 1
p := NewUInt64N(n)
s := p.Slice(n)
s[0] = firstValue
for i, v := range moreValues {
s[i+1] = v
}
return p
}
func NewUInt64N(n int) *UInt64 {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt64(0))))
return (*UInt64)(p)
}
func (p *UInt64) First() uint64 {
return uint64(*p)
}
func (p *UInt64) Slice(n int) []uint64 {
return ((*[1 << 28]uint64)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UInt64) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewIntPtr(firstValue uintptr, moreValues ...uintptr) *IntPtr {
n := len(moreValues) + 1
p := NewIntPtrN(n)
s := p.Slice(n)
s[0] = IntPtr(firstValue)
for i, v := range moreValues {
s[i+1] = IntPtr(v)
}
return p
}
func NewIntPtrN(n int) *IntPtr {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(IntPtr(0))))
return (*IntPtr)(p)
}
func (p *IntPtr) First() uintptr {
return uintptr(*p)
}
func (p *IntPtr) Slice(n int) []IntPtr {
return ((*[1 << 28]IntPtr)(unsafe.Pointer(p)))[0:n:n]
}
func (p *IntPtr) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------
func NewUIntPtr(firstValue uintptr, moreValues ...uintptr) *UIntPtr {
n := len(moreValues) + 1
p := NewUIntPtrN(n)
s := p.Slice(n)
s[0] = UIntPtr(firstValue)
for i, v := range moreValues {
s[i+1] = UIntPtr(v)
}
return p
}
func NewUIntPtrN(n int) *UIntPtr {
p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UIntPtr(0))))
return (*UIntPtr)(p)
}
func (p *UIntPtr) First() uintptr {
return uintptr(*p)
}
func (p *UIntPtr) Slice(n int) []UIntPtr {
return ((*[1 << 28]UIntPtr)(unsafe.Pointer(p)))[0:n:n]
}
func (p *UIntPtr) Free() {
C.free(unsafe.Pointer(p))
}
// -----------------------------------------------------------------------------