Files
swift-mirror/stdlib/core/ArrayBridge.swift
Dave Abrahams 9a13a7148b [stdlib] Consolidate bridging protocols
Squash _[Conditionally]BridgedToObjectiveC into one protocol.  This
change results in simpler bridging code with fewer dynamic protocol
conformance checks, and solves the nasty naming/semantics problem that
resulted from having _ConditionallyBridgedToObjectiveC refining
_BridgedToObjectiveC.

Also, rename things so they're more symmetrical and less confusing.

Swift SVN r20664
2014-07-29 01:30:27 +00:00

151 lines
5.4 KiB
Swift

//===--- ArrayBridge.swift - Casts and conversions for Array --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Because NSArray is effectively an [AnyObject], casting [T] -> [U]
// is an integral part of the bridging process and these two issues
// are handled together.
//
//===----------------------------------------------------------------------===//
/// Implements the semantics of `x as [Derived]` where `x` has type
/// `[Base]` and `Derived` is a verbatim-bridged trivial subtype of
/// `Base`.
public func _arrayDownCast<Base, Derived>(a: Array<Base>) -> [Derived] {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Base.self))
_sanityCheck(_isBridgedVerbatimToObjectiveC(Derived.self))
let native = a._buffer.requestNativeBuffer()
// Fast path: a native buffer that already stores elements of the
// Derived type.
if _fastPath(native != nil) {
if _fastPath(native!.storesOnlyElementsOfType(Derived.self)) {
return Array(a._buffer.castToBufferOf(Derived.self))
}
}
// FIXME: Make these checks deferred.
let result: [Derived]? = _arrayDownCastConditional(a)
_precondition(result != nil, "array cannot be downcast to array of derived")
return result!
}
/// Implements the semantics of `x as? [Derived]` where `x` has type
/// `[Base]` and `Derived` is a verbatim-bridged trivial subtype of
/// `Base`.
///
/// Returns an Array<Derived> containing the same elements as a in
/// O(1) iff a's buffer elements are dynamically known to have
/// type Derived or a type derived from Derived.
public func _arrayDownCastConditional<Base, Derived>(
a: Array<Base>
) -> [Derived]? {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Base.self))
_sanityCheck(_isBridgedVerbatimToObjectiveC(Derived.self))
if _fastPath(!a.isEmpty) {
let native = a._buffer.requestNativeBuffer()
if _fastPath(native != nil) {
if native!.storesOnlyElementsOfType(Derived.self) {
return Array(a._buffer.castToBufferOf(Derived.self))
}
return nil
}
// slow path: we store an NSArray
// We can skip the check if Derived happens to be AnyObject
if !(AnyObject.self is Derived.Type) {
for element in a {
// FIXME: unsafeBitCast works around <rdar://problem/16953026>
if !(unsafeBitCast(element, AnyObject.self) is Derived) {
return nil
}
}
}
return Array(a._buffer.castToBufferOf(Derived.self))
}
return []
}
/// Convert a to its corresponding bridged array type.
/// Precondition: T is bridged non-verbatim to objective C
/// O(N), because each element must be bridged separately.
public func _arrayBridgeToObjectiveC<BridgesToDerived, Base>(
source: Array<BridgesToDerived>
) -> Array<Base> {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Base.self))
_sanityCheck(!_isBridgedVerbatimToObjectiveC(BridgesToDerived.self))
var buf = _ContiguousArrayBuffer<Base>(count: source.count, minimumCapacity: 0)
var p = buf._unsafeElementStorage
for value in source {
let bridged: AnyObject? = _bridgeToObjectiveC(value)
_precondition(bridged != nil, "array element cannot be bridged to Objective-C")
p++.initialize(unsafeBitCast(bridged!, Base.self))
}
return Array(_ArrayBuffer(buf))
}
/// Try to convert the source array of objects to an array of values
/// produced by bridging the objects from Objective-C to \c
/// BridgesToDerived.
///
/// Precondition: Base is a class type.
/// Precondition: BridgesToDerived is bridged non-verbatim to Objective-C.
/// O(n), because each element must be bridged separately.
public func _arrayBridgeFromObjectiveC<Base, BridgesToDerived>(
source: Array<Base>
) -> Array<BridgesToDerived> {
let result: Array<BridgesToDerived>?
= _arrayBridgeFromObjectiveCConditional(source);
_precondition(result != nil, "array cannot be bridged from Objective-C")
return result!
}
/// Try to convert the source array of objects to an array of values
/// produced by bridging the objects from Objective-C to \c
/// BridgesToDerived.
///
/// Precondition: Base is a class type.
/// Precondition: BridgesToDerived is bridged non-verbatim to Objective-C.
/// O(n), because each element must be bridged separately.
public func _arrayBridgeFromObjectiveCConditional<Base, BridgesToDerived>(
source: Array<Base>
) -> Array<BridgesToDerived>? {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Base.self))
_sanityCheck(!_isBridgedVerbatimToObjectiveC(BridgesToDerived.self))
var buf = _ContiguousArrayBuffer<BridgesToDerived>(count: source.count,
minimumCapacity: 0)
var p = buf._unsafeElementStorage
ElementwiseBridging:
do {
for object: Base in source {
let value = Swift._conditionallyBridgeFromObjectiveC(
unsafeBitCast(object, AnyObject.self), BridgesToDerived.self)
if _slowPath(value == nil) {
break ElementwiseBridging
}
p++.initialize(value!)
}
return Array(_ArrayBuffer(buf))
}
while false
// Don't destroy anything we never created.
buf.count = p - buf._unsafeElementStorage
// Report failure
return nil
}