-
Notifications
You must be signed in to change notification settings - Fork 6
/
bridge.go
81 lines (62 loc) · 1.8 KB
/
bridge.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
package tulipindicators
/*
#include "indicators.h"
#include <stdio.h>
int bridgeStartFunction(ti_indicator_start_function f, TI_REAL const *options) {
return f(options);
}
int bridgeIndicatorFunction(ti_indicator_function f,
int size,
TI_REAL const *const *inputs,
TI_REAL const *options,
TI_REAL *const *outputs) {
return f(size, inputs, options, outputs);
}
*/
import (
"C"
)
import (
"fmt"
)
func indicator(
numOutputs int,
startFunc /* unsafe.Pointer, */ C.ti_indicator_start_function,
indicatorFunc /* unsafe.Pointer, */ C.ti_indicator_function,
inputs [][]float64,
options []float64,
) ([][]float64, error) {
castOptions := castToCDoubleArray(options)
defer freeCDoubleArray(castOptions)
castInputs, inputs := castToC2dDoubleArray(inputs)
defer freeC2dDoubleArray(castInputs, len(inputs))
outputSizeDiff := C.bridgeStartFunction(startFunc, castOptions)
outputSize := len(inputs[0]) - int(outputSizeDiff)
if outputSize < 1 {
return nil, fmt.Errorf("insufficient inputs")
}
outputs := make([][]float64, numOutputs)
for i := range outputs {
outputs[i] = make([]float64, outputSize)
}
castOutputs, outputs := castToC2dDoubleArray(outputs)
defer freeC2dDoubleArray(castOutputs, len(outputs))
castOutputSize := C.int(outputSize)
doResponse, doError := C.bridgeIndicatorFunction(
indicatorFunc,
castOutputSize,
castInputs,
castOptions,
castOutputs,
)
if doError != nil {
//skipping error because the output *is* actually valid. SCARY
//referencing golang github issue 23468
return nil, doError
}
if doResponse == C.TI_INVALID_OPTION {
return nil, fmt.Errorf("invalid Option for TulipIndicator")
}
outputs = extractOutputs(castOutputs, outputs)
return outputs, nil
}