This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
/
options.go
192 lines (151 loc) · 4.63 KB
/
options.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
package common
import "C"
import (
"github.com/CannibalVox/cgoparam"
"unsafe"
)
type CAllocatable interface {
PopulateCPointer(allocator *cgoparam.Allocator, preallocatedPointer unsafe.Pointer) (unsafe.Pointer, error)
}
type Options interface {
PopulateCPointer(allocator *cgoparam.Allocator, preallocatedPointer unsafe.Pointer, next unsafe.Pointer) (unsafe.Pointer, error)
NextOptionsInChain() Options
}
type OutData interface {
PopulateHeader(allocator *cgoparam.Allocator, preallocatedPointer unsafe.Pointer, next unsafe.Pointer) (unsafe.Pointer, error)
PopulateOutData(cDataPointer unsafe.Pointer, helpers ...any) (next unsafe.Pointer, err error)
NextOutDataInChain() OutData
}
type NextOptions struct {
Next Options
}
func (n NextOptions) NextOptionsInChain() Options {
return n.Next
}
type NextOutData struct {
Next OutData
}
func (n NextOutData) NextOutDataInChain() OutData {
return n.Next
}
func AllocOutDataHeader(allocator *cgoparam.Allocator, o OutData, preallocatedPointer ...unsafe.Pointer) (unsafe.Pointer, error) {
nextPtr, err := allocNextOutData(allocator, o)
if err != nil {
return nil, err
}
var preallocatedPointerToPass unsafe.Pointer
if len(preallocatedPointer) > 0 {
preallocatedPointerToPass = preallocatedPointer[0]
}
return o.PopulateHeader(allocator, preallocatedPointerToPass, nextPtr)
}
func AllocOutDataHeaderSlice[T any, O OutData](allocator *cgoparam.Allocator, o []O) (*T, error) {
optionCount := len(o)
optionPtr := (*T)(allocator.Malloc(optionCount * int(unsafe.Sizeof([1]T{}))))
optionSlice := unsafe.Slice(optionPtr, optionCount)
for i := 0; i < optionCount; i++ {
next, err := allocNextOutData(allocator, o[i])
if err != nil {
return nil, err
}
_, err = o[i].PopulateHeader(allocator, unsafe.Pointer(&optionSlice[i]), next)
if err != nil {
return nil, err
}
}
return optionPtr, nil
}
func allocNextOutData(allocator *cgoparam.Allocator, o OutData) (unsafe.Pointer, error) {
next := o.NextOutDataInChain()
if next == nil {
return nil, nil
}
return AllocOutDataHeader(allocator, next)
}
func AllocOptions(allocator *cgoparam.Allocator, o Options, preallocatedPointer ...unsafe.Pointer) (unsafe.Pointer, error) {
nextPtr, err := allocNextOptions(allocator, o)
if err != nil {
return nil, err
}
var preallocatedPointerToPass unsafe.Pointer
if len(preallocatedPointer) > 0 {
preallocatedPointerToPass = preallocatedPointer[0]
}
return o.PopulateCPointer(allocator, preallocatedPointerToPass, nextPtr)
}
func AllocOptionSlice[T any, O Options](allocator *cgoparam.Allocator, o []O) (*T, error) {
optionCount := len(o)
optionPtr := (*T)(allocator.Malloc(optionCount * int(unsafe.Sizeof([1]T{}))))
optionSlice := unsafe.Slice(optionPtr, optionCount)
for i := 0; i < optionCount; i++ {
next, err := allocNextOptions(allocator, o[i])
if err != nil {
return nil, err
}
_, err = o[i].PopulateCPointer(allocator, unsafe.Pointer(&optionSlice[i]), next)
if err != nil {
return nil, err
}
}
return optionPtr, nil
}
func allocNextOptions(allocator *cgoparam.Allocator, o Options) (unsafe.Pointer, error) {
next := o.NextOptionsInChain()
if next == nil {
return nil, nil
}
return AllocOptions(allocator, next)
}
func AllocSlice[T any, A CAllocatable](allocator *cgoparam.Allocator, a []A) (*T, error) {
optionCount := len(a)
optionSize := unsafe.Sizeof([1]T{})
optionPtr := allocator.Malloc(optionCount * int(optionSize))
optionIterPtr := optionPtr
for i := 0; i < optionCount; i++ {
_, err := a[i].PopulateCPointer(allocator, optionIterPtr)
if err != nil {
return nil, err
}
optionIterPtr = unsafe.Add(optionIterPtr, optionSize)
}
return (*T)(optionPtr), nil
}
func PopulateOutData(o OutData, cPointer unsafe.Pointer, helpers ...any) error {
next, err := o.PopulateOutData(cPointer, helpers...)
if err != nil {
return err
}
nextOptions := o.NextOutDataInChain()
if nextOptions != nil {
return PopulateOutData(nextOptions, next, helpers...)
}
return nil
}
func PopulateOutDataSlice[T any, O OutData](o []O, cSlicePointer unsafe.Pointer, helpers ...any) error {
cElementSize := unsafe.Sizeof([1]T{})
for i := 0; i < len(o); i++ {
err := PopulateOutData(o[i], cSlicePointer, helpers...)
if err != nil {
return err
}
cSlicePointer = unsafe.Add(cSlicePointer, cElementSize)
}
return nil
}
func OfType[T any](values []any) (T, bool) {
for _, val := range values {
typed, ok := val.(T)
if ok {
return typed, true
}
}
var zero T
return zero, false
}
func ConvertSlice[T any, U any](values []T, mapping func(in T) U) []U {
out := make([]U, len(values))
for i := 0; i < len(values); i++ {
out[i] = mapping(values[i])
}
return out
}