1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
@_implementationOnly import CoreFoundation
internal final class _NSCFDictionary : NSMutableDictionary {
deinit {
_CFDeinit(self)
_CFZeroUnsafeIvars(&_storage)
}
required init() {
fatalError()
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
required init(objects: UnsafePointer<AnyObject>!, forKeys keys: UnsafePointer<NSObject>!, count cnt: Int) {
fatalError()
}
required public convenience init(dictionaryLiteral elements: (Any, Any)...) {
fatalError("init(dictionaryLiteral:) has not been implemented")
}
override var count: Int {
return CFDictionaryGetCount(unsafeBitCast(self, to: CFDictionary.self))
}
override func object(forKey aKey: Any) -> Any? {
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(__SwiftValue.store(aKey), to: UnsafeRawPointer.self))
if value != nil {
return __SwiftValue.fetch(nonOptional: unsafeBitCast(value, to: AnyObject.self))
} else {
return nil
}
}
// This doesn't feel like a particularly efficient generator of CFDictionary keys, but it works for now. We should probably put a function into CF that allows us to simply iterate the keys directly from the underlying CF storage.
private struct _NSCFKeyGenerator : IteratorProtocol {
var keyArray : [NSObject] = []
var index : Int = 0
let count : Int
mutating func next() -> AnyObject? {
if index == count {
return nil
} else {
let item = keyArray[index]
index += 1
return item
}
}
init(_ dict : _NSCFDictionary) {
let cf = dict._cfObject
count = CFDictionaryGetCount(cf)
let keys = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: count)
CFDictionaryGetKeysAndValues(cf, keys, nil)
for idx in 0..<count {
let key = unsafeBitCast(keys.advanced(by: idx).pointee!, to: NSObject.self)
keyArray.append(key)
}
keys.deinitialize(count: 1)
keys.deallocate()
}
}
override func keyEnumerator() -> NSEnumerator {
return NSGeneratorEnumerator(_NSCFKeyGenerator(self))
}
override func removeObject(forKey aKey: Any) {
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(__SwiftValue.store(aKey), to: UnsafeRawPointer.self))
}
override func setObject(_ anObject: Any, forKey aKey: AnyHashable) {
CFDictionarySetValue(_cfMutableObject, unsafeBitCast(__SwiftValue.store(aKey), to: UnsafeRawPointer.self), unsafeBitCast(__SwiftValue.store(anObject), to: UnsafeRawPointer.self))
}
override var classForCoder: AnyClass {
return NSMutableDictionary.self
}
}
internal func _CFSwiftDictionaryGetCount(_ dictionary: AnyObject) -> CFIndex {
return (dictionary as! NSDictionary).count
}
internal func _CFSwiftDictionaryGetCountOfKey(_ dictionary: AnyObject, key: AnyObject) -> CFIndex {
if _CFSwiftDictionaryContainsKey(dictionary, key: key) {
return 1
} else {
return 0
}
}
internal func _CFSwiftDictionaryContainsKey(_ dictionary: AnyObject, key: AnyObject) -> Bool {
return (dictionary as! NSDictionary).object(forKey: key) != nil
}
//(AnyObject, AnyObject) -> Unmanaged<AnyObject>
internal func _CFSwiftDictionaryGetValue(_ dictionary: AnyObject, key: AnyObject) -> Unmanaged<AnyObject>? {
let dict = dictionary as! NSDictionary
if type(of: dictionary) === NSDictionary.self || type(of: dictionary) === NSMutableDictionary.self {
if let obj = dict._storage[key as! NSObject] {
return Unmanaged<AnyObject>.passUnretained(obj)
}
} else {
let k = __SwiftValue.fetch(nonOptional: key)
let value = dict.object(forKey: k)
let v = __SwiftValue.store(value)
dict._storage[key as! NSObject] = v
if let obj = v {
return Unmanaged<AnyObject>.passUnretained(obj)
}
}
return nil
}
internal func _CFSwiftDictionaryGetValueIfPresent(_ dictionary: AnyObject, key: AnyObject, value: UnsafeMutablePointer<Unmanaged<AnyObject>?>?) -> Bool {
if let val = _CFSwiftDictionaryGetValue(dictionary, key: key) {
value?.pointee = val
return true
} else {
value?.pointee = nil
return false
}
}
internal func _CFSwiftDictionaryGetCountOfValue(_ dictionary: AnyObject, value: AnyObject) -> CFIndex {
if _CFSwiftDictionaryContainsValue(dictionary, value: value) {
return 1
} else {
return 0
}
}
internal func _CFSwiftDictionaryContainsValue(_ dictionary: AnyObject, value: AnyObject) -> Bool {
if let value = value as? AnyHashable {
return (dictionary as! NSDictionary).allValues.lazy.compactMap { $0 as? AnyHashable }.contains(value)
} else {
return false
}
}
// HAZARD! WARNING!
// The contract of these CF APIs is that the elements in the buffers have a lifespan of the container dictionary.
// Since the public facing elements of the dictionary may be a structure (which could not be retained) or the boxing
// would potentially make a reference that is not the direct access of the element, this function must do a bit of
// hoop jumping to deal with storage.
// In the case of NSDictionary and NSMutableDictionary (NOT subclasses) we can directly reach into the storage and
// grab the un-fetched items. This allows the same behavior to be maintained.
// In the case of subclasses of either NSDictionary or NSMutableDictionary we cannot reconstruct boxes here since
// they will fall out of scope and be consumed by automatic reference counting at the end of this function. But since
// swift has a fragile layout we can reach into the super-class ivars (NSDictionary) and store boxed references to ensure lifespan
// is similar to how it works on Darwin. Effectively this binds the access point of all values and keys for those objects
// to have the same lifespan of the parent container object.
internal func _CFSwiftDictionaryGetValuesAndKeys(_ dictionary: AnyObject, valuebuf: UnsafeMutablePointer<Unmanaged<AnyObject>?>?, keybuf: UnsafeMutablePointer<Unmanaged<AnyObject>?>?) {
var idx = 0
if valuebuf == nil && keybuf == nil {
return
}
let dict = dictionary as! NSDictionary
if type(of: dictionary) === NSDictionary.self || type(of: dictionary) === NSMutableDictionary.self {
for (key, value) in dict._storage {
valuebuf?[idx] = Unmanaged<AnyObject>.passUnretained(value)
keybuf?[idx] = Unmanaged<AnyObject>.passUnretained(key)
idx += 1
}
} else {
dict.enumerateKeysAndObjects(options: []) { k, v, _ in
let key = __SwiftValue.store(k)
let value = __SwiftValue.store(v)
valuebuf?[idx] = Unmanaged<AnyObject>.passUnretained(value)
keybuf?[idx] = Unmanaged<AnyObject>.passUnretained(key)
dict._storage[key] = value
idx += 1
}
}
}
internal func _CFSwiftDictionaryApplyFunction(_ dictionary: AnyObject, applier: @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer) -> Void, context: UnsafeMutableRawPointer) {
(dictionary as! NSDictionary).enumerateKeysAndObjects(options: []) { key, value, _ in
applier(__SwiftValue.store(key), __SwiftValue.store(value), context)
}
}
internal func _CFSwiftDictionaryAddValue(_ dictionary: AnyObject, key: AnyObject, value: AnyObject) {
(dictionary as! NSMutableDictionary).setObject(value, forKey: key as! NSObject)
}
internal func _CFSwiftDictionaryReplaceValue(_ dictionary: AnyObject, key: AnyObject, value: AnyObject) {
(dictionary as! NSMutableDictionary).setObject(value, forKey: key as! NSObject)
}
internal func _CFSwiftDictionarySetValue(_ dictionary: AnyObject, key: AnyObject, value: AnyObject) {
(dictionary as! NSMutableDictionary).setObject(value, forKey: key as! NSObject)
}
internal func _CFSwiftDictionaryRemoveValue(_ dictionary: AnyObject, key: AnyObject) {
(dictionary as! NSMutableDictionary).removeObject(forKey: key)
}
internal func _CFSwiftDictionaryRemoveAllValues(_ dictionary: AnyObject) {
(dictionary as! NSMutableDictionary).removeAllObjects()
}
internal func _CFSwiftDictionaryCreateCopy(_ dictionary: AnyObject) -> Unmanaged<AnyObject> {
return Unmanaged<AnyObject>.passRetained((dictionary as! NSDictionary).copy() as! NSObject)
}
|