-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNSCoder.swift
318 lines (259 loc) · 10.8 KB
/
NSCoder.swift
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// 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
//
extension NSCoder {
/*!
Describes the action an NSCoder should take when it encounters decode failures (e.g. corrupt data) for non-TopLevel decodes. Darwin platfrom supports exceptions here, and there may be other approaches supported in the future, so its included for completeness.
*/
public enum DecodingFailurePolicy : Int {
case setErrorAndReturn
}
}
public protocol NSCoding {
func encode(with aCoder: NSCoder)
init?(coder aDecoder: NSCoder)
}
public protocol NSSecureCoding : NSCoding {
static var supportsSecureCoding: Bool { get }
}
open class NSCoder : NSObject {
internal var _pendingBuffers = Array<(UnsafeMutableRawPointer, Int)>()
deinit {
for buffer in _pendingBuffers {
// Cannot deinitialize a pointer to unknown type.
buffer.0.deallocate(bytes: buffer.1, alignedTo: MemoryLayout<Int>.alignment)
}
}
open func encodeValue(ofObjCType type: UnsafePointer<Int8>, at addr: UnsafeRawPointer) {
NSRequiresConcreteImplementation()
}
open func encode(_ data: Data) {
NSRequiresConcreteImplementation()
}
open func decodeValue(ofObjCType type: UnsafePointer<Int8>, at data: UnsafeMutableRawPointer) {
NSRequiresConcreteImplementation()
}
open func decodeData() -> Data? {
NSRequiresConcreteImplementation()
}
open func version(forClassName className: String) -> Int {
NSRequiresConcreteImplementation()
}
open func decodeObject<DecodedObjectType: NSCoding>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType? where DecodedObjectType: NSObject {
NSUnimplemented()
}
/*!
@method decodeObjectOfClasses:forKey:
@abstract Decodes an object for the key, restricted to the specified classes.
@param classes An array of the expected classes.
@param key The code key.
@return The decoded object.
@discussion This function signature differs from Foundation OS X in that
classes is an array of Classes, not a NSSet. This is because AnyClass cannot
be casted to NSObject, nor is it Hashable.
*/
open func decodeObject(of classes: [AnyClass]?, forKey key: String) -> Any? {
NSUnimplemented()
}
open func decodeTopLevelObject() throws -> Any? {
NSUnimplemented()
}
open func decodeTopLevelObject(forKey key: String) throws -> Any? {
NSUnimplemented()
}
open func decodeTopLevelObject<DecodedObjectType: NSCoding>(of cls: DecodedObjectType.Type, forKey key: String) throws -> DecodedObjectType? where DecodedObjectType: NSObject {
NSUnimplemented()
}
/*!
@method decodeTopLevelObjectOfClasses:
@abstract Decodes an top-level object for the key, restricted to the specified classes.
@param classes An array of the expected classes.
@param key The code key.
@return The decoded object.
@discussion This function signature differs from Foundation OS X in that
classes is an array of Classes, not a NSSet. This is because AnyClass cannot
be casted to NSObject, nor is it Hashable.
*/
open func decodeTopLevelObject(of classes: [AnyClass], forKey key: String) throws -> Any? {
NSUnimplemented()
}
open func encode(_ object: Any?) {
var object = object
withUnsafePointer(to: &object) { (ptr: UnsafePointer<Any?>) -> Void in
encodeValue(ofObjCType: "@", at: UnsafeRawPointer(ptr))
}
}
open func encodeRootObject(_ rootObject: Any) {
encode(rootObject)
}
open func encodeBycopyObject(_ anObject: Any?) {
encode(anObject)
}
open func encodeByrefObject(_ anObject: Any?) {
encode(anObject)
}
open func encodeConditionalObject(_ object: Any?) {
encode(object)
}
open func encodeArray(ofObjCType type: UnsafePointer<Int8>, count: Int, at array: UnsafeRawPointer) {
encodeValue(ofObjCType: "[\(count)\(String(cString: type))]", at: array)
}
open func encodeBytes(_ byteaddr: UnsafeRawPointer?, length: Int) {
var newLength = UInt32(length)
withUnsafePointer(to: &newLength) { (ptr: UnsafePointer<UInt32>) -> Void in
encodeValue(ofObjCType: "I", at: ptr)
}
var empty: [Int8] = []
withUnsafePointer(to: &empty) {
encodeArray(ofObjCType: "c", count: length, at: byteaddr ?? UnsafeRawPointer($0))
}
}
open func decodeObject() -> Any? {
if self.error != nil {
return nil
}
var obj: Any? = nil
withUnsafeMutablePointer(to: &obj) { (ptr: UnsafeMutablePointer<Any?>) -> Void in
decodeValue(ofObjCType: "@", at: UnsafeMutableRawPointer(ptr))
}
return obj
}
open func decodeArray(ofObjCType itemType: UnsafePointer<Int8>, count: Int, at array: UnsafeMutableRawPointer) {
decodeValue(ofObjCType: "[\(count)\(String(cString: itemType))]", at: array)
}
/*
// TODO: This is disabled, as functions which return unsafe interior pointers are inherently unsafe when we have no autorelease pool.
open func decodeBytes(withReturnedLength lengthp: UnsafeMutablePointer<Int>) -> UnsafeMutableRawPointer? {
var length: UInt32 = 0
withUnsafeMutablePointer(to: &length) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self))
}
// we cannot autorelease here so instead the pending buffers will manage the lifespan of the returned data... this is wasteful but good enough...
let result = UnsafeMutableRawPointer.allocate(bytes: Int(length), alignedTo: MemoryLayout<Int>.alignment)
decodeValue(ofObjCType: "c", at: result)
lengthp.pointee = Int(length)
_pendingBuffers.append((result, Int(length)))
return result
}
*/
open func encodePropertyList(_ aPropertyList: Any) {
NSUnimplemented()
}
open func decodePropertyList() -> Any? {
NSUnimplemented()
}
open var systemVersion: UInt32 {
return 1000
}
open var allowsKeyedCoding: Bool {
return false
}
open func encode(_ objv: Any?, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encodeConditionalObject(_ objv: Any?, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encode(_ boolv: Bool, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encode(_ intv: Int32, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encode(_ intv: Int64, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encode(_ realv: Float, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encode(_ realv: Double, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func encodeBytes(_ bytesp: UnsafePointer<UInt8>?, length lenv: Int, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func containsValue(forKey key: String) -> Bool {
NSRequiresConcreteImplementation()
}
open func decodeObject(forKey key: String) -> Any? {
NSRequiresConcreteImplementation()
}
open func decodeBool(forKey key: String) -> Bool {
NSRequiresConcreteImplementation()
}
// NOTE: this equivalent to the decodeIntForKey: in Objective-C implementation
open func decodeCInt(forKey key: String) -> Int32 {
NSRequiresConcreteImplementation()
}
open func decodeInt32(forKey key: String) -> Int32 {
NSRequiresConcreteImplementation()
}
open func decodeInt64(forKey key: String) -> Int64 {
NSRequiresConcreteImplementation()
}
open func decodeFloat(forKey key: String) -> Float {
NSRequiresConcreteImplementation()
}
open func decodeDouble(forKey key: String) -> Double {
NSRequiresConcreteImplementation()
}
// TODO: This is disabled, as functions which return unsafe interior pointers are inherently unsafe when we have no autorelease pool.
/*
open func decodeBytes(forKey key: String, returnedLength lengthp: UnsafeMutablePointer<Int>?) -> UnsafePointer<UInt8>? { // returned bytes immutable!
NSRequiresConcreteImplementation()
}
*/
/// - experimental: This method does not exist in the Darwin Foundation.
open func withDecodedUnsafeBufferPointer<ResultType>(forKey key: String, body: (UnsafeBufferPointer<UInt8>?) throws -> ResultType) rethrows -> ResultType {
NSRequiresConcreteImplementation()
}
open func encode(_ intv: Int, forKey key: String) {
NSRequiresConcreteImplementation()
}
open func decodeInteger(forKey key: String) -> Int {
NSRequiresConcreteImplementation()
}
open var requiresSecureCoding: Bool {
return false
}
open func decodePropertyListForKey(_ key: String) -> Any? {
NSUnimplemented()
}
/*!
@property allowedClasses
@abstract The set of coded classes allowed for secure coding. (read-only)
@discussion This property type differs from Foundation OS X in that
classes is an array of Classes, not a Set. This is because AnyClass is not
hashable.
*/
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
open var allowedClasses: [AnyClass]? {
NSUnimplemented()
}
open func failWithError(_ error: Error) {
NSUnimplemented()
// NOTE: disabled for now due to bridging uncertainty
// if let debugDescription = error.userInfo["NSDebugDescription"] {
// NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
// } else {
// NSLog("*** NSKeyedUnarchiver.init: decoding error")
// }
}
open var decodingFailurePolicy: NSCoder.DecodingFailurePolicy {
return .setErrorAndReturn
}
open var error: Error? {
NSRequiresConcreteImplementation()
}
internal func _decodeArrayOfObjectsForKey(_ key: String) -> [Any] {
NSRequiresConcreteImplementation()
}
internal func _decodePropertyListForKey(_ key: String) -> Any {
NSRequiresConcreteImplementation()
}
}