Files
swift-mirror/stdlib/core/NSSwiftArray.swift
Jordan Rose cca27d02a0 Tag everything in the standard library with accessibility attributes.
Keep calm: remember that the standard library has many more public exports
than the average target, and that this contains ALL of them at once.
I also deliberately tried to tag nearly every top-level decl, even if that
was just to explicitly mark things @internal, to make sure I didn't miss
something.

This does export more than we might want to, mostly for protocol conformance
reasons, along with our simple-but-limiting typealias rule. I tried to also
mark things private where possible, but it's really going to be up to the
standard library owners to get this right. This is also only validated
against top-level access control; I haven't fully tested against member-level
access control yet, and none of our semantic restrictions are in place.

Along the way I also noticed bits of stdlib cruft; to keep this patch
understandable, I didn't change any of them.

Swift SVN r19145
2014-06-24 21:32:18 +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 _CocoaArray 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, _CocoaArray {
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) -> _CocoaArray {
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
}
}