Access self from a C style pointer [duplicate]

雨燕双飞 提交于 2019-12-24 07:03:22

问题


I'm working on an application that utilises MIDI equipment. After some fooling around in a playground with CoreMIDI, I found how to get MIDI input signal, so I implemented this:

func makeInputSource() {
    var midiClient : MIDIClientRef = 0
    var inPort : MIDIPortRef = 0

    MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
    MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
        (pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) in
        let packetList : MIDIPacketList = pktList.pointee
        var packet : MIDIPacket = packetList.packet

        for _ in 1...packetList.numPackets {
            let bytes = Mirror(reflecting: packet.data).children
            var params : [UInt64] = []

            var i = packet.length
            for (_, attr) in bytes.enumerated() {
                let string = String(format: "%02X ", attr.value as! UInt8)
                params.append(UInt64(strtoul(string, nil, 16)))
                i -= 1

                if (i <= 0) {
                    break
                }
            }

            packet = MIDIPacketNext(&packet).pointee
        }
    }, nil, &inPort)
    MIDIPortConnectSource(inPort, self.source, &self.source)
}

Which works like a charm for using the signal. Now, I want to use the signal to edit the value of an NSSlider, so, naturally, what I came up with was this:

self.slider_one?.integerValue = params[2]

However, when I try to do that, I get the following error:

A C function pointer cannot be formed from a closure that captures context

So what I'm wondering is, is there a way to access self from inside of that closure, or is there some other way to use MIDI input in swift?

Thanks.

--- Edit: As asked, my code after modification:

func makeInputSource() {
    var midiClient : MIDIClientRef = 0
    var inPort : MIDIPortRef = 0
    var observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())

    MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
    MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
        (pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) in
        let packetList : MIDIPacketList = pktList.pointee
        var packet : MIDIPacket = packetList.packet

        for _ in 1...packetList.numPackets {
            let bytes = Mirror(reflecting: packet.data).children
            var params : [UInt64] = []

            var i = packet.length
            for (_, attr) in bytes.enumerated() {
                let string = String(format: "%02X ", attr.value as! UInt8)
                params.append(UInt64(strtoul(string, nil, 16)))
                i -= 1

                if (i <= 0) {
                    break
                }
            }

            let mySelf = Unmanaged<Wob>.fromOpaque(observer).takeUnretainedValue()
            mySelf.slider_one?.integerValue = 25 // 25 is a test value 
            packet = MIDIPacketNext(&packet).pointee
        }

    }, &observer, &inPort)
    MIDIPortConnectSource(inPort, self.source, &self.source)

}

回答1:


Usually you can pass some context into C functions, for example:

struct MyContext {
   var setSliderValue: (Int) -> Void        
}

var context = MyContext(setSliderValue: { sliderValue in
    Dispatch.queue.async {
       self.slider_one?.integerValue = sliderValue
    }
))

then pass it to your C function:

MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, { ... }, &context, &inPort)

and inside your closure function:

let readContext = readProcRefCon!.assumingMemoryBound(to: MyContext.self)
readContext.pointee.setSliderValue(params[2])

(written without testing)



来源:https://stackoverflow.com/questions/43589188/access-self-from-a-c-style-pointer

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