-
Notifications
You must be signed in to change notification settings - Fork 104
/
XMLEncodingStorage.swift
66 lines (52 loc) · 1.62 KB
/
XMLEncodingStorage.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
//
// XMLEncodingStorage.swift
// XMLCoder
//
// Created by Shawn Moore on 11/22/17.
// Copyright © 2017 Shawn Moore. All rights reserved.
//
import Foundation
// MARK: - Encoding Storage and Containers
struct XMLEncodingStorage {
// MARK: Properties
/// The container stack.
private var containers: [Box] = []
// MARK: - Initialization
/// Initializes `self` with no containers.
init() {}
// MARK: - Modifying the Stack
var count: Int {
return containers.count
}
var lastContainer: Box? {
return containers.last
}
mutating func pushKeyedContainer(_ keyedBox: KeyedBox = KeyedBox()) -> SharedBox<KeyedBox> {
let container = SharedBox(keyedBox)
containers.append(container)
return container
}
mutating func pushChoiceContainer() -> SharedBox<ChoiceBox> {
let container = SharedBox(ChoiceBox())
containers.append(container)
return container
}
mutating func pushUnkeyedContainer() -> SharedBox<UnkeyedBox> {
let container = SharedBox(UnkeyedBox())
containers.append(container)
return container
}
mutating func push(container: Box) {
if let keyedBox = container as? KeyedBox {
containers.append(SharedBox(keyedBox))
} else if let unkeyedBox = container as? UnkeyedBox {
containers.append(SharedBox(unkeyedBox))
} else {
containers.append(container)
}
}
mutating func popContainer() -> Box {
precondition(!containers.isEmpty, "Empty container stack.")
return containers.popLast()!
}
}