Using C functions in Swift that take functions as arguments

十年热恋 提交于 2020-01-01 12:13:17

问题


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.

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

addition

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.


回答1:


(Note: the following example uses Swift 3 on Xcode 8 beta 2.)

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

This function is imported to Swift as

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

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.

A simple example for a Swift wrapper is

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

which you can use like this:

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

swiftyFunc(passedFunc: functionToPass)

or with a closure argument:

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



回答2:


Do you know that you can get a mutable pointer to a var just by using the & operator? It does the "right thing" on arrays too.

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

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

var array = [0]
foo(&array)

var int = 0
bar(&int)

(Tested on Swift 2, but most likely still valid on Swift 3.)

I suspect that this could drastically reduce your need for wrappers.



来源:https://stackoverflow.com/questions/38329659/using-c-functions-in-swift-that-take-functions-as-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!