Foundation: Add staging typealiases for C pointer types.

Swift SVN r19209
This commit is contained in:
Joe Groff
2014-06-26 01:30:50 +00:00
parent c696acfefb
commit d1e7ff0590
2 changed files with 46 additions and 29 deletions

View File

@@ -13,6 +13,23 @@
@exported import Foundation // Clang module
import CoreFoundation
// TODO: Eliminate this monstrosity when pointer conversions are staged in.
#if ENABLE_POINTER_CONVERSIONS
struct _CPointerTo<T> {
typealias Mutable = UnsafePointer<T>
typealias Const = ConstUnsafePointer<T>
}
typealias _CMutableVoidPointer = UnsafePointer<Void>
typealias _CConstVoidPointer = ConstUnsafePointer<Void>
#else
struct _CPointerTo<T> {
typealias Mutable = CMutablePointer<T>
typealias Const = CConstPointer<T>
}
typealias _CMutableVoidPointer = CMutableVoidPointer
typealias _CConstVoidPointer = CConstVoidPointer
#endif
//===----------------------------------------------------------------------===//
// Enums
//===----------------------------------------------------------------------===//
@@ -233,18 +250,18 @@ class _NSContiguousString : NSString {
return value[index]
}
override func getCharacters(buffer: CMutablePointer<unichar>,
override func getCharacters(buffer: _CPointerTo<unichar>.Mutable,
range aRange: NSRange) {
_precondition(aRange.location + aRange.length <= Int(value.count))
if value.elementWidth == 2 {
UTF16.copy(
value.startUTF16 + aRange.location, destination: UnsafePointer(buffer),
value.startUTF16 + aRange.location, destination: UnsafePointer<unichar>(buffer),
count: aRange.length)
}
else {
UTF16.copy(
value.startASCII + aRange.location, destination: UnsafePointer(buffer),
value.startASCII + aRange.location, destination: UnsafePointer<unichar>(buffer),
count: aRange.length)
}
}
@@ -290,7 +307,7 @@ class _NSOpaqueString : NSString {
return owner.characterAtIndex(index + subRange.location)
}
override func getCharacters(buffer: CMutablePointer<unichar>,
override func getCharacters(buffer: _CPointerTo<unichar>.Mutable,
range aRange: NSRange) {
owner.getCharacters(
@@ -1274,7 +1291,7 @@ extension NSArray {
let x = _extractOrCopyToNativeArrayBuffer(elements._buffer)
// Use Imported:
// @objc(initWithObjects:count:)
// init(withObjects objects: CConstPointer<AnyObject?>,
// init(withObjects objects: ConstUnsafePointer<AnyObject?>,
// count cnt: Int)
self.init(objects: UnsafePointer(x.elementStorage), count: x.count)
_fixLifetime(x)
@@ -1311,7 +1328,7 @@ extension NSOrderedSet {
// - (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
// Imported as:
// @objc(initWithObjects:count:)
// init(withObjects objects: CConstPointer<AnyObject?>,
// init(withObjects objects: ConstUnsafePointer<AnyObject?>,
// count cnt: Int)
self.init(objects: UnsafePointer(x.elementStorage), count: x.count)
_fixLifetime(x)
@@ -1326,7 +1343,7 @@ extension NSSet {
// - (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
// Imported as:
// @objc(initWithObjects:count:)
// init(withObjects objects: CConstPointer<AnyObject?>, count cnt: Int)
// init(withObjects objects: ConstUnsafePointer<AnyObject?>, count cnt: Int)
self.init(objects: UnsafePointer(x.elementStorage), count: x.count)
_fixLifetime(x)
}

View File

@@ -90,8 +90,8 @@ extension String {
/// non-`nil`, convert the buffer to an `Index` and write it into the
/// memory referred to by `index`
func _withOptionalOutParameter<Result>(
index: CMutablePointer<Index>,
body: (CMutablePointer<Int>)->Result
index: _CPointerTo<Index>.Mutable,
body: (_CPointerTo<Int>.Mutable)->Result
) -> Result {
var utf16Index: Int = 0
let result = index._withBridgeValue(&utf16Index) {
@@ -105,8 +105,8 @@ extension String {
/// from non-`nil`, convert the buffer to a `Range<Index>` and write
/// it into the memory referred to by `range`
func _withOptionalOutParameter<Result>(
range: CMutablePointer<Range<Index>>,
body: (CMutablePointer<NSRange>)->Result
range: _CPointerTo<Range<Index>>.Mutable,
body: (_CPointerTo<NSRange>.Mutable)->Result
) -> Result {
var nsRange = NSRange(location: 0, length: 0)
let result = range._withBridgeValue(&nsRange) {
@@ -206,7 +206,7 @@ extension String {
/// interpret the file.
@public static func stringWithContentsOfFile(
path: String,
usedEncoding: CMutablePointer<NSStringEncoding> = nil,
usedEncoding: _CPointerTo<NSStringEncoding>.Mutable = nil,
error: NSErrorPointer = nil
) -> String? {
return usedEncoding.withUnsafePointer {
@@ -238,7 +238,7 @@ extension String {
/// data. Errors are written into the inout `error` argument.
@public static func stringWithContentsOfURL(
url: NSURL,
usedEncoding enc: CMutablePointer<NSStringEncoding> = nil,
usedEncoding enc: _CPointerTo<NSStringEncoding>.Mutable = nil,
error: NSErrorPointer = nil
) -> String? {
return enc.withUnsafePointer {
@@ -380,9 +380,9 @@ extension String {
/// reference the longest path that matches the `String`.
/// Returns the actual number of matching paths.
@public func completePathIntoString(
_ outputName: CMutablePointer<String> = nil,
_ outputName: _CPointerTo<String>.Mutable = nil,
caseSensitive: Bool,
matchesIntoArray: CMutablePointer<[String]> = nil,
matchesIntoArray: _CPointerTo<[String]>.Mutable = nil,
filterTypes: [String]? = nil
) -> Int {
var nsMatches: NSArray?
@@ -495,12 +495,12 @@ extension String {
/// Enumerates all the lines in a string.
@public func enumerateLines(body: (line: String, inout stop: Bool)->()) {
_ns.enumerateLinesUsingBlock {
(line: String?, stop: CMutablePointer<ObjCBool>)
(line: String?, stop: _CPointerTo<ObjCBool>.Mutable)
in
var stop_ = false
body(line: line!, stop: &stop_)
if stop_ {
UnsafePointer(stop).memory = true
UnsafePointer<ObjCBool>(stop).memory = true
}
}
}
@@ -608,11 +608,11 @@ extension String {
@public func getBytes(
inout buffer: [UInt8],
maxLength: Int,
usedLength: CMutablePointer<Int>,
usedLength: _CPointerTo<Int>.Mutable,
encoding: NSStringEncoding,
options: NSStringEncodingConversionOptions,
range: Range<Index>,
remainingRange: CMutablePointer<Range<Index>>
remainingRange: _CPointerTo<Range<Index>>.Mutable
) -> Bool {
return _withOptionalOutParameter(remainingRange) {
self._ns.getBytes(
@@ -664,9 +664,9 @@ extension String {
/// Returns by reference the beginning of the first line and
/// the end of the last line touched by the given range.
@public func getLineStart(
start: CMutablePointer<Index>,
end: CMutablePointer<Index>,
contentsEnd: CMutablePointer<Index>,
start: _CPointerTo<Index>.Mutable,
end: _CPointerTo<Index>.Mutable,
contentsEnd: _CPointerTo<Index>.Mutable,
forRange: Range<Index>
) {
_withOptionalOutParameter(start) {
@@ -690,9 +690,9 @@ extension String {
/// Returns by reference the beginning of the first paragraph
/// and the end of the last paragraph touched by the given range.
@public func getParagraphStart(
start: CMutablePointer<Index>,
end: CMutablePointer<Index>,
contentsEnd: CMutablePointer<Index>,
start: _CPointerTo<Index>.Mutable,
end: _CPointerTo<Index>.Mutable,
contentsEnd: _CPointerTo<Index>.Mutable,
forRange: Range<Index>
) {
_withOptionalOutParameter(start) {
@@ -748,7 +748,7 @@ extension String {
/// in a given encoding, and optionally frees the buffer. WARNING:
/// this method is not memory-safe!
@public static func stringWithBytesNoCopy(
bytes: CMutableVoidPointer, length: Int,
bytes: _CMutableVoidPointer, length: Int,
encoding: NSStringEncoding, freeWhenDone flag: Bool
) -> String? {
return NSString(
@@ -766,7 +766,7 @@ extension String {
/// given number of characters from a given array of Unicode
/// characters.
@public init(
utf16CodeUnits: CConstPointer<unichar>,
utf16CodeUnits: _CPointerTo<unichar>.Const,
count: Int
) {
self = NSString(characters: utf16CodeUnits, length: count)
@@ -780,7 +780,7 @@ extension String {
/// Returns an initialized `String` object that contains a given
/// number of characters from a given array of UTF16 Code Units
@public init(
utf16CodeUnitsNoCopy: CConstPointer<unichar>,
utf16CodeUnitsNoCopy: _CPointerTo<unichar>.Const,
count: Int,
freeWhenDone flag: Bool
) {
@@ -930,7 +930,7 @@ extension String {
scheme tagScheme: String,
options opts: NSLinguisticTaggerOptions = nil,
orthography: NSOrthography? = nil,
tokenRanges: CMutablePointer<[Range<Index>]> = nil // FIXME:Can this be nil?
tokenRanges: _CPointerTo<[Range<Index>]>.Mutable = nil // FIXME:Can this be nil?
) -> [String] {
var nsTokenRanges: NSArray? = nil
let result = tokenRanges._withBridgeObject(&nsTokenRanges) {