示例工程 26-objc-cpp-interop

26-objc-cpp-interop/Sources/Ch26Kit/CxxBridge.swift

swift · 0.6 KB

import Ch26Cxx
 
/// 把 C++ 类型包在 Swift 值类型里,调用方不需要开启 C++ 互操作模式。
public struct Counter {
    private var storage: CxxCounter
 
    public init(start: Int32 = 0) {
        storage = CxxCounter(start)
    }
 
    public mutating func increment() -> Int32 {
        storage.increment()
    }
 
    public mutating func add(_ amount: Int32) {
        storage.add(amount)
    }
 
    public var value: Int32 {
        storage.current()
    }
}
 
public enum CxxMath {
    public static func multiply(_ lhs: Int32, _ rhs: Int32) -> Int32 {
        cxxMultiply(lhs, rhs)
    }
}