Skip to content

Commit

Permalink
add port connector helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Dec 16, 2023
1 parent b2792c5 commit 9b56795
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,62 @@ private extension OcaRoot {
)
}
}

private extension SwiftOCADevice.OcaWorker {
var firstAvailablePortIndex: OcaUint16 {
1 + (ports.map(\.id.index).max() ?? 0)
}
}

public extension SwiftOCADevice.OcaBlock where ActionObject: SwiftOCADevice.OcaWorker {
func connect(
_ outputs: [ActionObject],
to inputs: [ActionObject],
name: OcaString? = nil,
addToBlock: Bool = true
) async throws {
precondition(outputs.count == inputs.count)

for i in 0..<outputs.count {
let name = name ?? "\(outputs[i].role) -> \(inputs[i].role)"

let outputPortID = OcaPortID(mode: .output, index: outputs[i].firstAvailablePortIndex)
let outputPort = OcaPort(
owner: objectNumber,
id: outputPortID,
name: "\(name) [Output Port \(i + 1)]"
)
outputs[i].ports.append(outputPort)

let inputPortID = OcaPortID(mode: .input, index: inputs[i].firstAvailablePortIndex)
let inputPort = OcaPort(
owner: objectNumber,
id: inputPortID,
name: "\(name) [Input Port \(i + 1)]"
)
inputs[i].ports.append(inputPort)

let signalPath = OcaSignalPath(sourcePort: outputPort, sinkPort: inputPort)
_ = try await add(signalPath: signalPath)

if addToBlock, outputs[i] != self, !actionObjects.contains(outputs[i]) {
try await add(actionObject: outputs[i])
}
}
}

func connect(
_ output: ActionObject,
to input: ActionObject,
width: Int = 1,
name: OcaString? = nil,
addToBlock: Bool = true
) async throws {
try await connect(
Array(repeating: output, count: width),
to: Array(repeating: input, count: width),
name: name,
addToBlock: addToBlock
)
}
}

0 comments on commit 9b56795

Please sign in to comment.