|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// Bridged C++ iterator that allows to traverse the elements of a sequence |
| 14 | +/// using a for-in loop. |
| 15 | +/// |
| 16 | +/// Mostly useful for conforming a type to the `CxxSequence` protocol and should |
| 17 | +/// not generally be used directly. |
| 18 | +/// |
| 19 | +/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator |
| 20 | +public protocol UnsafeCxxInputIterator: Equatable { |
| 21 | + associatedtype Pointee |
| 22 | + |
| 23 | + /// Returns the unwrapped result of C++ `operator*()`. |
| 24 | + /// |
| 25 | + /// Generally, Swift creates this property automatically for C++ types that |
| 26 | + /// define `operator*()`. |
| 27 | + var pointee: Pointee { get } |
| 28 | + |
| 29 | + /// Returns an iterator pointing to the next item in the sequence. |
| 30 | + /// |
| 31 | + /// Generally, Swift creates this property automatically for C++ types that |
| 32 | + /// define pre-increment `operator++()`. |
| 33 | + func successor() -> Self |
| 34 | +} |
| 35 | + |
| 36 | +extension UnsafePointer: UnsafeCxxInputIterator {} |
| 37 | + |
| 38 | +extension UnsafeMutablePointer: UnsafeCxxInputIterator {} |
| 39 | + |
| 40 | +extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator { |
| 41 | + public typealias Pointee = Wrapped.Pointee |
| 42 | + |
| 43 | + public var pointee: Pointee { |
| 44 | + if let value = self { |
| 45 | + return value.pointee |
| 46 | + } |
| 47 | + fatalError("Could not dereference nullptr") |
| 48 | + } |
| 49 | + |
| 50 | + public func successor() -> Self { |
| 51 | + if let value = self { |
| 52 | + return value.successor() |
| 53 | + } |
| 54 | + fatalError("Could not increment nullptr") |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Use this protocol to conform custom C++ sequence types to Swift's `Sequence` |
| 59 | +/// protocol like this: |
| 60 | +/// |
| 61 | +/// extension MyCxxSequenceType : CxxSequence {} |
| 62 | +/// |
| 63 | +/// This requires the C++ sequence type to define const methods `begin()` and |
| 64 | +/// `end()` which return input iterators into the C++ sequence. The iterator |
| 65 | +/// types must conform to `UnsafeCxxInputIterator`. |
| 66 | +public protocol CxxSequence: Sequence { |
| 67 | + associatedtype RawIterator: UnsafeCxxInputIterator |
| 68 | + associatedtype Element = RawIterator.Pointee |
| 69 | + |
| 70 | + // `begin()` and `end()` have to be mutating, otherwise calling |
| 71 | + // `self.sequence.begin()` will copy `self.sequence` into a temporary value, |
| 72 | + // and the result will be dangling. This does not mean that the implementing |
| 73 | + // methods _have_ to be mutating. |
| 74 | + |
| 75 | + /// Do not implement this function manually in Swift. |
| 76 | + mutating func begin() -> RawIterator |
| 77 | + |
| 78 | + /// Do not implement this function manually in Swift. |
| 79 | + mutating func end() -> RawIterator |
| 80 | +} |
| 81 | + |
| 82 | +public class CxxIterator<T>: IteratorProtocol where T: CxxSequence { |
| 83 | + // Declared as a class instead of a struct to avoid copies of this object, |
| 84 | + // which would result in dangling pointers for some C++ sequence types. |
| 85 | + |
| 86 | + public typealias Element = T.RawIterator.Pointee |
| 87 | + private var sequence: T |
| 88 | + private var rawIterator: T.RawIterator |
| 89 | + private let endIterator: T.RawIterator |
| 90 | + |
| 91 | + public init(sequence: T) { |
| 92 | + self.sequence = sequence |
| 93 | + self.rawIterator = self.sequence.begin() |
| 94 | + self.endIterator = self.sequence.end() |
| 95 | + } |
| 96 | + |
| 97 | + public func next() -> Element? { |
| 98 | + if self.rawIterator == self.endIterator { |
| 99 | + return nil |
| 100 | + } |
| 101 | + let object = self.rawIterator.pointee |
| 102 | + self.rawIterator = self.rawIterator.successor() |
| 103 | + return object |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +extension CxxSequence { |
| 108 | + public func makeIterator() -> CxxIterator<Self> { |
| 109 | + return CxxIterator(sequence: self) |
| 110 | + } |
| 111 | +} |
0 commit comments