|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | + @inlinable |
| 44 | + public var pointee: Pointee { |
| 45 | + if let value = self { |
| 46 | + return value.pointee |
| 47 | + } |
| 48 | + fatalError("Could not dereference nullptr") |
| 49 | + } |
| 50 | + |
| 51 | + @inlinable |
| 52 | + public func successor() -> Self { |
| 53 | + if let value = self { |
| 54 | + return value.successor() |
| 55 | + } |
| 56 | + fatalError("Could not increment nullptr") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Bridged C++ iterator that allows computing the distance between two of its |
| 61 | +/// instances, and advancing an instance by a given number of elements. |
| 62 | +/// |
| 63 | +/// Mostly useful for conforming a type to the `CxxRandomAccessCollection` |
| 64 | +/// protocol and should not generally be used directly. |
| 65 | +/// |
| 66 | +/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator |
| 67 | +public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator { |
| 68 | + associatedtype Distance: BinaryInteger |
| 69 | + |
| 70 | + static func -(lhs: Self, rhs: Self) -> Distance |
| 71 | + static func +=(lhs: inout Self, rhs: Distance) |
| 72 | +} |
| 73 | + |
| 74 | +extension UnsafePointer: UnsafeCxxRandomAccessIterator {} |
| 75 | + |
| 76 | +extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {} |
0 commit comments