Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enumerating a MIDIPacketList in Swift 2.
extension MIDIPacketList: SequenceType {
public typealias Generator = MIDIPacketListGenerator
public func generate() -> Generator {
return Generator(packetList: self)
}
}
/**
Generator for MIDIPacketList allowing iteration over its list of MIDIPacket objects.
*/
public struct MIDIPacketListGenerator : GeneratorType {
public typealias Element = MIDIPacket
init(packetList: MIDIPacketList) {
let ptr = UnsafeMutablePointer<MIDIPacket>.alloc(1)
ptr.initialize(packetList.packet)
self.packet = ptr
self.count = packetList.numPackets
}
public mutating func next() -> Element? {
guard self.packet != nil && self.index < self.count else { return nil }
let lastPacket = self.packet!
self.packet = MIDIPacketNext(self.packet!)
self.index++
return lastPacket.memory
}
// Extracted packet list info
var count: UInt32
var index: UInt32 = 0
// Iteration state
var packet: UnsafeMutablePointer<MIDIPacket>?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.