在Swift中使用C函数,将函数作为参数

时间:2016-07-12 13:00:38

标签: c swift wrapper unsafemutablepointer

我在C数学库周围写了一个包装器。每个函数都将一个或两个函数作为参数。但是,这些子函数(以及父函数)的参数不是Swifty -hence包装器。

我已经清理了示例代码,只显示了三个主要部分:c-library函数,所需的Swift函数将被传递给包装器(正文未显示,但环绕c-library)函数),以及所需的C函数形式。

//C library function, that calls the passed function dozens, hundreds or thousands of times, each time it changes the data provided in p, and uses the output from x
//The Swift arrays are passed as pointers, and the length of the and x array are m and n respectively
returnValue = cLibraryFunc(passedFunc, &p, &x, Int32(m), Int32(n), Int32(itmax), &opts, &info, &work, &covar, &adata)


//I would like to create a Swift function that would look like this (internals could be any myriad of things that takes inputs p and adata and returns data in x:
func desiredSwifty(p: inout [Double], x: inout [Double], m: Int, n: Int, adata: inout [Double]) {
    //very simple example
    //this example knows the length of p (so m as well)
    //and assumes that adata length is the same as the x length (n)
    //obviously, it could ifer m and n from p.count and x.count

    for i in 0..<n {
        x[i] = p[0] + p[1]*adata[i]  + p[2]*pow(adata[i], 2)
    }
}


//And the wrapper would "convert" it -internally- into the form that the C library function requires:
func requiredC(p: UnsafeMutablePointer<Double>?, x: UnsafeMutablePointer<Double>?, m: Int32, n: Int32, adata: UnsafeMutablePointer<Void>?) {
    //same thing, but using pointers, and uglier

    //first, have to bitcast the void back to a double
    let adataDouble : UnsafeMutablePointer<Double> = unsafeBitCast(adata, to: UnsafeMutablePointer<Double>.self)

    for i in 0..<Int(n) {
        x![i] = p![0] + p![1]*adataDouble[i]  + p![2]*pow(adataDouble[i], 2)
    }
}

除了

我应该补充说我可以访问c源代码,所以我可以添加一些虚拟参数(可能是为了找到传递上下文的方法)。但鉴于文档似乎表明人们无法使用c函数指针获取上下文,这可能毫无用处。

2 个答案:

答案 0 :(得分:5)

(注意:以下示例在Xcode 8 beta 2上使用Swift 3。)

您的问题是关于C函数将另一个C函数作为参数,所以让我们将问题简化为该问题。这是一个简单的C函数,它接受一个参数 再一个C函数,它接受一个双精度数组的指针 和整数计数:

// cfunction.h:
void cFunc(void (*func)(double *values, int count));

// cfunction.c:
void cFunc(void (*func)(double *values, int count)) {
    double x[] = { 1.2, 3.4, 5,6 };
    func(x, 3);
}

此函数作为

导入Swift
func cFunc(_ func: (@convention(c) (UnsafeMutablePointer<Double>?, Int32) -> Swift.Void)!)

这里@convention(c)声明块有C样式调用 约定。特别是,从Swift中你只能传递一个全局函数或一个不捕获任何上下文的闭包。

Swift包装器的一个简单示例是

func swiftyFunc(passedFunc: (@convention(c) (UnsafeMutablePointer<Double>?, Int32) -> Void)) {
    cFunc(passedFunc) 
}

你可以这样使用:

func functionToPass(values: UnsafeMutablePointer<Double>?, count: Int32) {
    let bufPtr = UnsafeBufferPointer(start: values, count: Int(count))
    for elem in bufPtr { print(elem) }
}

swiftyFunc(passedFunc: functionToPass)

或使用闭包参数:

swiftyFunc { (values, count) in
    let bufPtr = UnsafeBufferPointer(start: values, count: Int(count))
    for elem in bufPtr { print(elem) }
}

答案 1 :(得分:0)

您是否知道只需使用var运算符即可获得指向&的可变指针?它也在数组上做了“正确的事”。

func foo(_ x: UnsafeMutablePointer<Int>) {
    print(x)
}

func bar(_ x: UnsafeMutablePointer<Int>) {
    print(x)
}

var array = [0]
foo(&array)

var int = 0
bar(&int)

(在Swift 2上测试过,但很可能在Swift 3上仍然有效。)

我怀疑这可以大大减少你对包装的需求。