Files
swift-mirror/stdlib/core/NSSwiftArray.swift
Dave Abrahams 6d1095f44e Protocol names end in "Type," "ible," or "able"
Mechanically add "Type" to the end of any protocol names that don't end
in "Type," "ible," or "able."  Also, drop "Type" from the end of any
associated type names, except for those of the *LiteralConvertible
protocols.

There are obvious improvements to make in some of these names, which can
be handled with separate commits.

Fixes <rdar://problem/17165920> Protocols `Integer` etc should get
uglier names.

Swift SVN r19883
2014-07-12 17:29:57 +00:00

104 lines
3.6 KiB
Swift

//===--- NSSwiftArray.swift - Links NSArray and _ContiguousArrayStorage ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// _NSSwiftArray supplies the implementation of the _CocoaArrayType API
// (and thus, NSArray the API) for our _ContiguousArrayStorage<T>. We
// can't put this implementation directly on _ContiguousArrayStorage
// because generic classes can't override Objective-C selectors.
//
//===----------------------------------------------------------------------===//
import SwiftShims
// Base class of the heap buffer implementation backing the new Array
// design.
@objc internal
class _NSSwiftArray : HeapBufferStorageBase, _CocoaArrayType {
typealias Buffer = HeapBuffer<_ArrayBody, AnyObject>
func canStoreElementsOfDynamicType(_: Any.Type) -> Bool {
_fatalError(
"Concrete subclasses must implement canStoreElementsOfDynamicType")
}
/// A type that every element in the array is.
var staticElementType: Any.Type {
_fatalError(
"Concrete subclasses must implement dynamicElementType")
}
/// Returns the object located at the specified index.
func objectAtIndex(index: Int) -> AnyObject {
let buffer = reinterpretCast(self) as Buffer
// If used as an NSArray, the element type can have no fancy
// bridging; just get it and return it
_sanityCheck(buffer.value.elementTypeIsBridgedVerbatim)
return buffer[index]
}
// Copies the objects contained in the array that fall within the
// specified range to aBuffer.
func getObjects(aBuffer: UnsafePointer<AnyObject>, range: _SwiftNSRange) {
// These objects are "returned" at +0, so treat them as values to
// avoid retains.
var dst = UnsafePointer<Word>(aBuffer)
let buffer = reinterpretCast(self) as Buffer
// If used as an NSArray, the element type can have no fancy
// bridging; just get it and return it. Note however that if
// we're empty we might be emptyNSSwiftArray so don't assert in
// that case.
_sanityCheck(count == 0 || buffer.value.elementTypeIsBridgedVerbatim)
if _fastPath(buffer.value.elementTypeIsBridgedVerbatim) {
dst.initializeFrom(
UnsafePointer(buffer.elementStorage + range.location),
count: range.length)
}
for i in range.location..<range.location + range.length {
dst++.initialize(reinterpretCast(buffer[i]))
}
}
func copyWithZone(_: COpaquePointer) -> _CocoaArrayType {
return self
}
func countByEnumeratingWithState(
state: UnsafePointer<_SwiftNSFastEnumerationState>,
objects: UnsafePointer<AnyObject>, count bufferSize: Int
) -> Int {
var enumerationState = state.memory
let buffer = reinterpretCast(self) as Buffer
// If used as an NSArray, the element type can have no fancy
// bridging; just get it and return it
_sanityCheck(buffer.value.elementTypeIsBridgedVerbatim)
if enumerationState.state != 0 {
return 0
}
enumerationState.mutationsPtr = _fastEnumerationStorageMutationsPtr
enumerationState.itemsPtr = reinterpretCast(buffer.elementStorage)
enumerationState.state = 1
state.memory = enumerationState
return buffer.value.count
}
var count: Int {
return (reinterpretCast(self) as Buffer).value.count
}
}