Hi I am trying to wrap a C api using Swift 4
Swift has imported a function with the following signature.
public typealias indicator = @convention(c) (Int32, UnsafePointer<UnsafePointer<Double>?>?, UnsafePointer<Double>?, UnsafePointer<UnsafeMutablePointer<Double>?>?) -> Int32
according to the C libraries docs then the signature is as follows:
int indicator(int size,
double const *const *inputs,
double const *options,
double *const *outputs);
It’s worth noting that the int
return from the function is in c style the error type of the function, the actual return is in the outputs
pointer
So then assuming I create the following Swift types
let inputs: [[Double]] = []
let options: [Double] = []
var outputs: [[Double]] = []
with some appropriate values then I should be able to do something like: ( note info.pointee.indicator
is the imported function )
internal func calculateIndicator(options opts: [Double], input inputs: [[Double]], output outPuts: inout [[Double]]) -> [[Double]]? {
guard let sz = inputs.first?.count else {fatalError("Must supply a [[Double]] input param")}
let inputPointer = UnsafePointer<[Double]>(inputs)
let optionsPointer = UnsafePointer<Double>(opts)
var outputPointer = UnsafeMutablePointer<[Double]>(&outPuts)
let val = info.pointee.indicator(Int32(sz), inputPointer, optionsPointer, outputPointer)
// do something with the outputs and return the values
}
however the compiler complains with the following error:
Cannot invoke 'indicator' with an argument list of type '(Int32, UnsafePointer<[Double]>, UnsafePointer<Double>, UnsafeMutablePointer<[Double]>)'
This sort of makes sense as I am passing the incorrect types ( I think ).
So then memory management issues aside how would I go about converting the [[Double]]
types to for example the UnsafePointer<UnsafeMutablePointer<Double>>
pointer?
according to the docs here Calling Functions With Pointer Parameters I should be able to do this with implicit bridging but it seems not, perhaps I should just create the pointer types rather than try and convert from Swift?
Thanks in advance, I'm sure I'm missing something simple.
The C API itself is as follows:
typedef int (*indicator_function)(int size,
double const *const *inputs,
double const *options,
double *const *outputs);
typedef struct indicator_info {
char *name;
char *full_name;
indicator_start_function start;
indicator_function indicator;
int type, inputs, options, outputs;
char *input_names[MAXINDPARAMS];
char *option_names[MAXINDPARAMS];
char *output_names[MAXINDPARAMS];
} indicator_info;
The indicator
function is accessed through the struct above.
A given instance of an indicator function is as follows
int add(int size,
TI_REAL const *const *inputs,
TI_REAL const *options,
TI_REAL *const *outputs);