ns prefix 外伝 … copy-on-write #関モバ

Post on 16-Apr-2017

2.109 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

• NSString, String

• NSData, Data

• NSArray, Array<Element>

// 要所だけ抜粋 protocol ReferenceConvertible : _ObjectiveCBridgeable {

// 参照型は Objective-C 互換で、コピー可能 associatedtype ReferenceType : NSObject, NSCopying

}

public struct Data : ReferenceConvertible, _MutablePairBoxing {

// NSData とのブリッジ public typealias ReferenceType = NSData

// 参照をラップ (Boxing) する internal var _wrapped : _SwiftNSData

internal final class _SwiftNSData { // NSData と NSMutableData とを管理するラッパー var __wrapped : _MutableUnmanagedWrapper< NSData, NSMutableData>

internal enum _MutableUnmanagedWrapper< ImmutableType, MutableType> {

case Immutable(Unmanaged<ImmutableType>) case Mutable(Unmanaged<MutableType>) }

public init(bytes: UnsafeRawPointer, count: Int) {

_wrapped = _SwiftNSData(immutableObject: NSData(bytes: bytes, length: count)) }

public mutating func append(_ other: Data) {

// この関数で mutableCopy の必要性を判断・実施 _applyUnmanagedMutation { $0.append(other) }

}

public init(capacity: Int) {

if let d = NSMutableData(capacity: capacity) {

_wrapped = _SwiftNSData(mutableObject: d)

} else {

fatalError("Unable to allocate data of the requested capacity") } }

public func _bridgeToObjectiveC() -> NSData {

// NSData.self は 8 Byte return unsafeBitCast(_wrapped, to: NSData.self) }

public static func _forceBridgeFromObjectiveC( _ input: NSData, result: inout Data?) {

result = Data(referencing: input) }

public init(referencing reference: NSData) {

_wrapped = _SwiftNSData(immutableObject: reference.copy() as AnyObject) }

public struct Date : ReferenceConvertible {

// NSDate とのブリッジ public typealias ReferenceType = NSDate

// データを TimeInterval (Double) で直接保持 fileprivate var _time : TimeInterval

public init(timeIntervalSinceReferenceDate ti: TimeInterval) { _time = ti }

public mutating func addTimeInterval( _ timeInterval: TimeInterval) {

self += timeInterval }

public func _bridgeToObjectiveC() -> NSDate {

return NSDate(timeIntervalSinceReferenceDate: _time) }

public static func _conditionallyBridgeFromObjectiveC( _ x: NSDate, result: inout Date?) -> Bool {

result = Date(timeIntervalSinceReferenceDate: x.timeIntervalSinceReferenceDate) return true }

top related