-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAnyMapHolder.swift
342 lines (298 loc) · 9.31 KB
/
AnyMapHolder.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// AnyMapHolder.swift
// NitroModules
//
// Created by Marc Rousavy on 20.08.24.
//
import Foundation
/**
* Represents any value representable by the `AnyMap`.
* Note: Arrays are currently not implemented due to a Swift compiler bug https://github.com/swiftlang/swift/issues/75994
*/
public indirect enum AnyValue {
case null
case number(Double)
case bool(Bool)
case bigint(Int64)
case string(String)
case array(Array<AnyValue>)
case object(Dictionary<String, AnyValue>)
static func create(_ value: margelo.nitro.AnyValue) -> AnyValue {
if margelo.nitro.is_AnyValue_null(value) {
return .null
} else if margelo.nitro.is_AnyValue_bool(value) {
return .bool(margelo.nitro.get_AnyValue_bool(value))
} else if margelo.nitro.is_AnyValue_number(value) {
return .number(margelo.nitro.get_AnyValue_number(value))
} else if margelo.nitro.is_AnyValue_bigint(value) {
return .bigint(margelo.nitro.get_AnyValue_bigint(value))
} else if margelo.nitro.is_AnyValue_string(value) {
return .string(margelo.nitro.get_AnyValue_string(value).toSwift())
} else if margelo.nitro.is_AnyValue_AnyArray(value) {
return .array(margelo.nitro.get_AnyValue_AnyArray(value).toSwift())
} else if margelo.nitro.is_AnyValue_AnyObject(value) {
return .object(margelo.nitro.get_AnyValue_AnyObject(value).toSwift())
} else {
fatalError("AnyValue has unknown type!")
}
}
}
/**
* Represents an `AnyMap` that can be passed to Swift.
*/
public class AnyMapHolder {
public let cppPart: margelo.nitro.TSharedMap
public init() {
cppPart = margelo.nitro.AnyMap.make()
}
public init(withPreallocatedSize size: Int) {
cppPart = margelo.nitro.AnyMap.make(size)
}
public init(withCppPart otherCppPart: margelo.nitro.TSharedMap) {
cppPart = otherCppPart
}
// pragma MARK: Common Operations
/**
* Returns whether the given key exists in the map.
*/
public func contains(key: String) -> Bool {
return cppPart.pointee.contains(std.string(key))
}
/**
* Removes the given key from the map.
*/
public func remove(key: String) {
cppPart.pointee.remove(std.string(key))
}
/**
* Removes all keys in this map.
*/
public func clear() {
cppPart.pointee.clear()
}
// pragma MARK: Getters
/**
* Gets the double value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getDouble(key: String) -> Double {
return cppPart.pointee.getDouble(std.string(key))
}
/**
* Gets the boolean value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getBoolean(key: String) -> Bool {
return cppPart.pointee.getBoolean(std.string(key))
}
/**
* Gets the bigint value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getBigInt(key: String) -> Int64 {
return cppPart.pointee.getBigInt(std.string(key))
}
/**
* Gets the string value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getString(key: String) -> String {
let value = cppPart.pointee.getString(std.string(key))
return String(value)
}
/**
* Gets the array value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getArray(key: String) -> [AnyValue] {
let value = cppPart.pointee.getArray(std.string(key))
return value.toSwift()
}
/**
* Gets the object value at the given key.
* If no value exists at the given key, or if it is not a double,
* this function throws.
*/
public func getObject(key: String) -> Dictionary<String, AnyValue> {
let value = cppPart.pointee.getObject(std.string(key))
return value.toSwift()
}
// pragma MARK: Setters
/**
* Set the given key to `null`.
*/
public func setNull(key: String) {
cppPart.pointee.setNull(std.string(key))
}
/**
* Set the given key to the given double value.
*/
public func setDouble(key: String, value: Double) {
cppPart.pointee.setDouble(std.string(key), value)
}
/**
* Set the given key to the given boolean value.
*/
public func setBoolean(key: String, value: Bool) {
cppPart.pointee.setBoolean(std.string(key), value)
}
/**
* Set the given key to the given bigint value.
*/
public func setBigInt(key: String, value: Int64) {
cppPart.pointee.setBigInt(std.string(key), value)
}
/**
* Set the given key to the given string value.
*/
public func setString(key: String, value: String) {
cppPart.pointee.setString(std.string(key), std.string(value))
}
/**
* Set the given key to the given array value.
*/
public func setArray(key: String, value: [AnyValue]) {
cppPart.pointee.setArray(std.string(key), margelo.nitro.AnyArray.create(value))
}
/**
* Set the given key to the given object value.
*/
public func setObject(key: String, value: Dictionary<String, AnyValue>) {
cppPart.pointee.setObject(std.string(key), margelo.nitro.AnyObject.create(value))
}
// pragma MARK: Is Getters
/**
* Gets whether the given `key` is holding a null value, or not.
*/
public func isNull(key: String) -> Bool {
return cppPart.pointee.isNull(std.string(key))
}
/**
* Gets whether the given `key` is holding a double value, or not.
*/
public func isDouble(key: String) -> Bool {
return cppPart.pointee.isDouble(std.string(key))
}
/**
* Gets whether the given `key` is holding a boolean value, or not.
*/
public func isBool(key: String) -> Bool {
return cppPart.pointee.isBoolean(std.string(key))
}
/**
* Gets whether the given `key` is holding a bigint value, or not.
*/
public func isBigInt(key: String) -> Bool {
return cppPart.pointee.isBigInt(std.string(key))
}
/**
* Gets whether the given `key` is holding a string value, or not.
*/
public func isString(key: String) -> Bool {
return cppPart.pointee.isString(std.string(key))
}
/**
* Gets whether the given `key` is holding an array value, or not.
*/
public func isArray(key: String) -> Bool {
return cppPart.pointee.isArray(std.string(key))
}
/**
* Gets whether the given `key` is holding an object value, or not.
*/
public func isObject(key: String) -> Bool {
return cppPart.pointee.isObject(std.string(key))
}
}
// pragma MARK: margelo.nitro.AnyValue extension
extension margelo.nitro.AnyValue {
static func create(_ value: AnyValue) -> margelo.nitro.AnyValue {
switch value {
case .null:
return create()
case .bool(let bool):
return create(bool)
case .number(let number):
return create(number)
case .bigint(let bigint):
return create(bigint)
case .string(let string):
return create(string)
case .array(let array):
return create(array)
case .object(let object):
return create(object)
}
}
static func create() -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue()
}
static func create(_ value: Bool) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(value)
}
static func create(_ value: Double) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(value)
}
static func create(_ value: Int64) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(value)
}
static func create(_ value: String) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(std.string(value))
}
static func create(_ value: [AnyValue]) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(margelo.nitro.AnyArray.create(value))
}
static func create(_ value: Dictionary<String, AnyValue>) -> margelo.nitro.AnyValue {
return margelo.nitro.create_AnyValue(margelo.nitro.AnyObject.create(value))
}
}
// pragma MARK: std.string extension
extension std.string {
func toSwift() -> String {
return String(self)
}
}
// pragma MARK: margelo.nitro.AnyArray extension
extension margelo.nitro.AnyArray {
static func create(_ array: [AnyValue]) -> margelo.nitro.AnyArray {
var vector = margelo.nitro.AnyArray()
vector.reserve(array.count)
for value in array {
vector.push_back(margelo.nitro.AnyValue.create(value))
}
return vector
}
func toSwift() -> [AnyValue] {
var array: [AnyValue] = []
array.reserveCapacity(self.size())
for value in self {
array.append(AnyValue.create(value))
}
return array
}
}
// pragma MARK: margelo.nitro.AnyObject extension
extension margelo.nitro.AnyObject {
static func create(_ dictionary: Dictionary<String, AnyValue>) -> margelo.nitro.AnyObject {
var object = margelo.nitro.AnyObject()
object.reserve(dictionary.count)
for (key, value) in dictionary {
object[std.string(key)] = margelo.nitro.AnyValue.create(value)
}
return object
}
func toSwift() -> Dictionary<String, AnyValue> {
let keys = margelo.nitro.getAnyObjectKeys(self)
var dictionary = Dictionary<String, AnyValue>(minimumCapacity: keys.size())
for key in keys {
let value = margelo.nitro.getAnyObjectValue(self, key)
dictionary[String(key)] = AnyValue.create(value)
}
return dictionary
}
}