在 Swift 中使用将函数作为参数的 C 函数 [英] Using C functions in Swift that take functions as arguments

查看:26
本文介绍了在 Swift 中使用将函数作为参数的 C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 C 数学库的包装器.每个函数都接受一两个函数作为参数.但是,这些子函数(以及父函数)的参数不是 Swifty - 因此是包装器.

I'm writing a wrapper around a C mathematical library. Every function takes one or two functions as arguments. However, the arguments for those child functions (as well as the parent functions) are not Swifty -hence the wrapper.

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

I've cleaned up the example code to just show the three main pieces: the c-library function, the desired Swift function that would be passed to the wrapper (body not shown, but wrapping around the c-library function), and the required C function form.

//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 函数指针获取上下文,这可能没有用.

I should add that I have access to the c source code, so I could possibly add some dummy parameters (possibly to find a way to pass context in). But given that the docs seem to indicate that one can't grab context with a c function pointer, this may be of no use.

推荐答案

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

您的问题是关于将另一个 C 函数作为参数的 C 函数,因此让我们将问题简化为该问题.这是一个简单的 C 函数,它接受一个参数,即又是一个 C 函数,它接受一个指向双精度数组的指针和整数计数:

Your question is about C functions taking another C function as an argument, so let us reduce the question to that problem. Here is a simple C function which takes a single argument which is again a C function which takes a pointer to an array of doubles and an integer count:

// 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 中

This function is imported to Swift as

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

此处 @convention(c) 声明块具有 C 样式调用公约.特别是,在 Swift 中,您只能传递一个全局函数或一个不捕获任何上下文的闭包.

Here @convention(c) declares the block to have C-style calling conventions. In particular, from Swift you can pass only a global function or a closure which does not capture any context.

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

A simple example for a Swift wrapper is

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) }
}

这篇关于在 Swift 中使用将函数作为参数的 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆